1

現在実行中の倉庫管理システムをクエリする SSRS レポートからの次の TSQL クエリがあります。このクエリを記述するより良い方法はありますか? このタイプのクエリを記述するより良い方法があるはずですが、現時点ではそれがわかりません。

このクエリは、未払いの請求額を週ごとの列に入れます。

SELECT ContCode, TradingName, DaysFromInvoice, CreditLimit,
    CASE WHEN [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+] < 0 THEN [0-7] + [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+]
        WHEN [8-14] + [15-21] + [22-28] + [29-35] + [36-42] < 0 THEN [0-7] + [8-14] + [15-21] + [22-28] + [29-35] + [36-42]
        WHEN [8-14] + [15-21] + [22-28] + [29-35] < 0 THEN [0-7] + [8-14] + [15-21] + [22-28] + [29-35]
        WHEN [8-14] + [15-21] + [22-28] < 0 THEN [0-7] + [8-14] + [15-21] + [22-28]
        WHEN [8-14] + [15-21] < 0 THEN [0-7] + [8-14] + [15-21]
        WHEN [8-14] < 0 THEN [0-7] + [8-14]
        WHEN [0-7] = 0 AND [0-7] + [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+] = 0 AND Total <> 0 THEN Total 
        ELSE [0-7] END [0-7],  
    CASE WHEN [15-21] + [22-28] + [29-35] + [36-42] + [43+] < 0 AND [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+] < 0 THEN 0
        WHEN [15-21] + [22-28] + [29-35] + [36-42] + [43+] < 0 AND [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+] >= 0 THEN [8-14] + [15-21] + [22-28] + [29-35] + [36-42] + [43+]
        WHEN [15-21] + [22-28] + [29-35] + [36-42] < 0 AND [8-14] + [15-21] + [22-28] + [29-35] + [36-42] >= 0 THEN [8-14] + [15-21] + [22-28] + [29-35] + [36-42]
        WHEN [15-21] + [22-28] + [29-35] < 0 AND [8-14] + [15-21] + [22-28] + [29-35] >= 0 THEN [8-14] + [15-21] + [22-28] + [29-35]
        WHEN [15-21] + [22-28] < 0 AND [8-14] + [15-21] + [22-28] >= 0 THEN [8-14] + [15-21] + [22-28]
        WHEN [15-21] < 0 AND [8-14] + [15-21] >= 0 THEN [8-14] + [15-21]
        WHEN [8-14] < 0 THEN 0
        ELSE [8-14] END [8-14],  
    CASE WHEN [22-28] + [29-35] + [36-42] + [43+] < 0 AND [15-21] + [22-28] + [29-35] + [36-42] + [43+] < 0 THEN 0
        WHEN [22-28] + [29-35] + [36-42] + [43+] < 0 AND [15-21] + [22-28] + [29-35] + [36-42] + [43+] >= 0 THEN [15-21] + [22-28] + [29-35] + [36-42] + [43+]
        WHEN [22-28] + [29-35] + [36-42] < 0 AND [15-21] + [22-28] + [29-35] + [36-42] >= 0 THEN [15-21] + [22-28] + [29-35] + [36-42]
        WHEN [22-28] + [29-35] < 0 AND [15-21] + [22-28] + [29-35] >= 0 THEN [15-21] + [22-28] + [29-35]
        WHEN [22-28] < 0 AND [15-21] + [22-28] >= 0 THEN [15-21] + [22-28]
        WHEN [15-21] < 0 THEN 0
        ELSE [15-21] END [15-21],  
    CASE WHEN [29-35] + [36-42] + [43+] < 0 AND [22-28] + [29-35] + [36-42] + [43+] < 0 THEN 0
        WHEN [29-35] + [36-42] + [43+] < 0 AND [22-28] + [29-35] + [36-42] + [43+] >= 0 THEN [22-28] + [29-35] + [36-42] + [43+]
        WHEN [29-35] + [36-42] < 0 AND [22-28] + [29-35] + [36-42] >= 0 THEN [22-28] + [29-35] + [36-42]
        WHEN [29-35] < 0 AND [22-28] + [29-35] >= 0 THEN [22-28] + [29-35]
        WHEN [22-28] < 0 THEN 0
        ELSE [22-28] END [22-28],  
    CASE WHEN [36-42] + [43+] < 0 AND [29-35] + [36-42] + [43+] < 0 THEN 0
        WHEN [36-42] + [43+] < 0 AND [29-35] + [36-42] + [43+] >= 0 THEN [29-35] + [36-42] + [43+]
        WHEN [36-42] < 0 AND [29-35] + [36-42] >= 0 THEN [29-35] + [36-42]
        WHEN [29-35] < 0 THEN 0
        ELSE [29-35] END [29-35], 
    CASE WHEN [36-42] + [43+] < 0 AND [36-42] + [43+] < 0 THEN 0
        WHEN [36-42] + [43+] < 0 THEN 0 
        WHEN [43+] < 0 THEN [36-42] + [43+]
        ELSE [36-42] END [36-42], 
    CASE WHEN [43+] < 0 THEN 0 ELSE [43+] END [43+],
    Total
FROM
(
    SELECT ContGnrl.ContCode,ContGnrl.TradingName,ContBals.DaysFromInvoice, ContAcct.CreditLimit,
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) < 8 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [0-7],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) BETWEEN 8 AND 14 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [8-14],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) BETWEEN 15 AND 21 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [15-21],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) BETWEEN 22 AND 28 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [22-28],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) BETWEEN 29 AND 35 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [29-35],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) BETWEEN 36 AND 42 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) [36-42],
        COALESCE(SUM(CASE WHEN DATEDIFF(day, ContTran.TranDate, cast(convert(varchar(10), getdate(), 102) as datetime)) >= 43 AND ContTran.TranType <> 'P' THEN ContTran.Outstanding END), 0) 
        + COALESCE(SUM(CASE WHEN ContTran.TranType = 'P' THEN ContTran.Outstanding END), 0) [43+],
        COALESCE(SUM(ContTran.Outstanding), 0) Total
    FROM ContGnrl 
        JOIN ContBals 
            ON ContGnrl.ContCode = ContBals.ContCode 
        JOIN ContAcct 
            ON ContBals.ContCode = ContAcct.ContCode 
        LEFT JOIN ContTran
            ON ContGnrl.ContCode = ContTran.ContCode
                AND (ContTran.Finalised = 0) 
                AND (ContTran.TranType IN ('I','C','J','P','D'))

    GROUP BY ContGnrl.ContCode,ContGnrl.TradingName,ContBals.DaysFromInvoice, ContAcct.CreditLimit
    HAVING COALESCE(SUM(ContTran.Outstanding), 0) <> 0
) t
ORDER BY TradingName
4

0 に答える 0