我们正在尝试为每个客户列输出每月应有12个预算行条目。
情景1:
ie. Turn table data :
Name BudMnt1|BudMnt2|BudMnt3
cust1 0 0 0
cust2 0 0 0
cust3 2418 0 0
cust4 0 416 198
into this :
Name cust1| cust2| cust3| cust4
BudMnt1 0 0 24180 0
BudMnt2 0 0 0 416
BudMnt3 0 0 0 198
场景2: 包括方案1列作为预算+附加列是此处的销售代码 因此,它成为两个列预算,销售需要在单个查询结构上进行忽略。
ie. Turn table data :
Name JanSales|FebSales|MarSales
cust1 0 0 0
cust2 0 0 0
cust3 0 0 3
cust4 2 0 0
into this :
Name cust1|cust2|cust3|cust4
JanSales 0 0 0 2
Feb Sales 0 0 0 0
Mar Sales 0 0 3 0
非常感谢任何帮助!
答案 0 :(得分:1)
此链接可以帮助您作为对您问题的参考:
答案 1 :(得分:0)
此代码工作正常。在sql server 2012上品尝它。
SELECT NAME ,
budmtn ,
cust
INTO #temptable
FROM ( SELECT *
FROM tbl1
) AS result UNPIVOT ( cust FOR budmtn IN ( budmtn1, budmtn2, budmtn3 ) ) AS unpivotedtable
SELECT budmtn ,
cust1 ,
cust2 ,
cust3 ,
cust4
FROM #temptable PIVOT( SUM(cust) FOR NAME IN ( cust1, cust2, cust3, cust4 ) ) AS result