在SQL Server中水平构造数据 [英] Structure the data horizontally in SQL server

查看:51
本文介绍了在SQL Server中水平构造数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个查询返回如下数据



选择n.Key,np.value来自Test1 np join Test2 n on n.Key = np。密钥,其中n.NodeKey = 10000002&& np.pKey in(4,6,7,10,12)



返回如下数据



键值

-------- ------

10000002 2

10000002 0

10000002 2

10000002真

10000002 1



Test2查找表如下< br $>


关键PKey值

------------------------ ---------------

10000002 4 2

10000002 6 0

10000002 7 2

10000002 10真

10000002 12 1





想要更改查询,以便它返回如下数据



NodeKey Value1 Value2 Value3 Value4 Value5

------------------------------------------------ -

10000002 2 0 2真1



能帮忙吗我有这个。



我尝试了什么:



尝试重组查询,以便它水平返回数据

Hi I have a query which returns data as below

select n.Key, np.value from Test1 np join Test2 n on n.Key = np.Key where n.NodeKey = 10000002 && np.pKey in (4,6,7,10,12)

which returns data as below

Key value
-------- ------
10000002 2
10000002 0
10000002 2
10000002 True
10000002 1

Test2 is look up Table as below

Key PKey Value
---------------------------------------
10000002 4 2
10000002 6 0
10000002 7 2
10000002 10 True
10000002 12 1


Wanted to change the query so that it should return data as below

NodeKey Value1 Value2 Value3 Value4 Value5
--------------------------------------------------
10000002 2 0 2 True 1

Can you please help me with this.

What I have tried:

Tried restructuring the query so that it returns data horizontally

推荐答案

请看这里:使用PIVOT和UNPIVOT | Microsoft Docs [ ^ ]


这篇关于在SQL Server中水平构造数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆