我在SQL Server中有以下数据。我想在COMPONENT 1,COMPONENT 2,COMPONENT 3,COMPONENT 4列中转置值,并将JUL,AUG,SEPT,OCT,NOV,DEC中的数字相加,如果有任何重复。我通常在Excel中使用PIVOT表但我想在SQL Server中这样做以节省一些时间。
CUST NAME CUST PART# PN# COMPONENT 1 COMPONENET 2 COMPONENT 3 COMPONENT 4 JUL AUG SEP OCT NOV DEC
COMPANY X 4455623 7801080R 902031 1200958 1 2
COMPANY X 4455622 78601080L 902031 1200958 1 2
COMPANY X 4455621 78601076R 902031 1200959 1 2
COMPANY X 4455620 78601076L 902031 1200959 1 2
COMPANY X 4455439 78601018R 900574 1200960 2 12
COMPANY X 4455438 78601018L 900574 1200960 2 12
COMPANY X 4455364 78601016R 900574 1200959 6 6
COMPANY X 4455361 78601016L 900574 1200959 6 6
COMPANY X 4455363 78601014R 900574 1200958 6 6
COMPANY X 4455362 78601014L 900574 1200958 6 6
我想得到以下结果
CUST NAME COMP JUL AUG SEPT OCT NOV DEC
---------------------------------------------------
COMPANY X 1200958 2 12 16
COMPANY X 1200959 2 12 16
COMPANY X 1200960 4 24
答案 0 :(得分:0)
根据您的示例输出,您似乎不会转置任何内容,而只是简单地聚合“COMPONENET 2”。
查询:
SELECT CUST, NAME, COMP2,
SUM( JUL ) AS Jul, SUM( AUG ) AS AUG, SUM( SEPT ) AS SEPT,
SUM( OCT ) AS OCT, SUM( NOV ) AS AUG, SUM( DEC ) AS DEC
FROM [table]
GROUP BY CUST, NAME, COMP2