2016-07-29 16 views
1

SQL Server 2014を使用しています。顧客または地域別にパーティション化またはグループ化された日付の範囲で合計(合計)を集計する必要があります。重要なのは、すべての調整金額を取得して、請求処理日に適用するときにそれらを合計することです。SQL Serverでの日付範囲の集計

最後の請求日以降で、次の請求日よりも短い調整は、合計して請求額と一緒にうまく表示する必要があります。

例を参照してください:私が見たいのは何

+------------------+------------+------------+------------------+--------------------+ 
| TRANSACTION_TYPE | CUSTOMERID | LOCATIONID | TRANSACTION DATE | TRANSACTION AMOUNT | 
+------------------+------------+------------+------------------+--------------------+ 
| bill    | 215  | 102  | 7/7/2016   | $100.00   | 
| bill    | 215  | 102  | 6/6/2016   | $121.00   | 
| adj    | 215  | 102  | 6/1/2016   | $22.00    | 
| adj    | 215  | 102  | 5/8/2016   | $0.35    | 
| adj    | 215  | 102  | 5/7/2016   | $5.00    | 
| bill    | 215  | 102  | 5/6/2016   | $115.00   | 
| bill    | 215  | 102  | 4/7/2016   | $200.00   | 
| adj    | 215  | 102  | 4/2/2016   | $4.35    | 
| adj    | 215  | 102  | 4/1/2016   | $(0.50)   | 
| adj    | 215  | 102  | 3/28/2016  | $33.00    | 
| bill    | 215  | 102  | 3/28/2016  | $75.00    | 
| adj    | 215  | 102  | 3/5/2016   | $0.33    | 
| bill    | 215  | 102  | 3/3/2016   | $99.00    | 
+------------------+------------+------------+------------------+--------------------+ 

は以下の通りです:

+------------------+------------+------------+------------------+-------------+-------------------+ 
| TRANSACTION_TYPE | CUSTOMERID | LOCATIONID | TRANSACTION DATE | BILL AMOUNT | ADJUSTMENT AMOUNT | 
+------------------+------------+------------+------------------+-------------+-------------------+ 
| bill    | 215  | 102  | 7/7/2016   | $100.00  | $-    | 
| bill    | 215  | 102  | 6/6/2016   | $121.00  | $27.35   | 
| bill    | 215  | 102  | 5/6/2016   | $115.00  | $-    | 
| bill    | 215  | 102  | 4/7/2016   | $200.00  | $36.85   | 
| bill    | 215  | 102  | 3/28/2016  | $75.00  | $0.33    | 
| bill    | 215  | 102  | 3/3/2016   | $99.00  | $-    | 
+------------------+------------+------------+------------------+-------------+-------------------+ 

答えて

0

あなたがする必要がある:

  • 最初の2つ(仮想)としてテーブルを想像しますトランザクション・タイプのサブ表
  • LEAD機能を使用して、適用する調整の日付範囲を取得します。
  • 最後にeft joinを実行します。以下

テストされていないSQL:

with 
BillData as (
    select 
     TransactionType, 
     CustomerID, 
     LocationID, 
     TransactionDate, 
     TransactionAmount, 
     lead(TransactionDate, 1) over (partition by CustomerID 
             order by TransactionDate) as NextDate 
    from @data bill 
    where TransactionType = 'bill' 
), 
AdjData as (
    select 
     CustomerID, 
     TransactionDate, 
     sum(TransactionAmount) as AdjAmount 
    from @data adj 
    where TransactionType = 'adj' 
) 
select 
    bill.TransactionType, 
    bill.CustomerID, 
    bill.LocationID, 
    bill.TransactionDate, 
    sum(TransactionAmount) as BillAmount, 
    sum(AdjAmount)   as AdjAmount 
from BillData bill 
left join AdjData adj 
    on adj.CustomerID = bill.CustomerID 
    and bill.TransactionDate <= adj.TransactionDate 
    and adj.TransactionDate < bill.NextDate 
group by 
    bill.TransactionType, 
    bill.CustomerID, 
    bill.LocationID, 
    bill.TransactionDate 
; 
+1

感謝。クエリは正確には機能しませんでしたが、正しい方向に私を指していましたので、これを正しい答えとして受け入れます。再度、感謝します! – shawno

0

これは私がやってしまったものです:あなたの助けのための

 select 
     bill.TransactionType, 
     bill.CustomerID, 
     bill.LocationID, 
     bill.TransactionDate, 
     TransactionAmount as BillAmount, 
     sum(AdjAmount)   as AdjAmount 
    from 
    (
     select 
      TransactionType, 
      CustomerID, 
      LocationID, 
      TransactionDate, 
      TransactionAmount, 
      lag(TransactionDate, 1) over (partition by CustomerID, LocationID 
              order by TransactionDate) as PreviousDate --NextDate 
     from test1 
     where TransactionType = 'bill' 
    ) as bill 
    left join 
    (
     select 
      CustomerID, 
      LocationID, 
      TransactionDate, 
      TransactionAmount as AdjAmount 
     from test1 
     where TransactionType = 'adj' 
    ) as adj 
    ON 
     adj.CustomerID = bill.CustomerID 
     and adj.LocationID = bill.LocationID 
    and adj.TransactionDate >= bill.PreviousDate 
    and adj.TransactionDate < bill.TransactionDate 
    group by 
     bill.TransactionType, 
     bill.CustomerID, 
     bill.LocationID, 
     bill.TransactionDate, 
     bill.TransactionAmount 
    order by 4 desc 
関連する問題