我有以下数据集:
Account|Can|Fra|scl|
1012 0 10 1
1011 5 10 0
如何首先获得连续每列的总数:
Account|Can|Fra|scl|
1012 0 10 1
1011 5 10 0
Total 5 20 1
然后转置它,看起来像这样:
Can 5
Fra 20
Scl 1
答案 0 :(得分:1)
此查询可以帮助您。它将避免不必要的中间步骤。您可以使用以下查询直接获得最终结果
SELECT 'CAN' AS COUNTRY, SUM(CAN) AS COUNTVAL FROM MY_TABLE
UNION ALL
SELECT 'FRA' AS COUNTRY, SUM(FRA) AS COUNTVAL FROM MY_TABLE
UNION ALL
SELECT 'SCL' AS COUNTRY, SUM(SCL) AS COUNTVAL FROM MY_TABLE
如果您想获得每列的总数,那么您可以使用:
SELECT ACCOUNT, CAN, FRA,SCL FROM MY_TABLE
UNION ALL
SELECT 'TOTAL' AS ACCOUNT, SUM(CAN) AS CAN, SUM(FRA) AS FRA, SUM(SCL) AS SCL
FROM MY_TABLE