将行转换为TSQL列

发布于 2025-02-12 07:08:38 字数 1431 浏览 3 评论 0原文

如何将以下示例从行转换为列?

例如。

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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-02-19 07:08:38
SELECT  [first_name], [code],
        [2022-07-01], [2022-07-02], [2022-07-03]
FROM (
    SELECT *
    FROM dbo.test
) AS SourceTable
PIVOT (
    AVG(total)
    FOR date_from IN ([2022-07-01], [2022-07-02], [2022-07-03])
) AS PivotTable;
SELECT  [first_name], [code],
        [2022-07-01], [2022-07-02], [2022-07-03]
FROM (
    SELECT *
    FROM dbo.test
) AS SourceTable
PIVOT (
    AVG(total)
    FOR date_from IN ([2022-07-01], [2022-07-02], [2022-07-03])
) AS PivotTable;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文