2015-07-05 2 views
-1

У меня есть следующие таблицы с их соответствующими данными:четыре недели данных - группы

FORECAST: 
- ITEM 
- FORECAST_WEEK_DATE 
- FORECAST 

SALES: 
- ITEM 
- SALES_WEEK_DATE 
- SALES 

Колонка week_date имеет записей для каждого конца-недели даты, как: 7/02/2015 15/02/2015 и так далее.

Я хочу, чтобы принести результаты с обеих таблиц, так что результат, как:

ITEM FORECAST_TOTAL SALES_TOTAL 
001  23    45 

для общей сложности последние 4 недели данных.

Как это достичь?

ответ

2

SQL Fiddle

Oracle 11g R2 Настройка схемы:

CREATE TABLE FORECAST (ITEM, FORECAST_WEEK_DATE, FORECAST) AS 
      SELECT 1, DATE '2015-01-01',      1 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '7' DAY, 2 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '14' DAY, 3 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '21' DAY, 4 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '28' DAY, 5 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '35' DAY, 6 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '42' DAY, 7 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01',      3 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '7' DAY, 1 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '14' DAY, 5 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '21' DAY, 8 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '28' DAY, 3 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '35' DAY, 2 FROM DUAL; 

CREATE TABLE SALES (ITEM, SALES_WEEK_DATE, SALES) AS 
      SELECT 1, DATE '2015-01-01',      3 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '7' DAY, 2 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '14' DAY, 1 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '21' DAY, 5 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '28' DAY, 3 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '35' DAY, 8 FROM DUAL 
UNION ALL SELECT 1, DATE '2015-01-01' + INTERVAL '42' DAY, 11 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01',      7 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '7' DAY, 1 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '14' DAY, 4 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '21' DAY, 3 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '28' DAY, 6 FROM DUAL 
UNION ALL SELECT 2, DATE '2015-01-01' + INTERVAL '35' DAY, 8 FROM DUAL; 

Запрос 1:

Это позволит получить данные для последних 4-х недель в таблице для каждого элемента:

WITH ordered_week_data AS (
SELECT f.ITEM, 
     ROW_NUMBER() OVER (PARTITION BY f.ITEM ORDER BY FORECAST_WEEK_DATE DESC) AS RN, 
     f.FORECAST_WEEK_DATE AS WEEK_DATE, 
     FORECAST, 
     SALES 
FROM FORECAST f 
     INNER JOIN 
     SALES s 
     ON (f.ITEM = s.ITEM AND f.FORECAST_WEEK_DATE = s.SALES_WEEK_DATE) 
) 
SELECT ITEM, 
     MAX(WEEK_DATE) AS LAST_WEEK_DATE, 
     SUM(FORECAST) AS FORECAST_TOTAL, 
     SUM(SALES) AS SALES_TOTAL 
FROM ordered_week_data 
WHERE RN <= 4 
GROUP BY 
     ITEM 

Results:

| ITEM |    LAST_WEEK_DATE | FORECAST_TOTAL | SALES_TOTAL | 
|------|----------------------------|----------------|-------------| 
| 1 | February, 12 2015 00:00:00 |    22 |   27 | 
| 2 | February, 05 2015 00:00:00 |    18 |   21 |