我见过类似的问题,但无法找到解决这个问题的方法。承担着乱七八糟的桌子,问题应该是什么?非常坦率的。
我有下表。
LDTime Value
2013-07-24 00:00:00.000 14.85
2013-07-24 00:00:00.000 1.339
2013-07-24 00:15:00.000 95.9
2013-07-24 00:15:00.000 1.285
2013-07-24 00:30:00.000 160
2013-07-24 00:30:00.000 0.952
行1,3和5是一种数据。 2,4和6是另一种类型。我有以下查询可以根据过滤条件检索类型:
SELECT
A.LDTime, A.Value As AValue
FROM
Table7 G
INNER JOIN
Table1 A
INNER JOIN
Table2 B ON A.DataSetIdx = B.DataSetIdx
INNER JOIN
Table3 C ON B.RetrievalIdx = C.RetrievalIdx ON G.ActionIdx = C.ActionIdx
INNER JOIN
Table4 D ON G.SpatialRefIdx = D.SpatialRefIdx
INNER JOIN
Table5 E
INNER JOIN
Table6 F ON E.StationIdx = F.StationIDx ON D.SpatialRefIdx = E.SpatialRefIdx
WHERE
F.StationIDx = 1
AND A.ParameterIdx = 18
AND A.LDTime BETWEEN '2013-7-24' AND '2013-7-25'
ORDER
BY A.LDTime
SELECT
A.LDTime, A.Value As BValue
FROM
Table7 G
INNER JOIN
Table1 A
INNER JOIN
Table2 B ON A.DataSetIdx = B.DataSetIdx
INNER JOIN
Table3 C ON B.RetrievalIdx = C.RetrievalIdx ON G.ActionIdx = C.ActionIdx
INNER JOIN
Table4 D ON G.SpatialRefIdx = D.SpatialRefIdx
INNER JOIN
Table5 E
INNER JOIN
Table6 F ON E.StationIdx = F.StationIDx ON D.SpatialRefIdx = E.SpatialRefIdx
WHERE
F.StationIDx = 1
AND A.ParameterIdx = 19
AND A.LDTime BETWEEN '2013-7-24' AND '2013-7-25'
ORDER BY
A.LDTime
请注意,我在两个查询之间只更改了一个过滤变量(ParameterIdx
),这可以区分值列中存储的数据类型。
如何组合查询以生成一个结果集? (或者,我正在使用存储过程,因此可以在多个查询中完成)。
LDTime AValue BValue
2013-07-24 00:00:00.000 14.85 1.339
2013-07-24 00:15:00.000 95.9 1.285
2013-07-24 00:30:00.000 160 0.952
答案 0 :(得分:1)
这应该这样做:
SELECT a.LDTime
, Avalue = MAX(CASE WHEN A.ParameterIdx = 18 THEN A.Value END)
, BValue = MAX(CASE WHEN A.ParameterIdx = 19 THEN A.Value END)
FROM Table7 G INNER JOIN
Table1 A INNER JOIN
Table2 B ON A.DataSetIdx = B.DataSetIdx INNER JOIN
Table3 C ON B.RetrievalIdx = C.RetrievalIdx ON G.ActionIdx = C.ActionIdx INNER JOIN
Table4 D ON G.SpatialRefIdx = D.SpatialRefIdx INNER JOIN
Table5 E INNER JOIN
Table6 F ON E.StationIdx = F.StationIDx ON D.SpatialRefIdx = E.SpatialRefIdx
WHERE F.StationIDx = 1 AND A.ParameterIdx in (18, 19)
AND A.LDTime BETWEEN '2013-7-24' AND '2013-7-25'
GROUP BY A.LDTime
ORDER BY A.LDTime
答案 1 :(得分:1)
唯一的区别似乎是where
条款。因此,以下内容应正确标记行:
SELECT A.LDTime,A.Value,
(case when F.StationIDx = 1 AND A.ParameterIdx = 18 then 'A'
when A.ParameterIdx = 19 then 'B'
end) as AorB
FROM Table7 G INNER JOIN
Table1 A INNER JOIN
Table2 B ON A.DataSetIdx = B.DataSetIdx INNER JOIN
Table3 C ON B.RetrievalIdx = C.RetrievalIdx ON G.ActionIdx = C.ActionIdx INNER JOIN
Table4 D ON G.SpatialRefIdx = D.SpatialRefIdx INNER JOIN
Table5 E INNER JOIN
Table6 F ON E.StationIdx = F.StationIDx ON D.SpatialRefIdx = E.SpatialRefIdx
WHERE F.StationIDx = 1 AND A.ParameterIdx in (18, 19)
AND A.LDTime BETWEEN '2013-7-24' AND '2013-7-25'
ORDER BY A.LDTime
要执行您想要的操作,您需要查询group by
。这为您提供了所需的结果:
SELECT A.LDTime,
max(case when A.ParameterIdx = 18 then Value end) as AValue,
max(case when A.ParameterIdx = 19 then Value end) as BValue
FROM Table7 G INNER JOIN
Table1 A INNER JOIN
Table2 B ON A.DataSetIdx = B.DataSetIdx INNER JOIN
Table3 C ON B.RetrievalIdx = C.RetrievalIdx ON G.ActionIdx = C.ActionIdx INNER JOIN
Table4 D ON G.SpatialRefIdx = D.SpatialRefIdx INNER JOIN
Table5 E INNER JOIN
Table6 F ON E.StationIdx = F.StationIDx ON D.SpatialRefIdx = E.SpatialRefIdx
WHERE F.StationIDx = 1 AND A.ParameterIdx in (18, 19)
AND A.LDTime BETWEEN '2013-7-24' AND '2013-7-25'
group BY A.LDTime
order by 1