我有一个问题:
SELECT
(CONVERT (DATE, mrSUBMITDATE)) as 'Date',
SUM(CASE WHEN Submission__bTracking = 'Email' THEN 1 ELSE 0 END) as SubmittedbyEmail,
SUM(CASE WHEN Submission__bTracking = 'Phone' THEN 1 ELSE 0 END) as SubmittedbyPhone,
SUM(CASE WHEN Submission__bTracking = 'Verbal' THEN 1 ELSE 0 END) as SubmittedbyVerbal,
SUM(CASE WHEN Submission__bTracking = 'Web' THEN 1 ELSE 0 END) as SubmittedbyWeb
FROM
MASTER30
WHERE
mrSUBMITDATE >= (CONVERT (DATE, '2015-01-01'))
AND mrSUBMITDATE < (CONVERT (DATE, '2015-02-01'))
GROUP BY
mrSUBMITDATE
ORDER BY
mrSUBMITDATE
产生以下结果:
Date SubmittedbyEmail SubmittedbyPhone SubmittedbyVerbal SubmittedbyWeb
2015-01-01 0 0 1 0
2015-01-01 0 0 1 0
2015-01-01 1 0 0 0
2015-01-01 0 1 0 0
2015-01-01 0 0 0 1
2015-01-01 0 0 1 0
2015-01-01 0 0 0 1
2015-01-01 0 0 1 0
<<snipped>>
我希望能够进行整合,以便每天显示总计,而不是每天显示多行。有人可以帮助我吗?
非常感谢。
答案 0 :(得分:3)
让GROUP BY
使用与CONVERT
子句中相同的SELECT
来电:
SELECT
(CONVERT (DATE, mrSUBMITDATE)) as 'Date',
SUM(CASE WHEN Submission__bTracking = 'Email' THEN 1 ELSE 0 END) as SubmittedbyEmail,
SUM(CASE WHEN Submission__bTracking = 'Phone' THEN 1 ELSE 0 END) as SubmittedbyPhone,
SUM(CASE WHEN Submission__bTracking = 'Verbal' THEN 1 ELSE 0 END) as SubmittedbyVerbal,
SUM(CASE WHEN Submission__bTracking = 'Web' THEN 1 ELSE 0 END) as SubmittedbyWeb
FROM MASTER30
WHERE mrSUBMITDATE >= (CONVERT (DATE, '2015-01-01'))
AND mrSUBMITDATE < (CONVERT (DATE, '2015-02-01'))
GROUP BY (CONVERT (DATE, mrSUBMITDATE))
ORDER BY mrSUBMITDATE