四周数据 - 分组

时间:2015-07-05 23:26:29

标签: sql oracle date group-by

我有以下表格及其各自的数据:

FORECAST:
- ITEM
- FORECAST_WEEK_DATE
- FORECAST

SALES:
- ITEM
- SALES_WEEK_DATE
- SALES

WEEK_DATE列包含每个周末日期的记录,如: 2015年7月2日 15/02/2015 等等。

我想从两个表中得到结果,结果如下:

ITEM FORECAST_TOTAL SALES_TOTAL
001       23             45

最近4周的数据总数。

我如何实现这一目标?

1 个答案:

答案 0 :(得分: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 |