2014-12-24 140 views
1

我有一個查詢:獲取查詢結果的金額範圍內同一查詢

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

也許只是做工會,並選擇一個更排它概括這一切? – buhtla

回答

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)) 
+0

我可能會改變這裏唯一的事情(這是巨大nitpicky)是使用UNION ALL與UNION(例如http://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all)。但除此之外,這將工作,我可能會更喜歡它與WITH ROLLUP(雖然以後是一個有效的選項以及) – evanv

+0

你是對的,UNION ALL更有效,但在這種情況下不相關,因爲數據量非常大小(共7排)。不過謝謝你的提示。 – buhtla

+0

這是一個超級挑剔的細節。對此沒有任何爭議。儘管這個細節太細膩,我仍然贊同你的迴應。 – evanv

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)) 
+0

我可能在這裏改變的唯一一件事(這是巨大的nitpicky)是使用UNION ALL vs UNION(例如http://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and -union-ALL)。但除此之外,這將工作,我可能會更喜歡它與WITH ROLLUP(雖然後者是一個有效的選項以及) – evanv

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 

後來的選項將不輸出「ALL」行,但它是短了很多......此外,您不能使用ORDER BY和WITH ROLLUP在一起,所以知道,提前爲好。

在這兩者之間,我可能會使用第一個選項....這是同由誰回答,但我會使用UNION ALL而不是UNION的其他人提供的兩個。這是一個挑剔的細節,但如果你不需要它們,爲什麼還要重複檢查。欲瞭解更多關於UNION VS UNION ALL,請上網:What is the difference between UNION and UNION ALL?