如何合并日期列?

时间:2015-02-25 00:14:56

标签: sql

我有一个问题:

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>>

我希望能够进行整合,以便每天显示总计,而不是每天显示多行。有人可以帮助我吗?

非常感谢。

1 个答案:

答案 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