我有一个包含我感兴趣的两列的表格。
一列是日期,另一列是标记名。
我想将标记名返回到与相同日期匹配的不同列中。
我可以使用子查询来实现这一点,但是有没有更有效的方法来做到这一点?我说的是检索大约 20-30k 行,这会导致大约 300,000 个查询
选择插入以确保日期彼此对齐的最有效方法是什么。
这是我目前的疑问。 (我需要每个标记名的值在行中对齐)
谢谢
SELECT ah1.DateTime,ah1.Value as TPH,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM1_NXG.NXG_I'
and datetime = ah1.DateTime
) as EM1_Current,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM2_NXG.NXG_I'
and datetime = ah1.DateTime
) as EM2_Current,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM1_NXG.NXG_SPEED'
and datetime = ah1.DateTime
) as EM1_Speed,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM2_NXG.NXG_SPEED'
and datetime = ah1.DateTime
) as EM2_Speed,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM1_NXG.NXG_P'
and datetime = ah1.DateTime
) as EM1_Power,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM2_NXG.NXG_P'
and datetime = ah1.DateTime
) as EM2_Power,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM1_NXG.NXG_TRQ'
and datetime = ah1.DateTime
) as EM1_Torque,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM2_NXG.NXG_TRQ'
and datetime = ah1.DateTime
) as EM2_Torque,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM1_NXG.NXG_TRQ_UTIL'
and datetime = ah1.DateTime
) as EM1_Torque_U,
(select value
from dbo.AnalogHistory
where tagname = 'LS_EM2_NXG.NXG_TRQ_UTIL'
and datetime = ah1.DateTime
) as EM2_Torque_U,
(select value
from dbo.AnalogHistory
where tagname = 'LS_TE754001G.PVAI'
and datetime = ah1.DateTime
) as EM1_NDE,
(select value
from dbo.AnalogHistory
where tagname = 'LS_TE754001H.PVAI'
and datetime = ah1.DateTime
) as EM1_DE
FROM [Runtime].[dbo].[AnalogHistory] ah1
where TagName = 'CR_WQI752010.PVAI'
and wwResolution = '600000'
and DateTime > '20160816'
and wwRetrievalMode = 'cyclic'
最佳答案
也许您可以使用条件聚合。像这样的事情:
select datetime,
max(case when tagname = 'LS_EM1_NXG.NXG_I' then value end) as val1,
. . .
from dbo.AnalogHistory
group by datetime ;
关于sql - 将 SQL 数据从单列返回到多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050481/