SQL中每年的累积计数

SQL中每年的累积计数,sql,count,mariadb,Sql,Count,Mariadb,我有一个“活动”表、一个“客户”表和一个“客户活动”表。在“活动”表中,每个活动都有自己的id和开始、结束年份: 因此,每一行代表一项活动,第一列是开始年,第二列是结束年(也就是说,以下是名为X1的特定客户的活动) 表“customer”包含客户ID和客户名称。最后 表“customer\u activity”包含customer ID和activity ID。 我希望按客户名称(比如名称为X1的客户)进行查询,并得到如下表: Year New Total

我有一个“活动”表、一个“客户”表和一个“客户活动”表。在“活动”表中,每个活动都有自己的id和开始、结束年份: 因此,每一行代表一项活动,第一列是开始年,第二列是结束年(也就是说,以下是名为X1的特定客户的活动)

表“customer”包含客户ID和客户名称。最后 表“customer\u activity”包含customer ID和activity ID。 我希望按客户名称(比如名称为X1的客户)进行查询,并得到如下表:

        Year  New   Total
        2006   2      2
        2007   4      5  
        2008   1      4
        2009   1      2
        2010   0      1
        2011   2      3
        2012   0      2
        2013   0      2
我使用mariadb 10.1.10。
有人能帮我吗?

如果您有年份列表,您可以使用
加入和聚合:

select y.yyyy,
       sum(case when y.yyyy = ca.start then 1 else 0 end) as news,
       count(ca.customer_id) as total
from (select 2007 as yyyy union all select 2008 union all . . .
      select 2013
     ) y left join
     customer_activity ca
     on y.yyyy between ca.start and ca.end left join
     customer c
     on ca.customer_id = c.customer_id and c.name = 'X1'
group by y.yyyy
order by y.yyyy;

生成此类年份列表的最佳方法取决于数据库。

如果您有年份列表,可以使用
加入和聚合:

select y.yyyy,
       sum(case when y.yyyy = ca.start then 1 else 0 end) as news,
       count(ca.customer_id) as total
from (select 2007 as yyyy union all select 2008 union all . . .
      select 2013
     ) y left join
     customer_activity ca
     on y.yyyy between ca.start and ca.end left join
     customer c
     on ca.customer_id = c.customer_id and c.name = 'X1'
group by y.yyyy
order by y.yyyy;

生成此类年份列表的最佳方法取决于数据库。

替换相关列名后,您可以尝试以下方法:

select a.start, count(*) as total, count(c.id) as new
from (select distinct(start) from activity) a 
       left join activity b 
       on a.start between b.start and b.end 
       left join activity c
       on b.id=c.id and a.start=c.start
where b.id in (select customer_activity.id
            from customer left join customer_activity
            on customer.id = customer_activity.customer_id
            where customer.name = "X1" ) 
group by a.start

替换相关列名后,您可以尝试以下操作:

select a.start, count(*) as total, count(c.id) as new
from (select distinct(start) from activity) a 
       left join activity b 
       on a.start between b.start and b.end 
       left join activity c
       on b.id=c.id and a.start=c.start
where b.id in (select customer_activity.id
            from customer left join customer_activity
            on customer.id = customer_activity.customer_id
            where customer.name = "X1" ) 
group by a.start

用你正在使用的数据库标记你的问题。你已经用活动表击中了自己的脚。最好用ID和日期来设置它。然后,可以简单地说,
按年份(日期)从tablename组中选择年份(日期)、总和(id)
。事实上,您可能需要使用一个过程来填充缺失的日期。某些算法最好是在应用程序代码中完成的,而不是SQL。使用您正在使用的数据库标记您的问题。您已经用活动表伤了自己的脚。最好用ID和日期来设置它。然后,可以简单地说,
按年份(日期)从tablename组中选择年份(日期)、总和(id)
。事实上,您可能需要使用一个过程来填写缺失的日期。某些算法最好是用应用程序代码而不是SQL完成的。我不想手动输入年份:“选择2007年为yyyy union all…”有没有办法自动完成此操作?我也不知道您如何在年初和年末将y与customer_活动联系起来。customer_活动没有任何年份列。我是否遗漏了什么?我不想手动输入年份:“选择2007年为yyyy union all…”是否有自动完成的方法?我也不知道您如何在年初和年末加入y的customer_活动。customer_活动没有任何年份列。我错过什么了吗?