查询过去一年(按日)活跃订阅者和订阅数量

时间:2015-06-23 15:36:40

标签: sql date plsql

我正在寻找一个允许我捕获过去一年(按日)活跃订阅者和订阅数量的查询。例如,以下查询将告诉我们2013年6月2日至2014年6月1日期间随时主动订阅的唯一订阅者和订阅的数量。

SELECT      count(distinct(user_id)) as subscribers, 
            count(distinct(subscription_id)) as subscriptions
FROM        dataset
WHERE       (start_date <= to_date('01JUN2014:23:59:59','DDMONYYYY:HH24:MI:SS') and 
            end_date >= to_date('02JUN2013:00:00:00','DDMONYYYY:HH24:MI:SS')) and
            payment_amt > 0;

话虽如此,我正在寻求一个查询,允许我同时捕获过去12个月中活跃的所有活跃订阅者和活动订阅(过去365天的日期)。我正在寻找的最终数据集看起来像这样(365行; 3列):

Ref_Date     Subscribers        Subscriptions
6/1/2014        100              150
6/2/2014        101              153
…                 …               …
5/30/2015       106              160
5/31/2015       104              155

此外,对于此帖子中的查询,上表中“Ref_Date”的值为“6/1/2014”。我想对所有365“Ref_Date”执行此操作,只需一次数据拉动。 有人可以提供任何帮助吗?非常感谢!

2 个答案:

答案 0 :(得分:2)

您需要创建一个日期表并将其加入数据。下面的示例使用来自同一数据集的派生表来生成日期列表。

查询:

SELECT Ref_date
    ,count(DISTINCT (user_id)) AS subscribers
    ,count(DISTINCT (subscription_id)) AS subscriptions
FROM (
    SELECT DISTINCT start_date AS ref_date
    FROM dataset
    WHERE start_date BETWEEN DATEADD(YYYY,-1,CURRENT_DATE) and CURRENT_DATE
    ) dateslist
INNER JOIN dataset ON start_date < DATEADD(YYYY, 1, ref_date)
    AND end_date >= ref_date

答案 1 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

WITH all_dates_of_year AS (
  SELECT TRUNC(SYSDATE) - INTERVAL '1' YEAR + LEVEL - 1 AS day
  FROM   DUAL
  CONNECT BY
         TRUNC(SYSDATE) - INTERVAL '1' YEAR + LEVEL - 1 <= TRUNC(SYSDATE)
)
SELECT day,
       COUNT( DISTINCT user_id ) AS num_users,
       COUNT( DISTINCT subscription_id ) AS num_subs
FROM   all_dates_of_year a
       LEFT OUTER JOIN
       dataset d
       ON ( a.day BETWEEN TRUNC( d.start_date ) AND TRUNC( d.end_date ) )
GROUP BY
       a.day

查询1

|                         DAY | NUM_USERS | NUM_SUBS |
|-----------------------------|-----------|----------|
|      June, 23 2014 00:00:00 |         1 |        1 |
|      June, 24 2014 00:00:00 |         1 |        1 |
|      June, 25 2014 00:00:00 |         1 |        1 |
|      June, 26 2014 00:00:00 |         1 |        1 |
|      June, 27 2014 00:00:00 |         1 |        1 |
|      June, 28 2014 00:00:00 |         1 |        1 |
|      June, 29 2014 00:00:00 |         1 |        1 |
|      June, 30 2014 00:00:00 |         1 |        1 |
|      July, 01 2014 00:00:00 |         1 |        1 |
|      July, 02 2014 00:00:00 |         1 |        1 |
|      July, 03 2014 00:00:00 |         1 |        1 |
|      July, 04 2014 00:00:00 |         1 |        1 |
|      July, 05 2014 00:00:00 |         1 |        1 |
|      July, 06 2014 00:00:00 |         1 |        1 |
|      July, 07 2014 00:00:00 |         1 |        1 |
|      July, 08 2014 00:00:00 |         2 |        1 |
|      July, 09 2014 00:00:00 |         2 |        1 |
|      July, 10 2014 00:00:00 |         2 |        1 |
|      July, 11 2014 00:00:00 |         2 |        1 |
|      July, 12 2014 00:00:00 |         2 |        1 |
|      July, 13 2014 00:00:00 |         3 |        2 |
|      July, 14 2014 00:00:00 |         3 |        2 |
|      July, 15 2014 00:00:00 |         3 |        2 |
|      July, 16 2014 00:00:00 |         3 |        2 |
|      July, 17 2014 00:00:00 |         3 |        2 |
|      July, 18 2014 00:00:00 |         3 |        2 |
|      July, 19 2014 00:00:00 |         2 |        2 |
|      July, 20 2014 00:00:00 |         2 |        2 |
|      July, 21 2014 00:00:00 |         2 |        2 |
|      July, 22 2014 00:00:00 |         2 |        2 |
|      July, 23 2014 00:00:00 |         2 |        2 |
|      July, 24 2014 00:00:00 |         2 |        2 |
|      July, 25 2014 00:00:00 |         2 |        2 |
|      July, 26 2014 00:00:00 |         2 |        2 |
|      July, 27 2014 00:00:00 |         2 |        2 |
|      July, 28 2014 00:00:00 |         2 |        2 |
|      July, 29 2014 00:00:00 |         2 |        2 |
|      July, 30 2014 00:00:00 |         2 |        2 |
|      July, 31 2014 00:00:00 |         2 |        2 |
|    August, 01 2014 00:00:00 |         2 |        2 |
|    August, 02 2014 00:00:00 |         2 |        2 |
|    August, 03 2014 00:00:00 |         2 |        2 |
|    August, 04 2014 00:00:00 |         2 |        2 |
|    August, 05 2014 00:00:00 |         2 |        2 |
|    August, 06 2014 00:00:00 |         2 |        2 |
|    August, 07 2014 00:00:00 |         2 |        2 |
|    August, 08 2014 00:00:00 |         2 |        2 |
|    August, 09 2014 00:00:00 |         1 |        1 |
|    August, 10 2014 00:00:00 |         1 |        1 |
|    August, 11 2014 00:00:00 |         1 |        1 |
|    August, 12 2014 00:00:00 |         1 |        1 |
|    August, 13 2014 00:00:00 |         1 |        1 |
|    August, 14 2014 00:00:00 |         0 |        0 |
|    August, 15 2014 00:00:00 |         0 |        0 |
|    August, 16 2014 00:00:00 |         0 |        0 |
|    August, 17 2014 00:00:00 |         0 |        0 |
|    August, 18 2014 00:00:00 |         0 |        0 |
|    August, 19 2014 00:00:00 |         0 |        0 |
|    August, 20 2014 00:00:00 |         0 |        0 |

...

|      June, 21 2015 00:00:00 |         1 |        1 |
|      June, 22 2015 00:00:00 |         1 |        1 |
|      June, 23 2015 00:00:00 |         2 |        1 |

<强> Results

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^catelogue/([^/]+)/([^/]+)$ catelogue.php?page=3&cat1=$1&cat2=$2 [NC,L]
RewriteRule ^catelogue/?$ catelogue.php?page=3 [NC,L]
RewriteRule ^home/?$ index.php?page=1 [NC,L]