[英]How to transform table with several columns into a keyword table
I have a table with columns like CustID, FirstName, LastName, PhoneNumber, Membershiplevel, etc
.我有一个表,其中包含
CustID, FirstName, LastName, PhoneNumber, Membershiplevel, etc
列。
I want to produce a table with these columns: CustID, Keyword, Value
.我想生成一个包含这些列的表:
CustID, Keyword, Value
。
The column names would be the keywords, and there would be a row for every keyword.列名将是关键字,每个关键字都会有一行。
So this:所以这:
CustID, FirstName, LastName, PhoneNumber, MembershipLevel
1234 Joe Smith 555-555-5555, Select
Would become会成为
CustID, Keyword , Value
1234 , FirstName , Joe
1234 , LastName , Smith
1234 , PhoneNumber, 555-555-5555
1234 , MembershipLevel, Select
I know I could painstakingly accomplish this using dynamic SQL, but is there a straightforward, "SQL" way to do this without resorting to procedural T-SQL?我知道我可以使用动态 SQL 煞费苦心地完成此任务,但是否有一种简单的“SQL”方式可以在不诉诸过程 T-SQL 的情况下做到这一点?
use CROSS APPLY
operator with VALUES
constructor将
CROSS APPLY
运算符与VALUES
构造函数一起使用
select a.*
from tbl
cross apply
(
values
(CustID, 'FirstName', FirstName),
(CustID, 'LastName', FirstName),
(CustID, 'PhoneNumber', PhoneNumber),
(CustID, 'MembershipLevel',MembershipLevel)
) a (CustID, KeyWord, Value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.