我有以下查询,其中@BeginTime是该月的第一天,而@EndTime是该月的最后一天。
SET @BeginTime = RTRIM(@BeginTime) + ' 00:00:00'
SET @EndTime = RTRIM(@EndTime) + ' 23:59:59'
select C.Name , COUNT(DISTINCT D.Id) from DriverLic D
INNER JOIN Clov C WITH (NOLOCK) ON D.CId = C.CId
AND ((D.RDate < @EndTime)
AND ( D.UrDate > @BeginTime))
group by C.Name
我得到这样的输出:
Name Count(D.Id)
AC 22
AB 32
CD 11
我想得到这样的输出:
Year Month Name Count(D.id)
2013 8 AC 22
2013 8 AB 32
2013 8 CD 11
有没有办法实现这个目标?
答案 0 :(得分:4)
是的,
SELECT Year(yourDateColumn) AS 'Year', Month(yourDateColumn) AS 'Month', C.Name, COUNT(DISTINCT D.Id)
from DriverLic D
INNER JOIN Clov C WITH (NOLOCK) ON D.CId = C.CId
--WHERE your where conditions should go here...
GROUP BY
Year(yourDateColumn), Month(yourDateColumn), C.Name