答案 0 :(得分:0)
像
这样的东西DECLARE @Table TABLE(
a INT,
b INT,
c INT,
d INT,
e INT,
f INT
)
INSERT INTO @Table VALUES
(null,null,null,null,null,10),
(null,null,null,null,10,20),
(null,null,null,10,20,30)
SELECT *
FROM @Table
SELECT SUM([1]) a,
SUM([2]) b,
SUM([3]) c,
SUM([4]) d,
SUM([5]) e,
SUM([6]) f
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY (SELECT NULL)) ValID
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ID
FROM @Table
) p
UNPIVOT
(
val FOR col IN (a,b,c,d,e,f)
) up
) v
PIVOT (
SUM(val) FOR ValID IN ([1],[2],[3],[4],[5],[6])
) p
GROUP BY ID