2016-06-01 14 views
0

私は以下のように表のように定義されています。分割による週の重み付け

DayNum | Day | Weight | Cumulative Weight 
1  | MON | 0.3 | 0.3 
2  | TUE | 0.15 | 0.45 (Sum of Mon and Tues) 
3  | WED | 0.1 | 0.55 (Sum of Mon and Tues and Wed) 
4  | THU | 0.1 | 0.65 
5  | FRI | 0.15 | 0.8 
6  | SAT | 0.2 | 1 

そして、以下のように毎週レベル(月 - 日)で定義された別の表に金額があります。

Item | Date  | Amount 
A | 30-May-16 | 10 ---- Week in May and June 
A | 6-Jun-16 | 20 
A | 13-Jun-16 | 30 and so on 
A | 27-Jun-16 | 60 ---- Week in Jun and July 

は今は2つの異なるヶ月( - 6月5日から5月30日まで上記の例では)との間で重複している週間、毎日のレベルで別のテーブルに挿入します。 誰でもこれをOracleでどのように実現できるか説明できますか?

出力は以下のようにする必要があります。

Item | Date  | Amount 
A | 30-May-16 | 4.5  (2 days from May which are Mon and Tues - so calculation is 10 * 0.45) 
A | 1-Jun-16 | 5.5  (5 days from May which is the rest of the week - 10 minus 4.5) 
A | 6-Jun-16 | 20 and so on 
A | 27-Jun-16 | 39  (4 days from June which are Mon till Thurs - so calculation is 60 * 0.65) 
A | 1-Jul-16 | 21  (3 days from July which is the rest of the week - 60 minus 39) 
+0

スクリプトを共有してくださいあなたは –

答えて

1

試してみてください。

WITH some_data AS(
    select a.*, 
      trunc(trunc(add_months("DATE", 1), 'MM') - "DATE") 
         As days_to_end_of_month, 
      trunc(add_months("DATE", 1), 'MM') 
         As start_of_next_month 
    from amounts a 
), some_other_data AS (
     SELECT some_data.*, 
      CASE WHEN days_to_end_of_month >= 6 THEN Amount 
        ELSE (SELECT some_data.amount * "Cumulative Weight" FROM Weights w 
         WHERE some_data.days_to_end_of_month = DayNum) 
      END as new_Amount 
     FROM some_data 
) 
SELECT Item, "DATE", New_Amount as amount 
FROM some_other_data 
UNION ALL 
SELECT Item, start_of_next_month, amount-new_amount 
FROM some_other_data 
WHERE days_to_end_of_month < 6 
ORDER BY "DATE" 
; 
+0

おかげで多くのことを試してみました。私はここに投稿する前に私のバージョンのクエリを書いて、2番目の句で大文字小文字を使用することに固執しました!! – user3123752

0

Oracleのセットアップ

CREATE TABLE Weights (DayNum, Day, Weight) AS 
SELECT 1, 'MON', 0.3 FROM DUAL UNION ALL 
SELECT 2, 'TUE', 0.15 FROM DUAL UNION ALL 
SELECT 3, 'WED', 0.1 FROM DUAL UNION ALL 
SELECT 4, 'THU', 0.1 FROM DUAL UNION ALL 
SELECT 5, 'FRI', 0.15 FROM DUAL UNION ALL 
SELECT 6, 'SAT', 0.2 FROM DUAL; 

CREATE TABLE weekly_levels (Item, "Date", Amount) AS 
SELECT 'A', DATE '2016-05-30', 10 FROM DUAL UNION ALL 
SELECT 'A', DATE '2016-06-06', 20 FROM DUAL UNION ALL 
SELECT 'A', DATE '2016-06-13', 30 FROM DUAL UNION ALL 
SELECT 'A', DATE '2016-06-27', 60 FROM DUAL; 

クエリ

SELECT item, 
     start_date, 
     SUM(amount * weight) AS amount 
FROM (
      SELECT item, 
       "Date" AS start_date, 
       LEAST("Date" + INTERVAL '6' DAY, LAST_DAY("Date")) AS end_date, 
       amount 
      FROM weekly_levels 
      UNION 
      SELECT item, 
       GREATEST("Date", TRUNC("Date" + INTERVAL '6' DAY, 'MM')) AS start_date, 
       "Date" + INTERVAL '6' DAY AS end_date, 
       amount 
      FROM weekly_levels 
     ) d 
     INNER JOIN 
     Weights w 
     ON (w.DayNum BETWEEN TO_CHAR(start_date, 'D') 
          AND TO_CHAR(end_date, 'D')) 
GROUP BY item, start_date 
ORDER BY item, start_date; 

出力

ITEM START_DATE    AMOUNT 
---- ------------------- ---------- 
A 2016-05-30 00:00:00  4.5 
A 2016-06-01 00:00:00  5.5 
A 2016-06-06 00:00:00   20 
A 2016-06-13 00:00:00   30 
A 2016-06-27 00:00:00   39 
A 2016-07-01 00:00:00   21 
+0

ありがとうございました。上記のクエリを実行すると、以下のような出力が得られます。 'アイテム開始日の金額 ------- ------------------ ------------- A 5/30/2016 2.5 A 6/27/2016 30 ' – user3123752

関連する問題