SQL Server分组按周查询

时间:2014-03-12 15:25:47

标签: sql sql-server

请参阅下面的查询和结果

(SELECT count(*) as Count, p.regionid, a.territory,
CASE WHEN DATEPART(day,p.prodate) < 8 THEN '1' 
  ELSE CASE WHEN DATEPART(day,p.prodate) < 15 then '2' 
    ELSE CASE WHEN  DATEPART(day,p.prodate) < 22 then '3' 
      ELSE CASE WHEN  DATEPART(day,p.prodate) < 29 then '4'     
       ELSE '5'   
      END
    END
  END
END as WeekNumber 
from pros p 
inner join
GTS_Account a 
on a.locationGTSNo = p.BillToCustNo
  where a.account like '%abc%' and
  p.prodate between '2014-02-01' and '2014-02-28' 
group by p.ProDate,p.regionid,a.territory)

结果

╔═══════╦══════════╦═══════════╦════════════╗
║ Count ║ regionid ║ territory ║ WeekNumber ║
╠═══════╬══════════╬═══════════╬════════════╣
║     3 ║ LTL      ║         2 ║          1 ║
║    23 ║ RDFS     ║         2 ║          1 ║
║    10 ║ VOLUME   ║         2 ║          1 ║
║    22 ║ RDFS     ║         2 ║          1 ║
║    10 ║ VOLUME   ║         2 ║          1 ║
║     1 ║ EXP      ║         2 ║          2 ║
║     3 ║ LTL      ║         2 ║          2 ║
║    25 ║ RDFS     ║         2 ║          2 ║
║    11 ║ VOLUME   ║         2 ║          2 ║
║     1 ║ BRK      ║         2 ║          2 ║
║     6 ║ VOLUME   ║         2 ║          2 ║
║     1 ║ BRK      ║         2 ║          3 ║
║     4 ║ EXP      ║         2 ║          3 ║
║     1 ║ LTL      ║         2 ║          3 ║
║    20 ║ RDFS     ║         2 ║          3 ║
║     4 ║ EXP      ║         2 ║          4 ║
║     4 ║ LTL      ║         2 ║          4 ║
║    30 ║ RDFS     ║         2 ║          4 ║
║    15 ║ VOLUME   ║         2 ║          4 ║
╚═══════╩══════════╩═══════════╩════════════╝

这显示了本周每一天的数据,我只想让它显示每个地区/每周不是每天的总数。感谢

14年3月13日 我想显示此数据的月份和年份列,请在下面的查询中,每个地区/周应该只显示一行。

SELECT convert(nvarchar,DATEPART(YYYY,p.prodate))  + ' ' +
CASE WHEN DATEPART(month,p.prodate) = 1 THEN 'JAN' 
    WHEN DATEPART(month,p.prodate) = 2 THEN 'FEB' 
    WHEN DATEPART(month,p.prodate) = 3 THEN 'MAR' 
    WHEN DATEPART(month,p.prodate) = 4 THEN 'APR' 
    WHEN DATEPART(month,p.prodate) = 5 THEN 'MAY' 
    WHEN DATEPART(month,p.prodate) = 6 THEN 'JUN' 
    WHEN DATEPART(month,p.prodate) = 7 THEN 'JUL' 
    WHEN DATEPART(month,p.prodate) = 8 THEN 'AUG' 
    WHEN DATEPART(month,p.prodate) = 9 THEN 'SEP' 
    WHEN DATEPART(month,p.prodate) = 10 THEN 'OCT' 
    WHEN DATEPART(month,p.prodate) = 11 THEN 'NOV' 
    WHEN DATEPART(month,p.prodate) = 12 THEN 'DEC' 
    ELSE '6'
    END AS MONTHnAME,
count(*) as Count, p.regionid, 
CASE WHEN DATEPART(day,p.prodate) < 8 THEN '1' --+ ' ' +convert(nvarchar,DATEPART(mm,p.prodate))
     WHEN DATEPART(day,p.prodate) < 15 then '2' --+ ' ' +convert(nvarchar,DATEPART(mm,p.prodate))
    WHEN  DATEPART(day,p.prodate) < 22 then '3' --+ ' ' +convert(nvarchar,DATEPART(mm,p.prodate))
    WHEN  DATEPART(day,p.prodate) < 29 then '4'  --+ ' ' +convert(nvarchar,DATEPART(mm,p.prodate))   
    ELSE '5' + ' ' +convert(nvarchar,DATEPART(mm,p.prodate)) 
END as WeekNumber,a.territory,'rrts' from pros p inner join GTS_Account a on a.locationGTSNo = p.BillToCustNo
  where a.account like '%rrts%' and
  p.prodate between '2014-02-01' and '2014-02-28' group by 
  CASE 
            WHEN Datepart(day, p.prodate) < 8 THEN '1' 
            WHEN Datepart(day, p.prodate) < 15 THEN '2' 
            WHEN Datepart(day, p.prodate) < 22 THEN '3' 
            WHEN Datepart(day, p.prodate) < 29 THEN '4' 
            ELSE '5' 
          END
  ,p.prodate,a.territory, p.regionid
  order by p.regionid, weeknumber

MONTHnAME计数regionid WeekNumber地区(无列名) 2014 FEB 1 BRK 1 0002 rrts 2014 FEB 1 BRK 2 0002 rrts 2014 FEB 1 BRK 2 0002 rrts 2014 FEB 1 BRK 3 0002 rrts 2014 FEB 2 BRK 4 0002 rrts 2014 FEB 1 BRK 4 0002 rrts 2014 FEB 1 DED 3 0002 rrts 2014 FEB 3 EXP 1 0002 rrts 2014 FEB 1 EXP 1 0002 rrts 2014 FEB 2 EXP 1 0002 rrts 2014 FEB 3 EXP 1 0002 rrts 2014 FEB 2 EXP 1 0002 rrts 2014 FEB 1 EXP 2 0002 rrts 2014 FEB 1 EXP 2 0002 rrts 2014 FEB 2 EXP 2 0002 rrts 2014 FEB 1 EXP 2 0002 rrts 2014 FEB 4 EXP 3 0002 rrts 2014 FEB 1 EXP 3 0002 rrts 2014 FEB 6 EXP 3 0002 rrts 2014 FEB 3 EXP 3 0002 rrts 2014 FEB 7 EXP 3 0002 rrts 2014 FEB 1 EXP 4 0002 rrts 2014 FEB 4 EXP 4 0002 rrts 2014 FEB 2 EXP 4 0002 rrts 2014 FEB 3 EXP 4 0002 rrts

1 个答案:

答案 0 :(得分:3)

您只需按照您选择的相同内容进行分组,而不是p.prodate

此外,您不需要多个WHENs

的嵌套案例陈述
SELECT Count(*) AS Count, 
       p.regionid, 
       a.territory, 
       CASE 
         WHEN Datepart(day, p.prodate) < 8 THEN '1' 
         WHEN Datepart(day, p.prodate) < 15 THEN '2' 
         WHEN Datepart(day, p.prodate) < 22 THEN '3' 
         WHEN Datepart(day, p.prodate) < 29 THEN '4' 
         ELSE '5' 
       END      AS WeekNumber 
FROM   pros p 
       INNER JOIN gts_account a 
               ON a.locationgtsno = p.billtocustno 
WHERE  a.account LIKE '%abc%' 
       AND p.prodate BETWEEN '2014-02-01' AND '2014-02-28' 
GROUP  BY CASE 
            WHEN Datepart(day, p.prodate) < 8 THEN '1' 
            WHEN Datepart(day, p.prodate) < 15 THEN '2' 
            WHEN Datepart(day, p.prodate) < 22 THEN '3' 
            WHEN Datepart(day, p.prodate) < 29 THEN '4' 
            ELSE '5' 
          END, 
          p.regionid, 
          a.territory 

如果您的案例陈述足够令人讨厌,您也可以使用with子句使其更清晰

WITH cte 
     AS (SELECT 
                p.regionid, 
                a.territory, 
                CASE 
                  WHEN Datepart(day, p.prodate) < 8 THEN '1' 
                  WHEN Datepart(day, p.prodate) < 15 THEN '2' 
                  WHEN Datepart(day, p.prodate) < 22 THEN '3' 
                  WHEN Datepart(day, p.prodate) < 29 THEN '4' 
                  ELSE '5' 
                END AS WeekNumber 
         FROM   pros p 
                INNER JOIN gts_account a 
                        ON a.locationgtsno = p.billtocustno 
         WHERE  a.account LIKE '%abc%' 
                AND p.prodate BETWEEN '2014-02-01' AND '2014-02-28') 
SELECT Count(*) As Count,  
       regionid, 
       territory, 
       weeknumber 
FROM   cte 
GROUP  BY weeknumber, 
          regionid, 
          territory