我有两张桌子:TableA 和 TableB
表 A:columnBLA、objName、objID
表 B:objID、ColumnIndex、StringValue、NumberValue、DateValue
TableA has a row: bla, Object Name, 21
TableB also has data for objID=21:
21, 1, a, NULL, NULL
21, 1, b, NULL, NULL
21, 1, c, NULL, NULL
21, 2, NULL, 11, NULL
21, 2, NULL, 22, NULL
21, 2, NULL, 33, NULL
21, 3, NULL, NULL, 1/1/2012
21, 3, NULL, NULL, 1/1/2013
21, 3, NULL, NULL, 1/1/2014
现在我想将这些数据重塑为以下形式:
a, 11, 1/1/2012
b, 22, 1/1/2013
c, 33, 1/1/2014
我已经做到了这一点:
Select StringValue, NumberValue, DateValue
From
(Select StringValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 1) As StringValues
,
(Select NumberValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 2) As NumberValues
,
(Select DateValue
From TableA ta WITH (NOLOCK), TableB tb WITH (NOLOCK)
WHERE ta.objID = tb.objID
AND t.objName = N'Object Name'
AND d.ColumnIndex = 1) As DateValues
但我得到了不想要的结果。有人告诉我我应该为此使用 PIVOT,但我的 SQL 知识并没有那么远。