寻找优雅(或任何)解决方案将列转换为行.
这是一个例子:我有一个包含以下模式的表:
[ID] [EntityID] [Indicator1] [Indicator2] [Indicator3] ... [Indicator150]
Run Code Online (Sandbox Code Playgroud)
以下是我想得到的结果:
[ID] [EntityId] [IndicatorName] [IndicatorValue]
Run Code Online (Sandbox Code Playgroud)
结果值将是:
1 1 'Indicator1' 'Value of Indicator 1 for entity 1'
2 1 'Indicator2' 'Value of Indicator 2 for entity 1'
3 1 'Indicator3' 'Value of Indicator 3 for entity 1'
4 2 'Indicator1' 'Value of Indicator 1 for entity 2'
Run Code Online (Sandbox Code Playgroud)
等等..
这有意义吗?您对在T-SQL中查看的位置以及如何完成它有什么建议吗?
我有一张桌子,我需要以下列方式呈现输出.
tb_a:
col1 | reg_id | rsp_ind
Run Code Online (Sandbox Code Playgroud)
rsp_ind = 0的行数为"New",1为"Accepted"
输出应该是
NEW | Accepted
9 | 10
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下查询.
select
case when rsp_ind = 0 then count(reg_id)end as 'New',
case when rsp_ind = 1 then count(reg_id)end as 'Accepted'
from tb_a
Run Code Online (Sandbox Code Playgroud)
我得到的输出为
NEW | Accepted
NULL| 10
9 | NULL
Run Code Online (Sandbox Code Playgroud)
有人可以帮我调整查询以实现输出.注意:我无法在此处添加总和.它是一个更大的程序的一部分,所以我不能添加一个超级查询.
如果这是重复的,我很抱歉。请指出我正确的问题。我使用的是 SQL SERVER 2008。我使用下面的查询,因为我需要从 3 个表中获取数据。
SELECT qc.FileID as [FileID],
qc.QID1 as [QID1],
xqs.SID1 as [SID1],
xqc.CID1 as [CID1],
xqs.Comments as [SComments],
xqc.Comments as [CComments]
FROM QCTable(nolock) qc
JOIN QCSectionTable (nolock) xqs ON qc.QCID = xqs.QCID
LEFT JOIN QCChargeTable (nolock) xqc ON xqc.QCXrefID = xqs.QCXrefID
Run Code Online (Sandbox Code Playgroud)
对于上面我得到这个像 FieID1 SID1 SID1 CID1 SComments CComments
我有一排像下面
FileID1 QID1 SID1 CID1 SComments CComments
Run Code Online (Sandbox Code Playgroud)
我需要将上面的行拆分为
FileID1 QID1 SID1 null SComments
FileID1 QID1 SID1 CID1 CComments
Run Code Online (Sandbox Code Playgroud)
提前致谢。