产生错误输出的日期的 MIN 和 MAX

MIN and MAX of dates producing wrong output

我有付款数据,我想根据不同的付款类型和 COUNT 次付款和调整显示 MIN 和 MAX 日期。

CREATE TABLE Payment
(BillingId int, PaymentId int, PaymentDate date, PaymentType varchar(50), PaymentBy varchar(50));

INSERT INTO Payment
VALUES
(12345, 1, '2022-02-02', 'payment', 'Insurance'),
(12345, 2, '2022-02-02', 'adjustment', 'Insurance'),
(67890, 3, '2022-01-19', 'payment', 'Insurance'),
(67890, 4, '2022-01-19', 'adjustment', 'Insurance'),
(67890, 5, '2022-01-19', 'payment', 'Insurance'),
(67890, 6, '2022-02-02', 'payment', 'Client')

SELECT * FROM Payment
BillingId PaymentId PaymentDate PaymentType PaymentBy
12345 1 2022-02-02 payment Insurance
12345 2 2022-02-02 adjustment Insurance
67890 3 2022-01-19 payment Insurance
67890 4 2022-01-19 adjustment Insurance
67890 5 2022-01-19 payment Insurance
67890 6 2022-02-02 payment Client

我需要按 BillingId 对数据进行分组并显示 6 个日期(在其中发布,特别是在 MIN 中)和 4 个总计数(它们产生正确的输出):

  1. 保险付款的最迟日期
  2. 保险公司付款的最大日期
  3. MIN 客户付款日期
  4. 客户付款的最大日期
  5. MIN 调整日期
  6. 进行调整的最大日期
  7. COUNT 笔所有付款(未经调整)
  8. 保险公司支付了 COUNT 笔款项
  9. 客户支付了 COUNT 笔款项
  10. COUNT 个调整量

P.S。例如,如果保险公司只支付了 1 笔款项,那么 MIN 和 MAX 日期应该相同。

我的代码:

SELECT p.BillingId, 

        MIN(CASE WHEN p.PaymentBy = 'Insurance' AND p.PaymentType != 'adjustment' THEN p.PaymentDate ELSE '' END) AS EarliestDateInsurancePaid,
        MAX(CASE WHEN p.PaymentBy = 'Insurance' AND p.PaymentType != 'adjustment'  THEN p.PaymentDate ELSE '' END) AS LatestDateInsurancePaid,
        MIN(CASE WHEN p.PaymentBy = 'Client' AND p.PaymentType != 'adjustment' THEN p.PaymentDate ELSE '' END) AS EarliestDateClientPaid,
        MAX(CASE WHEN p.PaymentBy = 'Client' AND p.PaymentType != 'adjustment' THEN p.PaymentDate ELSE '' END) AS LatestDateClientPaid,
        MIN(CASE WHEN p.PaymentType = 'adjustment' THEN p.PaymentDate ELSE '' END) AS EarliestAdjustmentDate,
        MAX(CASE WHEN p.PaymentType = 'adjustment' THEN p.PaymentDate ELSE '' END) AS LatestAdjustmentDate,

        COUNT(CASE WHEN p.PaymentType != 'adjustment' THEN p.PaymentType END) AS TotalAmountOfPayments,
        COUNT(CASE WHEN p.PaymentBy = 'Insurance' AND p.PaymentType != 'adjustment' THEN p.PaymentBy END) AS AmountOfInsurancePayments,
        COUNT(CASE WHEN p.PaymentBy = 'Client' AND p.PaymentType != 'adjustment' THEN p.PaymentBy END) AS AmountOfClientPayments, 
        COUNT(CASE WHEN p.PaymentType = 'adjustment' THEN p.PaymentType END) AS AmountOfAdjustments
            
FROM Payment AS p
GROUP BY p.BillingId

上述查询的输出:

BillingId EarliestDateInsurancePaid LatestDateInsurancePaid EarliestDateClientPaid LatestDateClientPaid EarliestAdjustmentDate LatestAdjustmentDate TotalAmountOfPayments AmountOfInsurancePayments AmountOfClientPayments AmountOfAdjustments
12345 1900-01-01 2022-02-02 1900-01-01 1900-01-01 1900-01-01 2022-02-02 1 1 0 1
67890 1900-01-01 2022-01-19 1900-01-01 2022-02-02 1900-01-01 2022-01-19 3 2 1 1

我需要的输出:

BillingId EarliestDateInsurancePaid LatestDateInsurancePaid EarliestDateClientPaid LatestDateClientPaid EarliestAdjustmentDate LatestAdjustmentDate TotalAmountOfPayments AmountOfInsurancePayments AmountOfClientPayments AmountOfAdjustments
12345 2022-02-02 2022-02-02 1900-01-01 1900-01-01 2022-02-02 2022-02-02 1 1 0 1
67890 2022-01-19 2022-01-19 2022-02-02 2022-02-02 2022-01-19 2022-01-19 3 2 1 1

您对 max(case when..else '' end... 的使用导致了 1900 日期,因为 max 函数不会忽略 empty/zero 长度的字符串,它们被转换为日期。使用 NULL(或没有 ELSE 部分,默认为 NULL); MAX 和许多其他聚合函数忽略 NULL 值。