SQL Query查找每个产品中的项目数 [英] SQL Query to find out number of items in each product

查看:142
本文介绍了SQL Query查找每个产品中的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一张如下表格

Hi
I Have a table like below

Name	Dept 	Date
Ravi	CS	10-Jan
Rahul	IT	11-Jan
Siva	CS	12-Jan
Arun	CS	15-Jan
Muthu	IT	16-Feb
Nisanth	IT	17-Feb
Sameer	CS	18-Feb



我想要的结果如下


I want result as follows

Month Dept Count 
Jan   CS    3
Jan   IT    1
Feb   CS    1
FEB   IT    2



我试过以下查询


I tried with below query

select SUM(A.Dept1) as Total,A.Month1,A.Dept from(
select count(Dept) as Dept1,Dept,MONTH(CompletedDate) as Month1  from Table
group by Status,CompletedDate
)A Group by A.Dept, A.Month1, A.Dept1



但它显示错误的输出

请帮我实现这个

谢谢


But it showing wrong output
Please help me to achive this
Thanks

推荐答案

这将返回完整日期名称,不是缩写:



This returns the full date name, not abbreviated:

SELECT DATENAME(month,[Date]), Dept,COUNT(*)
FROM MyTable
GROUP BY DATENAME(month,[Date]), Dept


这篇关于SQL Query查找每个产品中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆