我有两张桌子:
CREATE TABLE [ObIndex](
[idx] [int] IDENTITY(1,1) NOT NULL,
[dtg] [smalldatetime] NOT NULL,
[site] [varchar](6) NOT NULL,
[name] [varchar](32) NOT NULL,
[lat] [float] NOT NULL,
[lon] [float] NOT NULL,
[el] [int] NOT NULL,
CONSTRAINT [PK_SurfaceObIndex] PRIMARY KEY CLUSTERED
(
[icao] ASC,
[dtg] ASC
)
和
CREATE TABLE [ObData](
[idx] [int] NOT NULL,
[label] [varchar](32) NOT NULL,
[value] [varchar](128) NOT NULL,
CONSTRAINT [PK_SurfaceObData] PRIMARY KEY CLUSTERED
(
[idx] ASC,
[label] ASC
)
对于ObIndex中的每条记录,ObData中将存在一条或多条记录,每条记录包含观察中的一个参数(请注意,工作站可能有不同的工具):
ObIndex (metadata)
1,"2017-11-15 12:00:00","C104","AWS 104",-77.100,-167.100,33
2,"2017-11-15 12:00:00","C105","AWS 105",-77.200,-167.200,55
etc.
ObData (instrument data)
1,"Temperature", -10
1,"DewPoint", -20
1,"WindDirection", 123
1,"WindSpeed", 45
1,"Humidity",66
2,"Temperature", -10
2,"DewPoint", -20
2,"WindDirection", 123
2,"WindSpeed", 45
2,"Pressure",998
etc.
还有第三个当前电台位置表与此问题无关。
如果可能,我想使用单个查询从ObData表中提取每个ObIndex记录的所有值,并转换'标签'列名称的值。此查询适用于单个观察(或ObIndex中的记录):
select
Min(Case label When 'Temperature' Then value End) Temperature,
Min(Case label When 'DewPoint' Then value End) DewPoint
Min(Case label When 'WindSpeed' Then value End) WindSpeed,
Min(Case label When 'WindDirection' Then value End) WindDirection,
From SurfaceObData Where SurfaceObData.idx = 138586
有人可以帮我完成查询以从所有观察中返回数据吗? 结果理想情况如下:
idx,site,dtg,WindDirection,WindSpeed,Temperature,Humidity,Dewpoint,Pressure
1,"C104",2017-11-15 12:00:00,123,45,-10,66,-20,
2,"C106",2017-11-15 12:00:00,123,45,-10,,-20,998
(不知道怎么把它放在表格格式......第一篇文章。)
提前致谢!
答案 0 :(得分:0)
只需按SurfaceObData.idx
列
select SurfaceObData.idx,
Min(Case label When 'Temperature' Then value End) Temperature,
Min(Case label When 'DewPoint' Then value End) DewPoint
Min(Case label When 'WindSpeed' Then value End) WindSpeed,
Min(Case label When 'WindDirection' Then value End) WindDirection,
From SurfaceObData
Group By SurfaceObData.idx
答案 1 :(得分:0)
感谢Squirrel的帮助,我找到了解决方案:
select i.idx,i.site,i.dtg,
Min(Case d.label When 'WindDirection' Then d.value End) WindDirection,
Min(Case d.label When 'WindSpeed' Then d.value End) WindSpeedKnots,
Min(Case d.label When 'Temperature' Then d.value End) Temperature,
Min(Case d.label When 'Humidity' Then d.value End) Humidity,
Min(Case d.label When 'DewPoint' Then d.value End) DewPoint,
Min(Case d.label When 'Pressure' Then d.value End) Pressure
from SurfaceObData as d, SurfaceObIndex as i
where d.idx = i.idx
group by i.idx,i.site,i.dtg