将列与不同的筛选器组合



我基本上有一个大型数据库,其中有[t_stamp]、[floatvalue]和[tagpath]列。我正在尝试最终能够拥有一个只有[t_stamp]的查询,多列[floatvalue]按不同的[tagpath]值排序。我不会把行变成列。我正在用不同的筛选复制一个专栏。据我所知,没有涉及转向。如果这让人困惑,我真的很抱歉,所以我一定会添加表格的图片。我认为这将是一个完美的案例场景示例,其中每一列都可以简单地有一个筛选标记路径的案例,但当我尝试这样做时,我会得到[t_stamp]的多个结果,其中包括一个相当慢的sql查询,只做了两个案例。(最终需要做15-20次,但一旦确认查询时间就不是一个因素(

select distinct
[t_stamp] as 'Time',
(case when dbo.taghistoriancombined.tagpath like 'extruder 1/plc/temps/exhaust wet bulb temp' then [floatvalue] end) as 'Wet Bulb',
(case when dbo.taghistoriancombined.tagpath like 'extruder 1/plc/extruder/motor load' then [floatvalue] end) as 'Motor Load'
from [dbo].[TagHistorianCombined]

然后我想,"也许我可以接受两个带有空占位符的查询,并使用一个联合来消除任何可能的覆盖

SELECT
[t_stamp] as time
,[floatvalue] as 'Motor Load'
,null as 'Wet Bulb'
FROM [dbo].[TagHistorianCombined]
where tagpath like 'extruder 1/plc/extruder/motor load'
union
SELECT
[t_stamp] as time
,null as 'Motor Load'
,[floatvalue] as 'Wet Bulb'
FROM [dbo].[TagHistorianCombined]
where tagpath like 'extruder 1/plc/temps/exhaust wet bulb temp'
order by time desc

但不幸的是,这给了我同样的结果。想要的模式但不想要的结果

解决这一问题的最佳方法是什么?我有相当粗略的解释技巧,这是我在这个网站上的第一篇帖子,所以如果我遗漏了什么,或者如果我需要进一步解释,请毫不犹豫地问。

我想你想要group by。以下每个时间戳返回一行:

select t_stamp,
max(case when thc.tagpath like 'extruder 1/plc/temps/exhaust wet bulb temp' then floatvalue end) as WetBulb,
max(case when thc.tagpath like 'extruder 1/plc/extruder/motor load' then floatvalue end) as MotorLoad
from [dbo].TagHistorianCombined thc
where thc.tagpath like 'extruder 1/plc/temps/exhaust wet bulb temp' or
thc.tagpath like 'extruder 1/plc/extruder/motor load'
group by t_stamp

最新更新