分组并计算月份数据

时间:2014-05-28 14:38:22

标签: sql-server

我有t_street_name,t_vrm,t_date_time_issued,ticket_no作为表中的列,我想知道在所有月份的所有街道上已经发出了多少票,我很想知道是否有特定的vrm特定票据任何一个月的街道。这是我的查询,但逐个命令无效。

SELECT t_vrm,t_streeet_name,JAN, FEB, MARCH,  
 APRIL, MAY, JUNE,JULY, AUG, SEPT, OCT, NOV, DECEMBER,  
JAN+ FEB+ MARCH+  
 APRIL+ MAY+ JUNE+JULY+ AUG+ SEPT+ OCT+ NOV+ DECEMBER
    AS row_total  
  FROM  
  ( SELECT t_street_name,t_zone_name,t_vrm, SUM(t1) AS JAN, SUM(t2) AS FEB, SUM(t3) AS MARCH, 
  SUM(t4) AS APRIL, SUM(t5) AS MAY, SUM(t6) AS JUNE, 
  SUM(t7) AS JULY, SUM(t8) AS AUG, SUM(t9) AS SEPT,  
  SUM(t10) AS OCT, SUM(t11) AS NOV, SUM(t12) AS DECEMBER
   FROM  
  (  
  SELECT b.*,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '01' AND '01' THEN 1 ELSE 0 END AS t1, 
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '02' AND '02' THEN 1 ELSE 0 END AS t2,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '03' AND '03' THEN 1  ELSE 0 END AS t3,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '04' AND '04' THEN 1  ELSE 0 END AS t4,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '05' AND '05' THEN 1  ELSE 0 END AS t5, 
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '06' AND '06' THEN 1  ELSE 0 END AS t6, 
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '07' AND '07' THEN 1  ELSE 0 END AS t7,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '08' AND '08' THEN 1  ELSE 0 END AS t8,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '09' AND '09' THEN 1  ELSE 0 END AS t9,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '10' AND '10' THEN 1  ELSE 0 END AS t10,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '11' AND '11' THEN 1  ELSE 0 END AS t11,  
  CASE WHEN  DATEPART(mm,t_date_time_issued) BETWEEN '12' AND '12' THEN 1  ELSE 0 END AS t12  
  FROM  
   (  
    SELECT  t_street_name,t_zone_name,t_vrm,t_date_time_issued FROM   tickets   


 )b  
)d    
 group by t_vrm,t_street_name,t_zone_name  )qry1

0 个答案:

没有答案