我试图将以下代码中的结果转换为将多个表连接在一起。我知道我需要使用PIVOT这可能是一个简单的修复,但我很难让代码工作。我的代码如下:
SELECT F.SetValue, D.Name FROM Device D
INNER JOIN Location L ON D.LocationId = L.LocationId
INNER JOIN Fitting F ON L.LocationId = F.LocationId
INNER JOIN LocationTypeFitting LTF ON F.LocationTypeFittingId = LTF.LocationTypeFittingId
WHERE D.DeviceName = 'Device 1' AND LTF.Name LIKE '%Television%';
打印以下结果:
SetValue | Name
===========================
1 | TV_Power
1 | TV_Volume
1 | TV_Source
我需要返回以下值:
TV_Power | TV_Volume | TV_Source
================================
1 | 1 | 1
我知道我还需要一个GROUP BY语句,但是加入其他表会使这个特定查询变得越来越困难。非常感谢任何帮助。
答案 0 :(得分:0)
希望这足以让你继续下去。
SELECT 'DeviceType' as DeviceTYpe,* FROM
(
SELECT D.Name, F.SetValue FROM Device D
INNER JOIN Location L ON D.LocationId = L.LocationId
INNER JOIN Fitting F ON L.LocationId = F.LocationId
INNER JOIN LocationTypeFitting LTF ON F.LocationTypeFittingId = LTF.LocationTypeFittingId
WHERE D.DeviceName = 'Device 1' AND LTF.Name LIKE '%Television%'
) AS SourceTable
PIVOT
(
MAX(SetValue)
FOR Name in ([TV_Power], [TV_Volume], [TV_Source])
) As PivotTable
答案 1 :(得分:0)
我会做以下两件事:
for k, v := range cols.Collections {
c.Index = i
p := &cols.Collections[k]
c.Collections[v.ID] = p
表(或其他一些表)添加另一列,例如DeviceName
,以便在执行数据透视后可以区分行(我假设会有多个一排)。它还会显示Device
在评论中的位置。
group by