1

我有以下查询,其中 @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

有没有办法我可以做到这一点?

4

1 回答 1

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
于 2013-09-24T13:54:53.997 回答