0

我见过类似的问题,但无法完全找到解决此问题的方法。忍受乱七八糟的桌子,问题应该是什么?非常坦率的。

我有下表。

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在两个查询之间只更改了一个过滤器变量 ( ),它区分了 value 列中存储的数据类型。

如何组合查询以生成一个结果集?(或者,我正在使用存储过程,所以这可以在多个查询中完成)。

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
4

2 回答 2

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
于 2013-07-24T22:56:42.637 回答
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
于 2013-07-24T22:58:03.947 回答