将行转换为TSQL列
如何将以下示例从行转换为列?
例如。
create table test
(
date_from date,
first_name varchar(50),
code int,
total decimal
)
Insert into test(date_from, first_name,code, total) values (getdate(), 'John', 1, 5);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'John', 1, 10);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'John', 1, 15);
Insert into test(date_from, first_name,code, total) values (getdate(), 'John', 2, 6);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'John', 2, 12);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'John', 2, 18);
Insert into test(date_from, first_name,code, total) values (getdate(), 'Oliver', 1, 5);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'Oliver', 1, 10);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'Oliver', 1, 15);
Insert into test(date_from, first_name,code, total) values (getdate(), 'Oliver', 2, 6);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'Oliver', 2, 12);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'Oliver', 2, 18);
我需要折射为:
How convert below example from row to column?
eg.
create table test
(
date_from date,
first_name varchar(50),
code int,
total decimal
)
Insert into test(date_from, first_name,code, total) values (getdate(), 'John', 1, 5);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'John', 1, 10);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'John', 1, 15);
Insert into test(date_from, first_name,code, total) values (getdate(), 'John', 2, 6);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'John', 2, 12);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'John', 2, 18);
Insert into test(date_from, first_name,code, total) values (getdate(), 'Oliver', 1, 5);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'Oliver', 1, 10);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'Oliver', 1, 15);
Insert into test(date_from, first_name,code, total) values (getdate(), 'Oliver', 2, 6);
Insert into test(date_from, first_name,code, total) values (getdate() + 1, 'Oliver', 2, 12);
Insert into test(date_from, first_name,code, total) values (getdate() + 2, 'Oliver', 2, 18);
I need resault as:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)