用于将各个区域的行信息放在同一结果集行的列中的SQL查询。

时间:2016-08-18 12:37:06

标签: sql jdbc

我正在使用以下表格,如下所示: 每天是否有各种地区的信息。

>>> s = u'\xd0\xb2\xd0\xb8\xd0\xb4\xd0\xb5\xd0\xbe \xd0\xbf\xd0\xb0\xd0\xbb\xd0\xb5\xd1\x86 \xd0\xb2\xd0\xb2\xd0\xb5\xd1\x80\xd1\x85'

>>> print s.encode('latin1')
видео палец вверх

我的任务是将输出结果集设置为下面显示的格式,该格式将根据WForecastDate和timeStamp的条件为同一行中的各个区域提供临时,风速和湿度信息。这里Temp100,Temp101等是这个地区的温度。

Daily Weather Information   
  WForecastDate    | TimeStamp   | RegionId  | Temp  | WindSpeed   | Humidity
----------------------------------------------------------------------------
  2016-08-16      | 1            | 100       |  23   | 123         |23
  2016-08-16      | 2            | 100       |  24   | 123         |24
  2016-08-16      | 3            | 100       |  24   | 123         |22
  2016-08-16      | 4            | 100       |  23   | 123         |21
  2016-08-16      | 5            | 100       |  25   | 123         |27
  2016-08-16      | 6            | 100       |  24   | 123         |26
  2016-08-16      | 7            | 100       |  22   | 123         |27
  2016-08-16      | 8            | 100       |  21   | 123         |26
  2016-08-16      | 9            | 100       |  20   | 123         |23
  2016-08-16      | 1            | 101       |  23   | 123         |22
  2016-08-16      | 2            | 101       |  22   | 123         |21
  2016-08-16      | 3            | 101       |  21   | 123         |27
  2016-08-16      | 4            | 101       |  22   | 123         |25
  2016-08-16      | 5            | 101       |  21   | 123         |23             
  2016-08-16      | 6            | 101       |  21   | 123         |24
  2016-08-16      | 1            | 102       |  23   | 123         |22
  2016-08-16      | 2            | 102       |  22   | 123         |21
  2016-08-16      | 3            | 102       |  21   | 123         |27
  2016-08-16      | 4            | 102       |  22   | 123         |25
  2016-08-16      | 5            | 102       |  21   | 123         |23             
  2016-08-16      | 6            | 102       |  21   | 123         |24
     .............................................................
     .............................................................

请帮助并告诉我应该编写哪个查询以便完成此任务。

1 个答案:

答案 0 :(得分:0)

SQL中的一种方法是使用条件聚合:

select wforecastdate, timestamp, 
       max(case when regionid = 100 then temp end) as temp_100,
       max(case when regionid = 101 then temp end) as temp_101,
       max(case when regionid = 102 then temp end) as temp_102,
       max(case when regionid = 100 then humidity end) as humidity_100,
       max(case when regionid = 101 then humidity end) as humidity_101,
       max(case when regionid = 102 then humidity end) as humidity_102,
       max(case when regionid = 100 then windspeed end) as windspeed_100,
       max(case when regionid = 101 then windspeed end) as windspeed_101,
       max(case when regionid = 102 then windspeed end) as windspeed_102
from dailyweatherinformation dwi
group by wforecastdate, timestamp
order by wforecastdate