如何将单个查询中的多个总和从列转换为行

时间:2015-09-25 19:39:30

标签: mysql sum pivot

我搜索了所有的答案数据库,老实说,找不到我需要的东西。我有一个包含合约价值的表格,以及与每个价值相关的日期。我的查询应该是对过去6个月累计的合约价值进行求和,从当前日期开始回顾3个月,总是考虑6个月的累积期:

SELECT
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 0 MONTH), INTERVAL 6 MONTH) AND (ContractCancelled <>1)) * ContractValue) AS Today, 
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND (ContractCancelled <>1)) * ContractValue) AS OneMonthAgo,
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) AND (ContractCancelled <>1)) * ContractValue) AS TwoMonthsAgo, 
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)) AND (ContractCancelled <>1)) * ContractValue) AS ThreeMonthsAgo 
FROM Contracts

查询工作正常,但我将结果放在一行中,每个累积值都在不同的列中:

+----+---------+-------------+--------------+----------------+
|    | Today   | OneMonthAgo | TwoMonthsAgo | ThreeMonthsAgo | 
+--------------+-------------+--------------+----------------+
|  1 | 3434005 |   3992877   |    4104565   |     3688412    |
+--------------+-------------+--------------+----------------+

我需要的是将此查询转换为图形,并且为了做到这一点,而不是列,累积的结果应该显示为行,或者像这样:

+---------------+--------------------------------------+
|  LookingTo    |  AcumulatedContractValue (6 months)  | 
+---------------+--------------------------------------+
|    Today      |              3434005                 |
+---------------+--------------------------------------+
| OneMonthAgo   |              3992877                 |
+---------------+--------------------------------------+
| TwoMonthsAgo  |              4104565                 |
+---------------+--------------------------------------+
|ThreeMonthsAgo |              3688412                 |
+---------------+--------------------------------------+

如果它是在MS SQL中,我相信PIVOT应该可以工作,但我不知道如何在MySQL中做到这一点。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我不明白为什么你需要那个,但你可以结合:

SELECT 'Today' as `LookingTo`,
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 0 MONTH), INTERVAL 6 MONTH) AND (ContractCancelled <>1)) * ContractValue) as  Acumulated
FROM Contracts
UNION ALL
SELECT 'OneMonthAgo',
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND (ContractCancelled <>1)) * ContractValue)
FROM Contracts
UNION ALL
SELECT 'TwoMonthsAgo',
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) AND (ContractCancelled <>1)) * ContractValue)  
FROM Contracts
UNION ALL
SELECT 'ThreeMonthsAgo',
SUM((ContractDate > DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), INTERVAL 6 MONTH) AND (ContractDate <= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)) AND (ContractCancelled <>1)) * ContractValue) 
FROM Contracts