获取每周平均天气数据

时间:2015-11-03 16:34:57

标签: sql-server tsql

需要弄清楚我的SELECT语句中有多天。 如果我只选择2列(Zip_code和Week),它就可以工作。

Zip_Code    Week
501 2014-12-29 to 2015-01-04
501 2015-01-05 to 2015-01-11
501 2015-01-12 to 2015-01-18
501 2015-01-19 to 2015-01-25
501 2015-01-26 to 2015-02-01
501 2015-02-02 to 2015-02-08
501 2015-02-09 to 2015-02-15
501 2015-02-16 to 2015-02-22
501 2015-02-23 to 2015-03-01

代码:

SELECT  DISTINCT Zip_Code =  postal_code       --RIGHT(REPLICATE('0', 5) + CAST(postal_code AS VARCHAR(5)), 5)
      ,[Week]               = CASE DATEPART(dw, Local_date) 
   WHEN 1 THEN CONVERT(char(10), DATEADD(DD, -6, Local_date),126) +  ' to ' + CONVERT(char(10), Local_date,126)
   WHEN 2 THEN CONVERT(char(10), Local_date,126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 6, Local_date),126)
   WHEN 3 THEN CONVERT(char(10), DATEADD(DD, -1, Local_date),126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 5, Local_date),126)
   WHEN 4 THEN CONVERT(char(10), DATEADD(DD, -2, Local_date),126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 4, Local_date),126)
   WHEN 5 THEN CONVERT(char(10), DATEADD(DD, -3, Local_date),126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 3, Local_date),126)
   WHEN 6 THEN CONVERT(char(10), DATEADD(DD, -4, Local_date),126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 2, Local_date),126)
   WHEN 7 THEN CONVERT(char(10), DATEADD(DD, -5, Local_date),126) +  ' to ' + CONVERT(char(10), DATEADD(DD, 1, Local_date),126)
   ELSE 'UNK'
                               END
     ,[Week_Avg_Temp_F]     = ROUND(AVG(CAST(tempAvg_F AS FLOAT)),2)
      ,[Week_Max_Temp_F]        = ROUND(AVG(CAST(tempMax_F AS FLOAT)),2)
      ,[Week_Min_Temp_F]        = ROUND(AVG(CAST(tempMin_F AS FLOAT)),2)
      ,[Hum_Avg_%]              = ROUND(AVG(CAST([relHumAvg_%] AS FLOAT)),2)
      ,[Hum_Min_%]              = ROUND(AVG(CAST([relHumMin_%] AS FLOAT)),2)
      ,[precip_Inches]          = ROUND(AVG(CAST(precip_In AS FLOAT)),2)
      ,[snowfall_Inches]        = ROUND(AVG(CAST(snowfall_In AS FLOAT)),2)
      ,[Avg_Wind_Speed_MPH]     = ROUND(AVG(CAST(windSpdAvg_MPH AS FLOAT)),2) 
FROM [Weather_Data].[dbo].[Weather_Data_2015]
GROUP BY postal_code, Local_date
ORDER BY zip_code, Week 

如果我选择所有列,我会获得相同邮政编码的多个日期条目。

Zip_Code    Week    Week_Avg_Temp_F Week_Max_Temp_F Week_Min_Temp_F Hum_Avg_%   Hum_Min_%   precip_Inches   snowfall_Inches Avg_Wind_Speed_MPH
501 2014-12-29 to 2015-01-04    47  50  41  98  77  0   0   7
501 2014-12-29 to 2015-01-04    36  46  27  82  59  1   0   7
501 2014-12-29 to 2015-01-04    36  42  31  54  42  0   0   7
501 2014-12-29 to 2015-01-04    33  38  25  48  38  0   0   10
501 2015-01-05 to 2015-01-11    18  24  9   58  37  0   0   13
501 2015-01-05 to 2015-01-11    26  33  18  62  49  0   1   10
501 2015-01-05 to 2015-01-11    14  20  5   55  42  0   0   8
501 2015-01-05 to 2015-01-11    16  22  11  55  44  0   0   6
501 2015-01-05 to 2015-01-11    24  34  11  61  38  0   0   5
501 2015-01-05 to 2015-01-11    19  22  17  70  44  0   0   5
501 2015-01-05 to 2015-01-11    34  47  20  48  34  0   0   13
501 2015-01-12 to 2015-01-18    25  29  18  82  74  0   0   7
501 2015-01-12 to 2015-01-18    33  35  30  87  65  0   0   3
501 2015-01-12 to 2015-01-18    35  45  17  96  87  1   0   5
501 2015-01-12 to 2015-01-18    25  32  20  78  58  0   0   4
501 2015-01-12 to 2015-01-18    21  27  14  62  38  0   0   4
501 2015-01-12 to 2015-01-18    26  34  17  68  50  0   0   7
501 2015-01-12 to 2015-01-18    28  39  19  58  40  0   0   8

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你需要将你的组移动到外层,因为现在它引用local_date,所以它仍然按天分割记录..我没有测试过这个但它应该可以工作

        SELECT Zip_Code, [WeekFrom], [WeekTo],
       [Week_Avg_Temp_F]        = ROUND(AVG(CAST(tempAvg_F AS FLOAT)),2)
      ,[Week_Max_Temp_F]        = ROUND(AVG(CAST(tempMax_F AS FLOAT)),2)
      ,[Week_Min_Temp_F]        = ROUND(AVG(CAST(tempMin_F AS FLOAT)),2)
      ,[Hum_Avg_%]              = ROUND(AVG(CAST([relHumAvg_%] AS FLOAT)),2)
      ,[Hum_Min_%]              = ROUND(AVG(CAST([relHumMin_%] AS FLOAT)),2)
      ,[precip_Inches]          = ROUND(AVG(CAST(precip_In AS FLOAT)),2)
      ,[snowfall_Inches]        = ROUND(AVG(CAST(snowfall_In AS FLOAT)),2)
      ,[Avg_Wind_Speed_MPH]     = ROUND(AVG(CAST(windSpdAvg_MPH AS FLOAT)),2)
FROM
(SELECT  DISTINCT Zip_Code =  postal_code       --RIGHT(REPLICATE('0', 5) + CAST(postal_code AS VARCHAR(5)), 5)
      ,[WeekFrom]               = CASE DATEPART(dw, Local_date) 
   WHEN 1 THEN DATEADD(DD, -6, Local_date)
   WHEN 2 THEN Local_date
   WHEN 3 THEN DATEADD(DD, -1, Local_date)
   WHEN 4 THEN DATEADD(DD, -2, Local_date)
   WHEN 5 THEN DATEADD(DD, -3, Local_date)
   WHEN 6 THEN DATEADD(DD, -4, Local_date)
   WHEN 7 THEN DATEADD(DD, -5, Local_date)
   ELSE 'UNK' END
   ,[WeekTo]               = CASE DATEPART(dw, Local_date) 
   WHEN 1 THEN Local_date
   WHEN 2 THEN DATEADD(DD, 6, Local_date)
   WHEN 3 THEN DATEADD(DD, 5, Local_date)
   WHEN 4 THEN DATEADD(DD, 4, Local_date)
   WHEN 5 THEN DATEADD(DD, 3, Local_date)
   WHEN 6 THEN DATEADD(DD, 2, Local_date)
   WHEN 7 THEN DATEADD(DD, 1, Local_date)
   ELSE 'UNK' END
     ,tempAvg_F
      ,tempMax_F
      ,tempMin_F
      ,[relHumAvg_%]
      ,[relHumMin_%] 
      ,precip_In
      ,snowfall_In
      ,windSpdAvg_MPH
FROM [Weather_Data].[dbo].[Weather_Data_2015]) AS WeeklyNumbers
GROUP BY zip_code, [WeekFrom], [WeekTo]
ORDER BY zip_code, [WeekFrom], [WeekTo]