如何将对角线行转换为单行



这是我的表。数字是对角排列的。

Engineering  Financials  Scope  Schedule  Risks  People
--------------------------------------------------------
1            NULL        NULL   NULL      NULL   NULL
NULL         0           NULL   NULL      NULL   NULL
NULL         NULL        0      NULL      NULL   NULL
NULL         NULL        NULL   0         NULL   NULL
NULL         NULL        NULL   NULL      0      NULL
2            NULL        NULL   NULL      NULL   NULL
NULL         4           NULL   NULL      NULL   NULL
NULL         NULL        3      NULL      NULL   NULL
NULL         NULL        NULL   4         NULL   NULL
NULL         NULL        NULL   NULL      4      NULL
NULL         NULL        NULL   NULL      NULL   0
0            NULL        NULL   NULL      NULL   NULL
NULL         0           NULL   NULL      NULL   NULL
NULL         NULL        0      NULL      NULL   NULL
NULL         NULL        NULL   0         NULL   NULL
NULL         NULL        NULL   NULL      0      NULL
NULL         NULL        NULL   NULL      NULL   0
0            NULL        NULL   NULL      NULL   NULL
NULL         0           NULL   NULL      NULL   NULL
NULL         NULL        0      NULL      NULL   NULL
NULL         NULL        NULL   0         NULL   NULL
NULL         NULL        NULL   NULL      0      NULL
NULL         NULL        NULL   NULL      NULL   0
1            NULL        NULL   NULL      NULL   NULL
NULL         0           NULL   NULL      NULL   NULL
NULL         NULL        5      NULL      NULL   NULL
NULL         NULL        NULL   4         NULL   NULL
NULL         NULL        NULL   NULL      3      NULL
NULL         NULL        NULL   NULL      NULL   3

我希望这些在这个输出中出现:

Engineering  Financials  Scope  Schedule  Risks  People
--------------------------------------------------------
1            0           0      0         0      NULL
2            4           3      4         4      0
0            0           0      0         0      0
0            0           0      0         0      0
0            0           0      0         0      0
1            0           5      4         3      3

实际上,我想将这些对角线行转换为每个部分的单行。

您想要的聚合结果似乎是基于表中行的顺序。在表中,行顺序应该无关紧要,并且假设顺序保持不变是危险的。

因此,除非您还没有向我们展示完整的源表(例如,其中有一个Section列),否则很难为您提供这个问题的有用答案。


编辑:

如果Section列,则可以使用以下查询来获得所需结果:

SELECT   Section
,SUM(Engineering) AS Engineering
,SUM(Financials) AS Financials
,SUM(Scope) AS Scope
,SUM(Schedule) AS Schedule
,SUM(Risks) AS Risks
,SUM(People) AS People
FROM     YourTable
GROUP BY Section;

您也可以使用MAX或其他函数,而不是使用SUM函数来聚合数据,这取决于数据和您想要从中获得的内容

这太长了,不能作为注释,所以我正在回答。

目前还不清楚您的数据来自哪里,是数据表还是查询。你之前的问题是关于PIVOT的,你提供的数据与相同的数据匹配,所以我猜这是来自该查询。

如果您使用的是PIVOT查询,并且子查询中的字段太多,则可能会发生这种类型的行为。

样本数据查询1

CREATE TABLE yourtable ([Name] varchar(11), [Rating] int);
INSERT INTO yourtable ([Name], [Rating])
VALUES
('Engineering', 1),
('Financials', 3),
('Scope', 1),
('Schedule', 2),
('Risks', 3),
('People', 3);

PIVOT将提供正确的结果

select *
from
(
select name, rating
from yourtable
) src
pivot
(
max(rating)
for name in ([Engineering], [Financials], [Scope],
[Schedule], [Risks], [People])
) piv

请参阅带有演示的SQL Fiddle

结果如下:

| ENGINEERING | FINANCIALS | SCOPE | SCHEDULE | RISKS | PEOPLE |
----------------------------------------------------------------
|           1 |          3 |     1 |        2 |     3 |      3 |

上面的语法是正确的,因为条目只有一行。现在,如果我稍微修改一下这个查询,你就会看到区别:

示例数据查询2:我已将id字段添加到此

CREATE TABLE yourtable([Name] varchar(11), [Rating] int, id int);
INSERT INTO yourtable([Name], [Rating], id)
VALUES
('Engineering', 1, 1),
('Financials', 3, 2),
('Scope', 1, 3), 
('Schedule', 2, 4),
('Risks', 3, 5),
('People', 3, 6);

如果我执行PIVOT并包含此ID字段,结果将与您的结果类似。

PIVOT

select [Engineering], [Financials], [Scope], [Schedule], [Risks], [People]
from
(
select name, rating, id  -- notice the addition of the id column
from yourtable
) src
pivot
(
max(rating)
for name in ([Engineering], [Financials], [Scope],
[Schedule], [Risks], [People])
) piv

请参阅带有演示的SQL Fiddle

结果:

| ENGINEERING | FINANCIALS |  SCOPE | SCHEDULE |  RISKS | PEOPLE |
------------------------------------------------------------------
|           1 |     (null) | (null) |   (null) | (null) | (null) |
|      (null) |          3 | (null) |   (null) | (null) | (null) |
|      (null) |     (null) |      1 |   (null) | (null) | (null) |
|      (null) |     (null) | (null) |        2 | (null) | (null) |
|      (null) |     (null) | (null) |   (null) |      3 | (null) |
|      (null) |     (null) | (null) |   (null) | (null) |      3 |

正如你所看到的,添加这个额外的专栏再现了你所得到的问题。我的建议是发布你的表结构,然后发布你正在使用的查询来获得结果。因此,我们将能够为您提供帮助。

最新更新