如何在同一个sql查询中求和和小计

时间:2011-07-07 16:47:26

标签: sql-server tsql

select problemrecordedbytitle,problemstatus ,
       count(problemstatus) from problemtable 
where problemrecordedccyy >='2011'
and problemrecordedbytitle like 'LPM%'
group by problemrecordedbytitle, problemstatus
order by problemrecordedbytitle asc

LPM-AMS-EDW Open    1
LPM-AMS-EDW WIP     1
LPM-AMS-EOM Closed  4

这个sql为我提供了很好的状态摘要。但是我今天有了新的要求。

我想总结所有ie count(problemstatus)和count(problemstatus<>摘要)。 它应该像

LPM-AMS-EDW    NotClosed    2
LPM-AMS-EDW    Total        6

我不知道我怎么能在TSQL中做到这一点

3 个答案:

答案 0 :(得分:2)

查看汇总:

http://msdn.microsoft.com/en-us/library/ms177673.aspx

或2005年:http://msdn.microsoft.com/en-us/library/ms177673%28v=SQL.90%29.aspx

 select problemrecordedbytitle, problemstatus, count(problemstatus) from problemtable 
 where problemrecordedccyy >='2011'
 and problemrecordedbytitle like 'LPM%'
 group by rollup(problemrecordedbytitle, problemstatus)
 order by problemrecordedbytitle asc

如果要对不同的状态进行分组,请执行以下操作:

 select problemrecordedbytitle, 
 case problemstatus when 4 then 'Closed' ELSE 'NotClosed' END as Status,
 Count(1)
 where problemrecordedccyy >='2011'
 and problemrecordedbytitle like 'LPM%'
 group by rollup(problemrecordedbytitle, case problemstatus when 4 then 'Closed' ELSE 'NotClosed' END)

对于2005年,我认为语法类似于

 .....
 GROUP BY problemrecordedbytitle, case problemstatus when 4 then 'Closed' ELSE 'NotClosed' END
 WITH ROLLUP

可能不完全如上,但你绝对可以用2005年

来做到这一点

答案 1 :(得分:0)

你真的应该注意不要对具有不同值的字段进行分组。 这将为您提供所需的结果。

;WITH a as(
SELECT problemrecordedbytitle,problemstatus from problemtable  
WHERE problemrecordedccyy >='2011' 
and problemrecordedbytitle like 'LPM%' 
)
SELECT problemrecordedbytitle, 'NotClosed', COUNT(*) FROM a WHERE problemstatus <> 'Closed' 
GROUP BY problemrecordedbytitle
UNION ALL
SELECT MIN(problemrecordedbytitle), 'Total', COUNT(*) FROM a

我会重写它以防止这些群体问题:

declare @problemtable table (problemrecordedbytitle varchar(20), problemstatus varchar(10))

INSERT @problemtable values('LPM-AMS-EDW', 'Open')
INSERT @problemtable values('LPM-AMS-EDW','WIP')
INSERT @problemtable values('LPM-AMS-EOM','Closed')
INSERT @problemtable values('LPM-AMS-EOM','Closed')
INSERT @problemtable values('LPM-AMS-EOM','Closed')
INSERT @problemtable values('LPM-AMS-EOM','Closed')

;WITH a as(
SELECT left(problemrecordedbytitle, 3) problemrecordedbytitle,problemstatus 
FROM @problemtable -- replace this tablename for your script 
-- You need these lines for your script
-- WHERE problemrecordedccyy >='2011' 
-- and problemrecordedbytitle like 'LPM%' 
)
SELECT problemrecordedbytitle, 'NotClosed' [status], COUNT(*) count FROM a WHERE problemstatus <> 'Closed' 
GROUP BY problemrecordedbytitle
UNION ALL
SELECT MIN(problemrecordedbytitle), 'Total', COUNT(*) FROM a

结果:

problemrecordedbytitle status    count
---------------------- --------- -----------
LPM                    NotClosed 2
LPM                    Total     6

答案 2 :(得分:0)

select COUNT(problemrecordedbytitle) AS Problem_Rec_Title, MAX(problemrecordedbytitle) as Prob_Rec_Title, problemstatus
       where problemrecordedccyy >='2011'
and problemrecordedbytitle like 'LPM%'
group by problemrecordedbytitle, problemstatus
order by problemrecordedbytitle asc

使用COUNTMAX进行游戏 - 这就是我解决类似问题的方法。

COUNT计数,然后MAX为您提供最大数量(排序的子总数)。然后,您可以按字段进行分组和排序。

我希望这会有所帮助。