如何使用 EXIST 报告为完成的工作支付的所有发票与未结发票?

How to report all invoices paid on completed job vs open invoices using EXIST?

我想报告一个工作已完成,并且该工作的所有发票都已支付,return date/month 最后一张发票已支付以创建佣金报告。

    SELECT DISTINCT J.No_,  
    MAX(CLE.[Closed at Date]) AS 'FINAL INVOICE PAID', 
    SUM(CLE.[Closed by Amount (LCY)])

    FROM [Cust_ Ledger Entry] CLE LEFT JOIN [Job Ledger Entry] JLE ON CLE. 
    [Document No_] = JLE. 
    [Document No_] 
    LEFT JOIN [Job] J ON JLE.[Job No_] = J.No_

    WHERE [Salesperson Code] = 'JOHNDOE'
    AND CLE.[Document No_] LIKE 'SPI%'
    AND J.Blocked = 2

    AND    EXISTS  (SELECT CLE.[Closed at Date]
            WHERE CLE.[Closed at Date] = '1/1/1753')

    GROUP BY J.No_

"Here are the two tables I'm joining the Invoice Document on:" Job Ledger Entry Table

Job No_ Document No_    Description
J-4062  SPI14064    30% OF 8,015.00 DOWN PAYMENT WITH
J-4062  SPI14342    PROGRESS PAYMENT #2 - 30% OF 8,015.00 
J-4062  SPI14399    ADDITIONAL 30% OF 8,015.00 DUE WITH COMPLETION 
J-4062  SPI14432    PROGRESS PAYMENT #3 - REMAINING BALANCE ON 
J-4062  SPI14433    HIGH PILE ENGINEERING & FPE STAMP ,500.00
J-4062  SPI14433    ANCHOR INSEPCTION 0.00
J-4062  SPI14433    RACK PERMITTING CALCULATIONS, PLANS & SUBMITTAL,
J-4062  SPI14433    CITY OF FIRE PERMIT PLAN CHECK
J-4062  SPI14433    CITY OF BUILDING PERMIT PLAN CHECK 
J-4062  SPI14433    TOTAL PERMIT FEES: ,819.10
J-4062  SPI14433    FREIGHT
J-4071  SPI14194    RIGID SHELTER
J-4071  SPI14194    SHELTER CUSHION LEFT 
J-4071  SPI14194    SHELTER CUSHION RIGHT
J-4071  SPI14194    ABOVE MATERIAL- 50% DUE
J-4071  SPI14235    RIGID SHELTER
J-4071  SPI14235    SHELTER CUSHION LEFT 
J-4071  SPI14235    SHELTER CUSHION RIGHT
J-4071  SPI14235    ABOVE MATERIAL-REMAINING 50%
J-4071  SPI14406    INSTALLATION
    Customer Ledger Entry Table
    Document No_    Closed at Date
    SPI14064    2020-04-27 00:00:00.000
    SPI14194    2020-04-07 00:00:00.000
    SPI14235    2020-06-11 00:00:00.000
    SPI14342    2020-07-13 00:00:00.000
    SPI14399    2020-07-31 00:00:00.000
    SPI14406    2020-07-20 00:00:00.000
    SPI14432    2020-07-31 00:00:00.000
    SPI14433    1753-01-01 00:00:00.000

您需要一个子查询。

SELECT DISTINCT J.No_,  
MAX(CLE.[Closed at Date]) AS 'FINAL INVOICE PAID', 
SUM(CLE.[Closed by Amount (LCY)])
FROM [Cust_ Ledger Entry] CLE LEFT JOIN [Job Ledger Entry] JLE ON CLE.[Document No_] = JLE.[Document No_] 
LEFT JOIN [Job] J ON JLE.[Job No_] = J.No_
WHERE [Salesperson Code] = 'JOHNDOE'
AND CLE.[Document No_] LIKE 'SPI%'
AND J.Blocked = 2
AND CLE.[Document No_] IN (SELECT [Document No_] FROM [Cust_ Ledger Entry] WHERE [Closed at Date] = '1/1/1753')
GROUP BY J.No_