Sql 使用不同的过滤器组合列

Sql 使用不同的过滤器组合列,sql,sql-server,Sql,Sql Server,我基本上有一个包含[t_stamp]、[floatvalue]和[tagpath]列的大型数据库。我试图最终能够让一个查询只有[t_stamp],多个[floatvalue]列按不同的[tagpath]值排序。我不会把行变成列。我正在用不同的筛选复制一列。据我所知,不涉及旋转。我真的很抱歉,如果这是混淆,所以我一定要添加表的图片。 我认为这是一个案例场景的完美例子,每个列都可以简单地用一个案例过滤标记路径,但当我尝试这样做时,我得到了[t_stamp]的多个结果,包括一个相当慢的sql查询,只需

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

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

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
但不幸的是,这给了我同样的结果


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

我想你想要
分组方式。
。以下每一个时间戳返回一行:

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

可能重复的I可能是错误的,但我确信这与数据透视表无关。这肯定消除了多个t_标记。谢谢!但现在不幸的是,它提供了大量的t_标记,其中既没有关于湿球的信息,也没有关于机动负载的信息。当两者都如果这些列为空?@Jacerracer3…一个
WHERE
子句应该满足您的要求。非常感谢
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