SQL从同一个表中选择多个

时间:2016-06-14 11:54:08

标签: sql-server

我有一个包含三列的Transactions表:transactionType,amount和transactionTimestamp。 transactionType列接受值1(现金),2(支票)或3(信用卡)。

我需要显示每天每个transactionType的总金额。我可以使用单独的查询为每个transactionType轻松地执行此操作:

    SELECT SUM(amount) AS cashTotal, dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
    FROM TransactionRecords
    WHERE transactionType = 1
    GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

    SELECT SUM(amount) AS checkTotal, dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
    FROM TransactionRecords
    WHERE transactionType = 2
    GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

    SELECT SUM(amount) AS cardTotal, dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
    FROM TransactionRecords
    WHERE transactionType = 3
    GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

但我需要做的是在一个查询中完成。我已尝试将上述内容合并为一个,如下所示:

SELECT 
        (SELECT SUM(amount) FROM TransactionRecords WHERE transactionType = 1 GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp))) AS cashTotal, 
        (SELECT SUM(amount) FROM TransactionRecords WHERE transactionType = 2 GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp))) AS checkTotal,
        (SELECT SUM(amount) FROM TransactionRecords WHERE transactionType = 3 GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp))) AS cardTotal,
        dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
    FROM TransactionRecords
    GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

但这只会导致“子查询返回超过1个值”错误。而且我觉得不管怎么回事。谢谢你的任何建议。

编辑:我应该澄清一点,我正在尝试输出如下结果:

----------------------------------------------------
cashTotal | checkTotal | cardTotal | transactionDay
----------------------------------------------------
1000      | 1000        | 1000      | date
0         | 500         | 0         | date
----------------------------------------------------

每一行显示该特定日期的每个transactionType的总金额。

编辑:编辑了一些细节以删除敏感信息。

4 个答案:

答案 0 :(得分:1)

所以你想通过paymentType简单地分组吗?

SELECT SUM(amount) AS cashTotal
     , paymentType
     , dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
FROM PaymentRecords
GROUP BY paymentType
       , dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

您可以添加ORDER BY以按照您想要的顺序获取商品(按类型,然后按日期,反之亦然)

答案 1 :(得分:1)

您可以使用条件聚合。此外,您可以转换为date以删除日期的时间部分:

SELECT SUM(CASE WHEN transactionType = 1 THEN amount ELSE 0 END) as cashTotal, 
       SUM(CASE WHEN transactionType = 2 THEN amount ELSE 0 END) as checkTotal, 
       SUM(CASE WHEN transactionType = 3 THEN amount ELSE 0 END) as cardTotal,
       CAST(transactionTimestamp as DATE) as TransactionDay
FROM TransactionRecords
GROUP BY CAST(transactionTimestamp as DATE)
ORDER BY CAST(transactionTimestamp as DATE);

答案 2 :(得分:1)

只需使用CASE EXPRESSION条件聚合:

SELECT 
        SUM(CASE WHEN paymentType = 1 THEN amount ELSE 0 END) as cashTotal,
        SUM(CASE WHEN paymentType = 2 THEN amount ELSE 0 END) as chequeTotal,
        SUM(CASE WHEN paymentType = 3 THEN amount ELSE 0 END) as cardTotal,
        dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp)) AS transactionDay
    FROM PaymentRecords
    GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));

虽然这一行看起来很奇怪

dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp))

你的意思是截断日期时间吗?如果是这样,请将其替换为:

CAST(transactionTimestamp as DATE)

答案 3 :(得分:0)

在你的方法中,最后一行"GROUP BY dateadd(DAY, 0, datediff(DAY, 0, transactionTimestamp));不是必需的,我猜..

看起来你只需要按支付类型和交易日进行分组......类似

Select sum(amnt),payment_type,transactionDay from PaymentRecords group by payment_type,transactionDay

注意:根据您的需要格式化transactionDay。