sqlserver:将行转换为动态列 [英] sqlserver: convert rows into columns dynamic
问题描述
我使用sqlserver 2012 Express
我有这个选择
选择C.COMPUTER_NAME,A .PART_NAME
来自Projects.PARTS_ON_COMPUTERS P
左加入Projects.COMPUTERS C ON C.Id = P.COMPUTER
LEFT JOIN Projects.COMPUTER_PARTS A ON A.Id = P.PART
给出mes结果
computer_Name part_name
--------------------------------------------
pc1硬200g
pc1显卡
pc1网卡
pc2硬500g
pc2显卡
但是我需要的结果是
pc1硬200g显示卡网卡< br $> b $ b pc2硬500g显示卡
请帮忙
I use sqlserver 2012 Express
I have this select
select C.COMPUTER_NAME, A.PART_NAME
from Projects.PARTS_ON_COMPUTERS P
left join Projects.COMPUTERS C ON C.Id= P.COMPUTER
LEFT JOIN Projects.COMPUTER_PARTS A ON A.Id= P.PART
gives mes results
computer_Name part_name
--------------------------------------------
pc1 hard 200g
pc1 display card
pc1 network card
pc2 hard 500g
pc2 display card
but I need the results to be
pc1 hard 200g display card network card
pc2 hard 500g display card
please help
推荐答案
我怀疑你真的不需要这个,并且转换会更好地完成。代码。
但如果你*真的*需要在SQL,看看PIVOT和UNPIVOT。
哦,看看页面右边的'相关问题'......
I suspect you don't really need to this, and the transformation would be better done in .the code.
But if you *really* need to do it in SQL, look at PIVOT and UNPIVOT.
Oh, and take a look over on the right of the page at 'Related Questions'...
这篇关于sqlserver:将行转换为动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!