嵌套SQL查询计数月数

时间:2016-07-28 22:03:01

标签: mysql sql oracle

我是SQL的新手,想知道如何为这个问题编写查询。 让我们说我们有这些领域: date_created date_unsubscribed subscriberid

enter image description here

如何编写一个SQL查询,按月列出订阅列表的人数,从列表中取消订阅以及有多少净订阅者(新订阅者减去取消订阅者)。 全部在一个查询...

3 个答案:

答案 0 :(得分:2)

以下是使用conditional aggregationunion all

的一个选项
select month(dt), 
       count(case when subscribe = 1 then 1 end) subscribecount,
       count(case when subscribe = -1 then 1 end) unsubscribecountt,
       sum(subscribe) overallcount
from (
   select date_created as dt, 1 as subscribe
   from yourtable
   union all
   select date_unsubscribed, -1
   from yourtable
   where date_unsubscribed is not null
) t
group by month(dt) 

子查询创建一个日期列表,其中包含subscribe或unsubscribe标志。然后,您可以countcase一起使用,以确定适当数量的订阅者/取消订阅者。

SQL Fiddle Demo

答案 1 :(得分:1)

你可以写一个sum(case)(有条件的和)来聚合 - 假设date_created列永远不为null。例如:

ORACLE:

SELECT
TO_CHAR(DATE_CREATED,'MM-YYYY') CREATE_MONTH
,SUM(CASE WHEN date_unsubscribed is not null then 1 else 0 end) unsubscribed
,SUM(CASE WHEN date_unsubscribed is null then 1 else 0 end) subscribed
,COUNT(SUBSCRIBER_ID)
FROM
--YOURTABLENAME
--WHERE
--WHATEVER OTHER CONDITIONS YOU HAVE APPLY
GROUP BY TO_CHAR(DATE_CREATED,'MM-YYYY')

MYSQL:

SELECT
    DATE_FORMAT(DATE_CREATED,'%m-%Y') CREATE_MONTH
    ,SUM(CASE WHEN date_unsubscribed is not null then 1 else 0 end) unsubscribed
    ,SUM(CASE WHEN date_unsubscribed is null then 1 else 0 end) subscribed
    ,COUNT(SUBSCRIBER_ID)
    FROM
    --YOURTABLENAME
    --WHERE
    --WHATEVER OTHER CONDITIONS YOU HAVE APPLY
    GROUP BY DATE_FORMAT(DATE_CREATED,'%m-%Y')

答案 2 :(得分:1)

Oracle解决方案

这是一个使用PIVOT运算符的查询,它是为这种工作创建的,而ROLLUP是为了获得网络号。这只是为了说明;我假设年份是用户或应用程序输入(绑定变量:year,输出设置为2015),我显示1月到6月的摘要。

with
     test_data ( date_created, date_unsubscribed, subscriber_id ) as (
       select date '2015-05-10', null             , 330053448 from dual union all
       select date '2015-04-28', null             , 330053457 from dual union all
       select date '2015-05-10', null             , 330053466 from dual union all
       select date '2015-04-28', null             , 220053475 from dual union all
       select date '2015-04-28', date '2015-05-10', 330053484 from dual
     ),
     prep ( type, val, mth ) as (
       select  'Subscribed'  ,  1, extract(month from date_created)      from test_data
          where extract(year from date_created)      = :year
       union all
       select  'Unsubscribed', -1, extract(month from date_unsubscribed) from test_data
          where extract(year from date_unsubscribed) = :year
     ) 
select nvl(type, 'Net Subscr') as description,
       nvl(sum(jan), 0) as jan, nvl(sum(feb), 0) as feb, nvl(sum(mar), 0) as mar, 
       nvl(sum(apr), 0) as apr, nvl(sum(may), 0) as may, nvl(sum(jun), 0) as jun
from prep
pivot (
         sum(val)
         for mth in (1 as jan, 2 as feb, 3 as mar, 4 as apr, 5 as may, 6 as jun)
      )
group by rollup(type)
order by case type when 'Subscribed' then 1 when 'Unsubscribed' then 2 else 3 end
;


DESCRIPTION         JAN        FEB        MAR        APR        MAY        JUN
------------ ---------- ---------- ---------- ---------- ---------- ----------
Subscribed            0          0          0          3          2          0
Unsubscribed          0          0          0          0         -1          0
Net Subscr            0          0          0          3          1          0

3 rows selected.