Oracle - 返回定期订购的产品

时间:2014-03-14 17:00:59

标签: sql oracle select count where

我在编写表达式时遇到问题,该表达式将显示订单之间差距不超过一年的产品。因此,我可以将它们归类为常规订单。澄清我不想退回一年内订购超过一次的产品。

这是我的两张桌子:

SALES FACT TABLE
TIME_KEY                      PRODUCT_KEY BRANCH_KEY LOCATION_KEY POUNDS_SOLD  AVG_SALES UNITS_SOLD
----------------------------- ----------- ---------- ------------ ----------- ---------- ----------
22-DEC-13 08.31.18.442000000            2 B1         L19             21542.39    10821.2        100 
21-DEC-10 21.19.37.182000000            3 B8         L5                 65487    32793.5        100 
13-SEP-13 06.36.03.720000000            7 B2         L15             78541.84   39470.92        400 
24-JUN-13 12.21.45.186000000            1 B7         L13                94115    47167.5        220 
18-SEP-07 12.58.06.873000000            8 B2         L2                 54000      27250        500 
11-FEB-11 18.06.08.475000000            8 B9         L6                 11123     5636.5        150 
28-SEP-13 15.06.20.153000000            6 B3         L16             45896.31   23008.16        120 
22-DEC-08 19.34.48.490000000            5 B6         L3              87451.01   43875.51        300 
23-JUL-13 20.08.51.173000000            6 B6         L14                69542      34971        400 
20-DEC-13 22.47.24.962000000            9 B4         L17             21584.39    10872.2        160 
21-DEC-06 19.11.50.472000000            5 B10        L1                 10000      27250        500 
13-MAR-13 14.13.58.555000000            1 B2         L11                62413      31256         99 
06-MAR-13 18.15.40.365000000            4 B6         L10                94785    47542.5        300 
20-DEC-13 23.35.12.683000000            2 B5         L18             52359.19    26289.6        220 
15-MAR-13 19.11.58.459000000            4 B9         L12             66499.84   33299.92        100 
19-DEC-11 13.17.34.443000000            9 B2         L7                 51449    26049.5        650 
14-FEB-12 10.20.20.787000000           10 B5         L8                 66589    33394.5        200 
19-DEC-09 10.09.41.844000000            3 B7         L4                 99125    49687.5        250 
22-MAR-12 19.36.24.790000000           10 B2         L9              62331.66   31765.83       1200 
11-JAN-14 19.18.58.595000000            7 B8         L20             35214.85   17667.43        120

PRODUCT DIMENSION TABLE
PRODUCT_KEY PRODUCT_NAME              BRAND                TYPE       SUPPLIER_TYPE  
----------- ------------------------- -------------------- ---------- ----------------
          1 SVF1521P2EB               SONY                 LAPTOP     WHOLESALER       
          2 15-A003SA                 COMPAQ               LAPTOP     WHOLESALER       
          3 15-N271SA                 HP                   LAPTOP     RETAIL           
          4 15-N290SA                 HP                   LAPTOP     RETAIL           
          5 E6400                     DELL                 LAPTOP     RETAIL           
          6 SVF1521C2EB               SONY                 LAPTOP     WHOLESALER       
          7 SVF1532K4EB               SONY                 LAPTOP     WHOLESALER       
          8 C50-A-1CK                 TOSHIBA              LAPTOP     WHOLESALER       
          9 NX.MF8EK.001              ACER                 LAPTOP     RETAIL           
         10 NP915S3G-K01UK            SAMSUNG              LAPTOP     RETAIL

我假设要达到所需的输出,我需要比较日期等,但我正在努力。

1 个答案:

答案 0 :(得分:1)

这对你有帮助吗?

select * from (
select  
 --row_number() over ( partition by product_key order by time_key ) as rn
 --,lead(time_key) over ( partition by product_key order by time_key ) as l
 lead(time_key) over ( partition by product_key order by time_key ) - time_key as diff
 ,a.*
from 
(
select sysdate  as  TIME_KEY, 0 as PRODUCT_KEY, 'XX' as BRANCH_KEY, 'XX' as LOCATION_KEY, 0 as POUNDS_SOLD, 0 as AVG_SALES, 0 as UNITS_SOLD from dual union all
select to_date('22-DEC-13 08.31.18','DD-MON-YY HH24.MI.SS'),  2, 'B1',  'L19',  21542.39,   10821.2,  100 from dual union all 
select to_date('21-DEC-10 21.19.37','DD-MON-YY HH24.MI.SS'),  3, 'B8',  'L5 ',     65487,   32793.5,  100 from dual union all 
select to_date('13-SEP-13 06.36.03','DD-MON-YY HH24.MI.SS'),  7, 'B2',  'L15',  78541.84,  39470.92,  400 from dual union all 
select to_date('24-JUN-13 12.21.45','DD-MON-YY HH24.MI.SS'),  1, 'B7',  'L13',     94115,   47167.5,  220 from dual union all 
select to_date('18-SEP-07 12.58.06','DD-MON-YY HH24.MI.SS'),  8, 'B2',  'L2 ',     54000,     27250,  500 from dual union all 
select to_date('11-FEB-11 18.06.08','DD-MON-YY HH24.MI.SS'),  8, 'B9',  'L6 ',     11123,    5636.5,  150 from dual union all 
select to_date('28-SEP-13 15.06.20','DD-MON-YY HH24.MI.SS'),  6, 'B3',  'L16',  45896.31,  23008.16,  120 from dual union all 
select to_date('22-DEC-08 19.34.48','DD-MON-YY HH24.MI.SS'),  5, 'B6',  'L3 ',  87451.01,  43875.51,  300 from dual union all 
select to_date('23-JUL-13 20.08.51','DD-MON-YY HH24.MI.SS'),  6, 'B6',  'L14',     69542,     34971,  400 from dual union all 
select to_date('20-DEC-13 22.47.24','DD-MON-YY HH24.MI.SS'),  9, 'B4',  'L17',  21584.39,   10872.2,  160 from dual union all 
select to_date('21-DEC-06 19.11.50','DD-MON-YY HH24.MI.SS'),  5, 'B10', 'L1 ',     10000,     27250,  500 from dual union all 
select to_date('13-MAR-13 14.13.58','DD-MON-YY HH24.MI.SS'),  1, 'B2',  'L11',     62413,     31256,   99 from dual union all 
select to_date('06-MAR-13 18.15.40','DD-MON-YY HH24.MI.SS'),  4, 'B6',  'L10',     94785,   47542.5,  300 from dual union all 
select to_date('20-DEC-13 23.35.12','DD-MON-YY HH24.MI.SS'),  2, 'B5',  'L18',  52359.19,   26289.6,  220 from dual union all 
select to_date('15-MAR-13 19.11.58','DD-MON-YY HH24.MI.SS'),  4, 'B9',  'L12',  66499.84,  33299.92,  100 from dual union all 
select to_date('19-DEC-11 13.17.34','DD-MON-YY HH24.MI.SS'),  9, 'B2',  'L7 ',     51449,   26049.5,  650 from dual union all 
select to_date('14-FEB-12 10.20.20','DD-MON-YY HH24.MI.SS'), 10, 'B5',  'L8 ',     66589,   33394.5,  200 from dual union all 
select to_date('19-DEC-09 10.09.41','DD-MON-YY HH24.MI.SS'),  3, 'B7',  'L4 ',     99125,   49687.5,  250 from dual union all 
select to_date('22-MAR-12 19.36.24','DD-MON-YY HH24.MI.SS'), 10, 'B2',  'L9 ',  62331.66,  31765.83, 1200 from dual union all 
select to_date('11-JAN-14 19.18.58','DD-MON-YY HH24.MI.SS'),  7, 'B8',  'L20',  35214.85,  17667.43,  120 from dual 
) a ) where diff >= 365;