在case sql语句中使用sum和count

时间:2016-06-01 09:18:42

标签: sql sql-server count sum case

我想对表中的每一行进行计数,但是我收到了这个错误:

  

无法对包含的表达式执行聚合函数   聚合或子查询。

   SELECT     TOP (100) PERCENT ISNULL(MAX(Id), 0) AS ID, 
           MONTH(p.datep) AS nMonth, 
           DATENAME(mm, p.dateP) AS month, 
           YEAR(p.dateP) AS year, c.EId, e.SId,
           e.nE, s.libelle,
           SUM(p.a) AS a, 
           SUM(p.a - p.b) AS c, 
           SUM(p.b) AS b, 
           SUM(p.dureePM + p.dureeAM) AS dX,
           SUM(CASE WHEN p.dureePm != 0 THEN 1 ELSE 0 END) AS nbrPm, 
           SUM(CASE WHEN p.dureeAm != 0 THEN 1 ELSE 0 END) AS nbrAm, 
           COUNT(CASE WHEN p.nbX > 2 THEN  SUM(p.nbX-2)  ELSE 0 END) AS nbrXSupp, 
           COUNT(CASE WHEN p.nbY > 2 THEN  SUM(p.nbY-2)  ELSE 0 END) AS nbrYSupp

FROM       dbo.table AS p 
INNER JOIN
           dbo.tableC AS c ON p.cId = c.CId 
INNER JOIN
           dbo.tableE AS e ON c.EId = e.EId 
INNER JOIN
            dbo.tableS AS s ON e.SId = s.SId

GROUP BY MONTH(p.dateP), DATENAME(mm, p.dateP), YEAR(p.dateP), 
          c.EId, e.SId, e.nE, s.libelle

4 个答案:

答案 0 :(得分:0)

最简单的方法是使用CTE。 希望这个帮助

CREATE TABLE ## tableA(id INT,x INT,y INT) 插入## tableA VALUES (1,5,3), (2,7,2), (3,2,5)

;与SUMS AS( SELECT id,
 情况是x> 2那么(x-2)ELSE 0 END作为number1,  例如,y> 2那么(y-2)ELSE 0结束为number2

FROM ## tableA )

选择 * 来自SUMS

答案 1 :(得分:0)

Select SUM(T1Count.TotalRec) T1Counts, SUM(T2Count.TotalRec) T2Counts 
From Table1 T01 
Outer Apply (Select COUNT(T01_Id) TotalRec From Table1 T1 Where T1.T01_Id=T01.T01_Id And T01_FieldToCheck > 1) T1Count
Outer Apply (Select COUNT(T01_Id) TotalRec From Table1 T1 Where T1.T01_Id=T01.T01_Id And T01_FieldToCheck =< 1) T2Count

我认为这可以帮助您解决一些问题。这样,您可以对数据进行不同的条件摘要。

如果有任何问题,请回来。

答案 2 :(得分:0)

以下查询将提供所需的输出: -

SELECT id,
CASE WHEN min(x)> 2  THEN  (min(x)-2) ELSE 0  END AS number1, 
CASE WHEN min(y) > 2 THEN (min(y)-2)  ELSE 0 END AS number2
FROM tableA
group by id

输出: -

id  number1 number2
 1     3    1
 2     5    0
 3     0    3

答案 3 :(得分:0)

正如我在评论中已经提到的,SQl服务器不支持另一个内部的聚合函数。您可以通过两个步骤打破查询来实现目的。我已将您的查询保存在cte中,并进一步查询它以计算您需要的列。

 with cte as
(
 SELECT     TOP (100) PERCENT ISNULL(MAX(Id), 0) AS ID, 
           MONTH(p.datep) AS nMonth, 
           DATENAME(mm, p.dateP) AS month, 
           YEAR(p.dateP) AS year, c.EId, e.SId,
           e.nE, s.libelle,
           SUM(p.a) AS a, 
           SUM(p.a - p.b) AS c, 
           SUM(p.b) AS b, 
           SUM(p.dureePM + p.dureeAM) AS dX,
           SUM(CASE WHEN p.dureePm != 0 THEN 1 ELSE 0 END) AS nbrPm, 
           SUM(CASE WHEN p.dureeAm != 0 THEN 1 ELSE 0 END) AS nbrAm, 
           CASE WHEN p.nbX > 2 THEN  SUM(p.nbX-2)  ELSE 0 END AS nbrXSupp, --removed the COUNT
           CASE WHEN p.nbY > 2 THEN  SUM(p.nbY-2)  ELSE 0 END AS nbrYSupp

FROM       dbo.table AS p 
INNER JOIN
           dbo.tableC AS c ON p.cId = c.CId 
INNER JOIN
           dbo.tableE AS e ON c.EId = e.EId 
INNER JOIN
            dbo.tableS AS s ON e.SId = s.SId

GROUP BY MONTH(p.dateP), DATENAME(mm, p.dateP), YEAR(p.dateP), 
          c.EId, e.SId, e.nE, s.libelle
)
select cte.*,count(nbrXSupp) as Cn_nbrXSupp,count(nbrYSupp) as Cn_nbrYSupp
from cte
group by nMonth,month,year,EId,SId, nE,libelle

希望它有所帮助。显然我没有运行并测试它。如果查询中有任何列绑定问题,请将它们排序。