MS Access 中使用 sum、group by 和 order by 进行 SQL 查询左连接

问题描述 投票:0回答:1

我正在尝试在 MS Access 中使用

left join
sum
group by
order by
子句执行 SQL 查询。

但结果不正确 - 也许我使用的 SQL 代码是错误的。

桌子

Expense

ID  DATE        INVONO      TRANSACTION TOTEXP
----------------------------------------------
1   29-08-2024  EXP-1001    EXPENSE 25000
2   30-08-2024  EXP-1002    EXPENSE 25000
3   29-09-2024  EXP-1003    EXPENSE 30000
5   29-09-2024  EXP-1004    EXPENSE 30000

桌子

Invoice

DATE        INVONO      TRANSACTION    TOTPRP   TOTPRS  PAYMENT
---------------------------------------------------------------
29-08-2024  SALES-1000  SALES           100000  150000  150000
30-08-2024  SALES-1001  SALES           300000  350000  350000
29-09-2024  SALES-1002  SALES           200000  250000  250000
29-09-2024  SALES-1003  SALES           200000  250000  250000
30-09-2024  SALES-1004  SALES           250000  300000  
30-09-2024  SALES-1005  SALES           250000  300000  

我的询问:

SELECT 
    Invoice.Date AS [DATE],
    SUM(Invoice.TotPRP) AS TOTPRP, 
    SUM(Invoice.TotPRS) AS TOTPRS, 
    SUM(Invoice.PAYMENT) AS PAYMENT, 
    SUM(Expense.Totexp) AS TOTEXP, 
    SUM(Invoice.TotPRS) - SUM(Invoice.TotPRP) AS TOTRESULT,
    SUM(Invoice.PAYMENT) - SUM(Invoice.TotPRS) AS TOTOUTSTANDING,
    SUM(Invoice.PAYMENT) - (SUM(Invoice.TotPRP) + SUM(Expense.TotEXP)) AS TOTPROFITNET
FROM 
    Invoice 
LEFT JOIN 
    Expense ON Invoice.Date = Expense.Date
GROUP BY 
    Invoice.Date, Invoice.INVONO
ORDER BY 
    Invoice.Date;

我的 SQL 查询结果:

日期 TOTPRP TOTPRS 付款 TOTEXP 全部结果 非常出色 TOTPROFITNET
2024年8月29日 100000 150000 150000 25000 50000 0 25000
2024年9月29日 400000 500000 500000 60000 100000 0 40000
2024年9月29日 400000 500000 500000 60000 100000 0 40000
2024年9月29日 300000 350000 350000 25000 50000 0 25000
2024年9月29日 250000 300000 50000
2024年9月29日 250000 300000 50000

是否可以将字段

date
应用于
period

想要的结果

期间 TOTPRP TOTPRS 付款 TOTEXP 全部结果 非常出色 TOTPROFITNET
8 月 24 日 400000 500000 500000 50000 100000 0 50000
9 月 24 日 900000 1100000 500000 60000 200000 -600000 -460000
sql ms-access group-by left-join sql-order-by
1个回答
0
投票
WITH InvoiceSummary AS (
    SELECT 
        FORMAT(DATE, 'yy-MMM') AS Period,
        SUM(TotPRP) AS TOTPRP, 
        SUM(TotPRS) AS TOTPRS, 
        SUM(PAYMENT) AS PAYMENT
    FROM 
        Invoice 
    GROUP BY 
        FORMAT(DATE, 'yy-MMM')
),
ExpenseSummary AS (
    SELECT 
        FORMAT(DATE, 'yy-MMM') AS Period,
        SUM(TotEXP) AS TOTEXP
    FROM 
        Expense 
    GROUP BY 
        FORMAT(DATE, 'yy-MMM')
)

SELECT 
    ISNULL(i.Period, e.Period) AS Period,
    ISNULL(i.TOTPRP, 0) AS TOTPRP, 
    ISNULL(i.TOTPRS, 0) AS TOTPRS, 
    ISNULL(i.PAYMENT, 0) AS PAYMENT, 
    ISNULL(e.TOTEXP, 0) AS TOTEXP, 
    ISNULL(i.TOTPRS, 0) - ISNULL(i.TOTPRP, 0) AS TOTRESULT,
    ISNULL(i.PAYMENT, 0) - ISNULL(i.TOTPRS, 0) AS TOTOUTSTANDING,
    ISNULL(i.PAYMENT, 0) - (ISNULL(i.TOTPRP, 0) + ISNULL(e.TOTEXP, 0)) AS TOTPROFITNET
FROM 
    InvoiceSummary i
FULL OUTER JOIN 
    ExpenseSummary e ON i.Period = e.Period
ORDER BY 
    Period;

Result
| Period | TOTPRP  | TOTPRS  | PAYMENT  | TOTEXP  | TOTRESULT | TOTOUTSTANDING | TOTPROFITNET |
|--------|---------|---------|----------|---------|-----------|----------------|---------------|
| 24-Aug | 400000  | 500000  | 500000   | 50000   | 100000    | 0              | 50000         |
| 24-Sep | 900000  | 1100000 | 500000   | 60000   | 200000    | -600000        | -460000       |

© www.soinside.com 2019 - 2024. All rights reserved.