Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

sql server 2008 - Dynamic select statement to convert recordset into a readable format

I am stumped on an output query. I feel unpivot will get me there but I can't seem to get the syntax correct and could use some help. Basically, a single record set contains two columns and one column is actually the column name while the other column is the column value. Ideally I would like the output to label column 2 as column 1's result for each possible unique value. Below is a simplified sample but the final solution could contain an infinite number of attributes per pc.

create table #t (pc int, attribute varchar(100), result varchar(100) )
go
INSERT INTO #t VALUES (1,'OS','LINUX'),(1,'VERSION','10.4'),(2,'OS','WINDOWS'),(2,'VERSION','10.1903'),(3,'OS','LINUX'),(3,'VERSION','11.0'),(4,'OS','WINDOWS'),(4,'VERSION','10.1909'),(5,'OS','WINDOWS'),(5,'VERSION','10.1909'),(6,'OS','LINUX'),(6,'VERSION','10.4')
go
select * from #t

Result Set:

id pc  attribute   result
1   OS          LINUX
1   VERSION     10.4
2   OS          WINDOWS>
2   VERSION     10.1903
3   OS          LINUX
3   VERSION     11.0
4   OS          WINDOWS
4   VERSION     10.1909
5   OS          WINDOWS
5   VERSION     10.1909
6   OS          LINUX
6   VERSION     10.4

Desired Output:

pc OS          VERSION
1   LINUX       10.4
2   WINDOWS     10.1903
3   LINUX       11.0
4   WINDOWS     10.1909
5   WINDOWS     10.1909
6   LINUX       10.4

Any tips/advice you can give would be greatly appreciated. Cheers mate.

question from:https://stackoverflow.com/questions/65886819/dynamic-select-statement-to-convert-recordset-into-a-readable-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...