Я использовал предыдущий пример стека, чтобы начать, но у меня проблемы с суммированием итоговых данных. Извините, это долго, но все это актуально.
SELECT DISTINCT TRIM(RMCUST.REP_CODE) AS 'Rep',
RMCUST.CUST_NAME AS 'Customer',
FORMAT(CASE WHEN DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) <= 30
THEN SUM(RMDET.TRAN_AMT)
OVER (PARTITION BY RMCUST.CUST_NAME)
ELSE 0
END, 'c') AS '0 - 30',
FORMAT(CASE WHEN DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) > 30 AND DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) <= 60
THEN SUM(RMDET.TRAN_AMT)
OVER (PARTITION BY RMCUST.CUST_NAME)
ELSE 0
END, 'c') AS '31 - 60',
FORMAT(CASE WHEN DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) > 60 AND DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) <= 90
THEN SUM(RMDET.TRAN_AMT)
OVER (PARTITION BY RMCUST.CUST_NAME)
ELSE 0
END, 'c') AS '61 - 90',
FORMAT(CASE WHEN DATEDIFF(DD, RMDET.TRAN_DATE, GETDATE()) > 90
THEN SUM(RMDET.TRAN_AMT)
OVER (PARTITION BY RMCUST.CUST_NAME)
ELSE 0
END, 'c') AS '>90'
FROM RMDET (NOLOCK)
INNER JOIN RMCUST (NOLOCK) ON RMCUST.CUST_CODE = RMDET.CUST_CODE
WHERE RMDET.TRAN_AMT_APPLIED = 0
GROUP BY RMCUST.REP_CODE, RMCUST.CUST_NAME, RMDET.TRAN_DATE, RMDET.TRAN_AMT
ORDER BY RMCUST.CUST_NAME
Вывод:
Rep Customer 0 - 30 31 - 60 61 - 90 >90
------------------------------------------------------------------------
KG 135 PRIME $0.00 $0.00 $462.66 $0.00
AS 71 ABOVE $2,469.60 $0.00 $0.00 $0.00
KH ABERNETHYS MUSIC CENTER $0.00 $0.00 $1,014.60 $0.00
KH ABERNETHYS MUSIC CENTER $1,014.60 $0.00 $0.00 $0.00
BH AC HOTEL $132.28 $0.00 $0.00 $0.00
AS ACE HOTEL $0.00 $0.00 $0.00 ($0.91)
AS AKASHA $0.00 $0.00 $12,052.06 $0.00
AS AKASHA $0.00 $12,052.06 $0.00 $0.00
AS AKASHA $12,052.06 $0.00 $0.00 $0.00
Вот необработанные данные из таблицы. Запрос помещает общую сумму задолженности во все столбцы, когда он должен показывать только одну строку с разными итогами для каждого столбца в зависимости от даты. Куда я иду не так? Спасибо.
Rep Customer Date Invoice # 0 - 30 31 - 60 61 - 90 >90
------------------------------------------------------------------------------------
KG 135 PRIME 12/20/2019 870 $0.00 $0.00 $462.66 $0.00
AS 71 ABOVE 02/14/2020 5240 $397.60 $0.00 $0.00 $0.00
AS 71 ABOVE 02/15/2020 5341 $978.60 $0.00 $0.00 $0.00
AS 71 ABOVE 02/17/2020 5423 $427.00 $0.00 $0.00 $0.00
AS 71 ABOVE 02/18/2020 5529 $509.60 $0.00 $0.00 $0.00
AS 71 ABOVE 02/19/2020 5608 $156.80 $0.00 $0.00 $0.00
KH ABERNETHYS MUSIC CENTER 12/19/2019 721 $0.00 $0.00 $450.50 $0.00
KH ABERNETHYS MUSIC CENTER 02/19/2020 5657 $180.20 $0.00 $0.00 $0.00
KH ABERNETHYS MUSIC CENTER 12/05/2019 235884 $0.00 $0.00 $383.90 $0.00
BH AC HOTEL 02/13/2020 5181 $30.21 $0.00 $0.00 $0.00
BH AC HOTEL 02/18/2020 5593 $102.07 $0.00 $0.00 $0.00
AS ACE HOTEL 03/14/2018 193386 $0.00 $0.00 $0.00 ($0.72)
AS ACE HOTEL 04/30/2018 CREDITS-09 $0.00 $0.00 $0.00 ($0.19)
AS AKASHA 01/02/2020 1789 $0.00 $128.70 $0.00 $0.00
AS AKASHA 01/04/2020 1959 $0.00 $223.37 $0.00 $0.00
AS AKASHA 01/07/2020 2141 $0.00 $161.95 $0.00 $0.00
AS AKASHA 01/08/2020 2218 $0.00 $102.00 $0.00 $0.00
AS AKASHA 01/09/2020 2326 $0.00 $444.00 $0.00 $0.00
AS AKASHA 01/10/2020 2416 $0.00 $596.82 $0.00 $0.00
AS AKASHA 01/11/2020 2531 $0.00 $462.11 $0.00 $0.00
AS AKASHA 01/14/2020 2701 $0.00 $303.10 $0.00 $0.00
AS AKASHA 01/16/2020 2879 $0.00 $804.24 $0.00 $0.00
AS AKASHA 01/17/2020 2989 $0.00 $442.11 $0.00 $0.00
AS AKASHA 01/18/2020 3080 $0.00 $95.47 $0.00 $0.00
AS AKASHA 01/20/2020 3181 $225.86 $0.00 $0.00 $0.00
AS AKASHA 01/22/2020 3366 $145.02 $0.00 $0.00 $0.00
AS AKASHA 01/23/2020 3451 $126.42 $0.00 $0.00 $0.00
AS AKASHA 01/24/2020 3544 $510.90 $0.00 $0.00 $0.00
AS AKASHA 01/25/2020 3656 $227.76 $0.00 $0.00 $0.00
AS AKASHA 01/27/2020 3733 $156.88 $0.00 $0.00 $0.00
AS AKASHA 01/29/2020 3914 $264.37 $0.00 $0.00 $0.00
AS AKASHA 01/30/2020 4006 $705.27 $0.00 $0.00 $0.00
AS AKASHA 01/31/2020 4106 $184.20 $0.00 $0.00 $0.00
AS AKASHA 02/01/2020 4222 $180.30 $0.00 $0.00 $0.00
AS AKASHA 02/04/2020 4387 $209.59 $0.00 $0.00 $0.00
AS AKASHA 02/05/2020 4462 $259.76 $0.00 $0.00 $0.00
AS AKASHA 02/06/2020 4572 $276.91 $0.00 $0.00 $0.00
AS AKASHA 02/07/2020 4666 $558.18 $0.00 $0.00 $0.00
AS AKASHA 02/08/2020 4782 $249.31 $0.00 $0.00 $0.00
AS AKASHA 02/11/2020 4866 $395.93 $0.00 $0.00 $0.00
AS AKASHA 02/13/2020 5108 $467.89 $0.00 $0.00 $0.00
AS AKASHA 02/14/2020 5220 $125.63 $0.00 $0.00 $0.00
AS AKASHA 02/15/2020 5367 $173.64 $0.00 $0.00 $0.00
AS AKASHA 02/17/2020 5449 $355.40 $0.00 $0.00 $0.00
AS AKASHA 02/18/2020 5548 $220.65 $0.00 $0.00 $0.00
AS AKASHA 02/19/2020 5636 $608.92 $0.00 $0.00 $0.00
AS AKASHA 12/04/2019 235786 $0.00 $0.00 $299.46 $0.00
AS AKASHA 12/04/2019 235806 $0.00 $0.00 $150.18 $0.00
AS AKASHA 12/06/2019 236042 $0.00 $0.00 $954.18 $0.00
AS AKASHA 12/07/2019 236108 $0.00 $0.00 $310.82 $0.00
AS AKASHA 12/10/2019 236300 $0.00 $0.00 $70.50 $0.00
AS AKASHA 12/31/2019 CREDITS-08 $0.00 ($125.74) $0.00 $0.00