2016-03-14 46 views
0

我需要累計數週的客戶數。有沒有好的方法來做到這一點,而不做子查詢?Oracle中的累計計數不同

我一直在尋找一些分析功能,但沒有發現任何有價值的東西。所有的工作解決方案都使用子查詢或求和計數,這對我的任務來說是錯誤的。

例如,對於這些表

WITH the_data as ( 
    select sysdate - 28 dt, 1 val from dual union all 
    select sysdate - 27 dt, 13 val from dual union all 
    select sysdate - 20 dt, 15 val from dual union all 
    select sysdate - 19 dt, 1 cusval from dual union all 
    select sysdate - 18 dt, 2 from dual union all 
    select sysdate - 17 dt, 3 from dual union all 
    select sysdate - 16 dt, 4 from dual union all 
    select sysdate - 15 dt, 5 from dual union all 
    select sysdate - 14 dt, 6 from dual union all 
    select sysdate - 8 dt, 7 from dual union all 
    select sysdate - 6 dt, 8 from dual union all 
    select sysdate - 3 dt, 9 from dual union all 
    select sysdate - 2 dt, 9 from dual union all 
    select sysdate - 1 dt, 10 from dual) 

我想以下

+----------------------------------+ 
| WEEK_NUM CUM_DISTINCT_COUNT | 
+----------------------------------+ 
| 8   2     | 
| 9   6     | 
| 10   8     | 
| 11   11     | 
| 12   12     | 
+----------------------------------+ 
+0

您應該發佈您的表結構,示例數據,您嘗試過的以及期望的結果;沒有這些信息,就很難回答。 [Here](http://stackoverflow.com/help/how-to-ask)你可以閱讀如何提出一個好問題 – Aleksej

+0

沒有任何數據,它將很難幫助你,但是解決方案可能像'COUNT( DISTINCT CUST_NUMBER)(每週劃分)' –

回答

0
SELECT DISTINCT 
     TRUNC(date_column, 'WW') AS week_number, 
     COUNT(customer_id) OVER (ORDER BY TRUNC(date_column, 'WW')) AS num_customers 
FROM your_table 
ORDER BY week_number 

要獲得自2016-01-01不同客戶的累計數,你可以這樣做:

SELECT DISTINCT 
     last_day_of_week, 
     COUNT(is_first_instance_of_customer) 
     OVER(ORDER BY last_day_of_week) AS num_unique_customers 
FROM (
    SELECT TRUNC(date_value, 'WW') + 6 AS last_day_of_week, 
     CASE ROW_NUMBER() 
       OVER (PARTITION BY customer_id 
         ORDER BY date_value) 
     WHEN 1 
     THEN 1 
     END AS is_first_instance_of_customer 
    FROM your_table 
) 
ORDER BY last_day_of_week; 
+0

謝謝,但那不行。這就是我如何得到不同的week_number和num_customers對。但我需要在01/01/2016和每週最後一天之間的客戶數量,再加上我的表「your_table」包含customer_id的重複數據 – Powerfool

+0

@Powerfool更新了不同客戶的累計數量。 – MT0

0

如何做到這一點取決於您的數量是否需要獨特的客戶。爲了表現出一定的運行聚合選項:

WITH the_data as (
    select sysdate - 28 dt, 1 val from dual union all 
    select sysdate - 27 dt, 13 val from dual union all 
    select sysdate - 20 dt, 15 val from dual union all 
    select sysdate - 19 dt, 1 cusval from dual union all 
    select sysdate - 18 dt, 2 from dual union all 
    select sysdate - 17 dt, 3 from dual union all 
    select sysdate - 16 dt, 4 from dual union all 
    select sysdate - 15 dt, 5 from dual union all 
    select sysdate - 14 dt, 6 from dual union all 
    select sysdate - 8 dt, 7 from dual union all 
    select sysdate - 6 dt, 8 from dual union all 
    select sysdate - 3 dt, 9 from dual union all 
    select sysdate - 2 dt, 9 from dual union all 
    select sysdate - 1 dt, 10 from dual) 
select distinct trunc(dt,'WW') wk 
     ,count(val) over (partition by trunc(dt,'WW')) as wk_cnt 
     ,count(distinct val) over (partition by trunc(dt,'WW')) as wk_distinct_cnt 
     ,count(val) over (order by trunc(dt,'WW')) as running_wk_cnt 
--  ,count(distinct val) over (order by trunc(dt,'WW')) as running_distinct_wk_cnt 
from the_data 
order by trunc(dt,'WW') 

returns 
WK,   WK_CNT, WK_DISTINCT_CNT, RUNNING_WK_CNT 
12/02/2016, 2,  2,    2 
19/02/2016, 3,  3,    5 
26/02/2016, 4,  4,    9 
04/03/2016, 2,  2,    11 
11/03/2016, 3,  2,    14 

但是,如果你取消對該行做了運行不同的數,您會得到一個錯誤,使用的正是這種結構是不可能的。

0

大家都謝謝。這是我正在查詢的工作查詢:

with dt as ( 
select 'A1' PLANT, '1' CUSTOMERID, date'2016-01-01' CALDAY from dual union all 
select 'A1' PLANT, '2' CUSTOMERID, date'2016-01-01' CALDAY from dual union all 
select 'A1' PLANT, '3' CUSTOMERID, date'2016-01-01' CALDAY from dual union all 
select 'A2' PLANT, '1' CUSTOMERID, date'2016-01-02' CALDAY from dual union all 
select 'A2' PLANT, '2' CUSTOMERID, date'2016-01-02' CALDAY from dual union all 
select 'A1' PLANT, '1' CUSTOMERID, date'2016-01-08' CALDAY from dual union all 
select 'A1' PLANT, '2' CUSTOMERID, date'2016-01-08' CALDAY from dual union all 
select 'A2' PLANT, '4' CUSTOMERID, date'2016-01-08' CALDAY from dual union all 
select 'A2' PLANT, '4' CUSTOMERID, date'2016-01-15' CALDAY from dual union all 
select 'A1' PLANT, '4' CUSTOMERID, date'2016-01-22' CALDAY from dual) 


, first_buys_plant as (
    select customerid, plant, calday from (
    select customerid, plant, calday, row_number() over (partition by customerid, plant order by calday) rn 
    from dt 
) where rn = 1 
) 
select plant, calweek, max(cum_num) cum_num_cust from (
    select 
    plant, 
    to_char(calday, 'ww') calweek, 
    count(customerid) over(partition by plant order by calday rows between unbounded preceding and current row) cum_num 
    from first_buys_plant 
) group by plant, calweek 
order by 1,2