我有一个问题:
SELECT case
when Submission__bTracking = 'Phone' then 'Phone'
when Submission__bTracking = 'Web' then 'Web'
when Submission__bTracking = 'Email' then 'Email
when Submission__bTracking = 'Live__bTech__bSupport' then '@ Live Tech Support
when Submission__bTracking = 'Verbal' then 'Verbal Request'
when Submission__bTracking = 'Fax__b__f__bform' then 'Fax / Form'
End as Sub_Tracking,
COUNT(Submission__bTracking) as tickets FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
GROUP BY Submission__bTracking
产生以下结果:
Sub_Tracking tickets
Email 36
Fax / Form 1
@ Live Tech Support 18
Phone 441
Web 41
如何同时生成同一查询中所有故障单的总和?
如下所示:
Sub_Tracking tickets
Email 36
Fax / Form 1
@ Live Tech Support 18
Phone 441
Web 41
ALL 537
感谢您的帮助!!
答案 0 :(得分:2)
您可以使用UNION,只需在结果集中再添加一行:
YOUR QUERY
UNION ALL
select 'ALL' as Sub_Tracking,
COUNT(Submission__bTracking) as tickets FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
答案 1 :(得分:1)
你可以做到
...previous lines omitted
GROUP BY Submission__bTracking
UNION
SELECT 'ALL' as Sub_Tracking, count(Submission__bTracking) as tickets
FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
答案 2 :(得分:0)
这里有两种选择。一种是将您的初始查询与计算所有票证的另一个查询结合起来,例如
SELECT case
when Submission__bTracking = 'Phone' then 'Phone'
when Submission__bTracking = 'Web' then 'Web'
when Submission__bTracking = 'Email' then 'Email
when Submission__bTracking = 'Live__bTech__bSupport' then '@ Live Tech Support
when Submission__bTracking = 'Verbal' then 'Verbal Request'
when Submission__bTracking = 'Fax__b__f__bform' then 'Fax / Form'
End as Sub_Tracking,
COUNT(Submission__bTracking) as tickets FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
GROUP BY Submission__bTracking
UNION ALL
SELECT "All" AS Sub_Tracking,
COUNT(Submission__bTracking) as tickets FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
另一种选择是使用WITH ROLLUP(例如http://dev.mysql.com/doc/refman/5.6/en/group-by-modifiers.html)
看起来像这样:
SELECT case
when Submission__bTracking = 'Phone' then 'Phone'
when Submission__bTracking = 'Web' then 'Web'
when Submission__bTracking = 'Email' then 'Email
when Submission__bTracking = 'Live__bTech__bSupport' then '@ Live Tech Support
when Submission__bTracking = 'Verbal' then 'Verbal Request'
when Submission__bTracking = 'Fax__b__f__bform' then 'Fax / Form'
End as Sub_Tracking,
COUNT(Submission__bTracking) as tickets FROM dbo.MASTER30
WHERE mrSUBMITDATE >= (CONVERT (date, CURRENT_TIMESTAMP -1))
AND mrSUBMITDATE < (CONVERT (date, CURRENT_TIMESTAMP))
GROUP BY Submission__bTracking WITH ROLLUP
后一个选项不会输出&#34; ALL&#34;但是它要短得多......而且,你不能同时使用ORDER BY和WITH ROLLUP,所以也要提前知道。
在两者之间,我可能会使用第一个选项....这与其他响应的人提供的选项相同,只是我使用UNION ALL而不是UNION。这是一个挑剔的细节,但如果您不需要它们,为什么还要重复检查。有关UNION和UNION ALL的更多信息,请查看:What is the difference between UNION and UNION ALL?)