我有一个加载的表,看起来像这样:
CUSTID VALUETYPE COST
1 A 123
1 B 456
1 C 789
2 B 222
我需要将其展平在同一张表中或插入新表中,如下所示:
CUSTID A B C
1 123 456 789
2 0 222 0
每行都有一个未显示的标识列。
此光标是什么样的?
谢谢。
答案 0 :(得分:0)
使用查询,例如条件聚合:
concrete.limits.sitemap_pages
答案 1 :(得分:0)
用例
<header>
<ul>
<li><a href="#"><span>Home</span></a></li>
<li><a href="#"><span>About us</span></a></li>
<li><a href="#"><span>Services</span></a></li>
<li><a href="#"><span>Contact</span></a></li>
</ul>
</header>
答案 2 :(得分:0)
我看不到您需要对各列求和:
select
custid,
max(case when valuetype = 'A' then cost else 0 end) A,
max(case when valuetype = 'B' then cost else 0 end) B,
max(case when valuetype = 'C' then cost else 0 end) C
from tablename
group by custid
答案 3 :(得分:0)
您可以使用PIVOT
SELECT
CUSTID
,ISNULL(p.A,0) AS A
,ISNULL(p.B,0) AS B
,ISNULL(p.C,0) AS C
FROM t
PIVOT (
SUM(COST) FOR VALUETYPE IN ([A],[B],[C])) p
答案 4 :(得分:0)
如果您不介意结果中的NULL值
Select *
From YourTable
Pivot (sum(Cost) for ValueType in ([A],[B],[C])) pvt
返回
CUSTID A B C
1 123 456 789
2 NULL 222 NULL
否则,您可以消除NULL值
Select *
From (Select * From YourTable
Union All
Select A.CustID ,B.VALUETYPE,0
From (Select Distinct CustID from YourTable) A
Cross Join (Select Distinct VALUETYPE from YourTable) B
) src
Pivot (sum(Cost) for ValueType in ([A],[B],[C])) pvt
返回
CUSTID A B C
1 123 456 789
2 0 222 0