SQL Group依赖于case语句?

时间:2015-08-04 19:15:14

标签: sql database

在SQL中使用日期范围时,还需要对其进行分组。在这种情况下,我按周分组,但如果值为less than 10,我希望它合并在一个'Others'行中。

如何按周分组,然后按'Others'分组?我只需要一个'Others'行,总计小于10

enter image description here

SELECT SUM(DISTINCT SOTRAN.ACTQTY) AS value
   ,CASE 
        WHEN SUM(DISTINCT SOTRAN.ACTQTY) > 10
    THEN 
     'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)
   ELSE 'Others'
   END AS label
FROM SOTRAN AS SOTRAN
WHERE (
        SOTRAN.tdate >= '1/1/2015 12:00:00 AM'
        AND SOTRAN.tdate <= '12/31/2015 11:59:59 PM'
        )
GROUP BY 
'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)

3 个答案:

答案 0 :(得分:0)

select SUM(distinct SOTRAN.ACTQTY) AS value, 
case  when SUM(distinct SOTRAN.ACTQTY) > 10 then 'Week of ' +  CONVERT(VARCHAR(10),dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0),1) else 'Others' end AS label
from SOTRAN AS SOTRAN
WHERE ( SOTRAN.tdate>='1/1/2015 12:00:00 AM' AND SOTRAN.tdate<='12/31/2015 11:59:59 PM' )  
GROUP BY 'Week of ' + CONVERT(VARCHAR(10),dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0),1),label

答案 1 :(得分:0)

您的整个案例陈述应该在group by子句中(减去“AS标签”)。

我之前没有共享过一个sqlfiddle,所以我希望这可行,但请在此处查看:

http://sqlfiddle.com/#!4/fabb86/6

否则试试这个:

tf

更新:

select SUM(distinct SOTRAN.ACTQTY) AS value, 
       case when SUM(distinct SOTRAN.ACTQTY) > 10 then 
         'Week of ' + CONVERT(VARCHAR(10),dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0),1) else 
          'Others' end AS label
  from SOTRAN AS SOTRAN
 WHERE (SOTRAN.tdate>='1/1/2015 12:00:00 AM' 
   AND SOTRAN.tdate<='12/31/2015 11:59:59 PM' )  
 GROUP BY case when SUM(distinct SOTRAN.ACTQTY) > 10 then 
         'Week of ' + CONVERT(VARCHAR(10),dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0),1) else 
          'Others' end

答案 2 :(得分:0)

你可以写Outer Select来获得你想要的东西

SELECT SUM(Value),label
FROM
    (
       SELECT SUM(DISTINCT SOTRAN.ACTQTY) AS value
           ,CASE 
             WHEN SUM(DISTINCT SOTRAN.ACTQTY) > 10
                   THEN  'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)
             ELSE 'Others'
            END AS label
       FROM SOTRAN AS SOTRAN
       WHERE (
               SOTRAN.tdate >= '1/1/2015 12:00:00 AM'
               AND SOTRAN.tdate <= '12/31/2015 11:59:59 PM'
            )
       GROUP BY 
             'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)
    ) as innerData
 GROUP BY label

或者你可以使用CTE,如下所示,

;WITH innerData AS
(
    SELECT SUM(DISTINCT SOTRAN.ACTQTY) AS value
           ,CASE 
             WHEN SUM(DISTINCT SOTRAN.ACTQTY) > 10
                   THEN  'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)
             ELSE 'Others'
            END AS label
       FROM SOTRAN AS SOTRAN
       WHERE (
               SOTRAN.tdate >= '1/1/2015 12:00:00 AM'
               AND SOTRAN.tdate <= '12/31/2015 11:59:59 PM'
            )
       GROUP BY 
             'Week of ' + CONVERT(VARCHAR(10), dateadd(wk, datediff(wk, 0, SOTRAN.tdate), 0), 1)
)

然后只需使用groupby

选择
 SELECT SUM(Value),label
 FROM  innerData
 GROUP BY label