加入并计算3个查询

时间:2019-05-07 08:52:28

标签: sql

我有3个sql查询,条件和规则不同,但输出相同:

查询1:

Select CONVERT(char(10), DatePayment, 120) PaymentDate, IsNull(SUM(UnitPrice * Quantity), 0) POSAmount 
From POSSales P 
Inner Join POSSalesDetails PD On P.ID = PD.SalesID 
Inner Join Payments PAY On P.PaymentID = PAY.ID 
Inner Join POSItems PDI On PD.ItemID = PDI.ID 
Where DatePayment >= '2019-04-01 08:00' And 
DatePayment <= '2019-04-30 08:00'
group by CONVERT(char(10), DatePayment, 120)
order by CONVERT(char(10), DatePayment, 120)

样本输出

----------------------------
| PaymentDate  |  POSAmount|
---------------------------
| 2019-05-01   |    510.00 |
| 2019-05-02   |    120.00 |
| ........     | ........  |
----------------------------

Query2:

Select CONVERT(char(10), DatePayment, 120) PaymentDate, 
IsNull(SUM(TotalAmount), 0) ShowerAmount 
From ShowerBookings S 
Inner Join ShowerPayments SP On S.ID = SP.BookingID 
Inner Join Payments PAY On SP.PaymentID = PAY.ID 
Where 
DatePayment >= '2019-04-01 08:00' And 
DatePayment <= '2019-04-30 08:00' 
group by CONVERT(char(10), DatePayment, 120) 
order by CONVERT(char(10), DatePayment, 120)

样本输出

----------------------------
| PaymentDate  |ShowerAmount|
----------------------------
| 2019-05-01   |    220.00  |
| 2019-05-02   |    310.00  |
| ........     | ........   |
-----------------------------

Query3:

Select CONVERT(char(10), DatePayment, 120) PaymentDate, 
IsNull(SUM(TotalAmount), 0) LockerAmount 
From LockerBookings L 
Inner Join LockerPayments LP On L.ID = LP.BookingID 
Inner Join Payments PAY On LP.PaymentID = PAY.ID 
Where 
DatePayment >= '2019-04-01 08:00' And 
DatePayment <= '2019-04-30 08:00' 
group by CONVERT(char(10), DatePayment, 120) 
order by CONVERT(char(10), DatePayment, 120)

样本输出

----------------------------
| PaymentDate  |LockerAmount|
----------------------------
| 2019-05-01   |    150.00  |
| 2019-05-02   |    180.00  |
| ........     | ........   |
-----------------------------

和我的最终输出。.我想求和(Amount1 + Amount2 + Amount3)并按PaymentDate分组。

我尝试了以下操作,但不起作用

select Q1.PaymentDate, sum(Q1.Amount1 + Q2.Amount2 + Q3.Amount3) as TotalAmount
from (Query1) as Q1, (Query2) as Q2, (Query3) as Q3

我期望这样的结果:

预期输出

----------------------------
| PaymentDate  |  TAmount  |
---------------------------
| 2019-05-01   |    880.00 |
| 2019-05-02   |    610.00 |
| ........     | ........  |
----------------------------

如果可以那样生活,生活会容易得多-_- 感谢您的帮助和协助。 谢谢

注意 我更新了查询以更好地了解为何需要全部金额。我计划不更改主查询,因为它将被其他功能使用。因此,我需要提出新的查询来获取所有3个查询的总数。

3 个答案:

答案 0 :(得分:3)

您的3个查询很陌生,可以在没有任何聚合函数的情况下使用GROUP BY成功运行吗?

您期望的查询应具有以下结构:

SELECT col1, SUM(col2) AS TAmount  
FROM
(
    query_1  --SELECT col1, col2, ...
    UNION ALL
    query_2  --SELECT same number of columns as query_1. 
             -- Name of columns could be any name, but using UNION ALL, in output the DBMS will only use names of columns in query_1 
    UNION ALL
    query_3  --SELECT same number of columns as query_1. 
)
GROUP BY col1
ORDER BY col1;

因此,对于您编辑的问题,最终查询将是:

SELECT PaymentDate, SUM(amount) AS TAmount  
FROM
(
    SELECT CONVERT(CHAR(10), DatePayment, 120) AS PaymentDate, 
        IsNull(SUM(UnitPrice * Quantity), 0) AS amount
    FROM POSSales P 
    INNER JOIN POSSalesDetails PD ON P.ID = PD.SalesID 
    INNER JOIN Payments PAY ON P.PaymentID = PAY.ID 
    INNER JOIN POSItems PDI ON PD.ItemID = PDI.ID 
    WHERE DatePayment >= '2019-04-01 08:00' 
        AND  DatePayment <= '2019-04-30 08:00'
    GROUP BY CONVERT(CHAR(10), DatePayment, 120)

    UNION ALL

    SELECT CONVERT(CHAR(10), DatePayment, 120) AS PaymentDate, 
        IsNull(SUM(TotalAmount), 0) --AS amount
    FROM ShowerBookings S 
    INNER JOIN ShowerPayments SP ON S.ID = SP.BookingID 
    INNER JOIN Payments PAY ON SP.PaymentID = PAY.ID 
    WHERE 
        DatePayment >= '2019-04-01 08:00' 
        AND  DatePayment <= '2019-04-30 08:00' 
    GROUP BY CONVERT(CHAR(10), DatePayment, 120) 

    UNION ALL

    SELECT CONVERT(CHAR(10), DatePayment, 120) AS PaymentDate, 
        IsNull(SUM(TotalAmount), 0) --AS amount
    FROM LockerBookings L 
    INNER JOIN LockerPayments LP ON L.ID = LP.BookingID 
    INNER JOIN Payments PAY ON LP.PaymentID = PAY.ID 
    WHERE 
        DatePayment >= '2019-04-01 08:00' 
        AND  DatePayment <= '2019-04-30 08:00' 
    GROUP BY CONVERT(CHAR(10), DatePayment, 120) 
)
GROUP BY PaymentDate
ORDER BY PaymentDate;

答案 1 :(得分:2)

这应该按照您的原始组件进行合并。内部我只有日期和按日期分组的总和,因此每个内部都已经在最终外部查询之前进行了预查询

Select 
        CONVERT(char(10), PQ.DatePayment, 120) PaymentDate, 
        SUM(PQ.LineAmount) TotalAmount 
    from
        ( Select DatePayment, SUM(UnitPrice * Quantity) LineAmount 
            From POSSales P
                Join POSSalesDetails PD On P.ID = PD.SalesID 
                    Join POSItems PDI On PD.ItemID = PDI.ID 
                Inner Join Payments PAY On P.PaymentID = PAY.ID 
            Where DatePayment >= '2019-04-01 08:00' 
            And DatePayment <= '2019-04-30 08:00'
            group by DatePayment 
        UNION ALL
        Select DatePayment, SUM(TotalAmount) LineAmount 
            From ShowerBookings S 
                Join ShowerPayments SP On S.ID = SP.BookingID 
                    Join Payments PAY On SP.PaymentID = PAY.ID 
            Where 
                    DatePayment >= '2019-04-01 08:00' 
                And DatePayment <= '2019-04-30 08:00' 
            group by DatePayment
        UNION ALL
        Select DatePayment, SUM(TotalAmount) LineAmount 
            From LockerBookings L 
                Inner Join LockerPayments LP On L.ID = LP.BookingID 
                    Inner Join Payments PAY On LP.PaymentID = PAY.ID 
            Where 
                    DatePayment >= '2019-04-01 08:00' 
                And DatePayment <= '2019-04-30 08:00' 
            group by DatePayment ) PQ
    group by 
        CONVERT(char(10), PQ.DatePayment, 120) 
    order by 
        CONVERT(char(10), PQ.DatePayment, 120)

答案 2 :(得分:0)

您还可以使用视图。 从每个查询中创建一个视图,然后使用表之类的视图。