SQL查询未产生预期结果

时间:2018-11-27 10:03:23

标签: sql excel

我需要将数据(图片的第一部分)转换为按列的版本(图片的第二部分)。

到目前为止,我的代码是

StrQuery = "SELECT  CAST(readingDTTM AS DATETIME) DATETIME, rValue FROM
loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'" & _
"and Sensor = 'Depth' GROUP BY  CAST(readingDTTM AS DATETIME), rValue, Sensor"

但这只会恢复深度。我还需要2列温度和电压列。

Images of Data

2 个答案:

答案 0 :(得分:0)

SELECT CAST(readingDTTM AS DATETIME) DATETIME, case Sensor when 'Depth' then rValue end as Depth, case Sensor when 'Temperature' then rValue end as Temperature FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft' and Sensor in ('Depth','Temperature') GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor

或者您可以使用数据透视

答案 1 :(得分:0)

select DATETIME, max(Depth) Depth, max(Temperature) Temperature from( SELECT CAST(readingDTTM AS DATETIME) DATETIME, case Sensor when 'Depth' then rValue end as Depth, case Sensor when 'Temperature' then rValue end as Temperature FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft' and Sensor in ('Depth','Temperature') GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor ) group by DATETIME

-不漂亮,但应该可以使用