MySQL与子查询连接表

时间:2015-07-08 16:05:42

标签: mysql loops date while-loop stack

我在下面有这个疯狂的查询。我想组织他们,但不知道如何。 有人可以帮我运行这个查询吗?我想加入下面的5个子查询表,最后,结果将是一个显示行item_id的表,其中包括last5days,last10days,last30days,last60days,last90days。 非常感谢!

Select q1.line_item_id, 
       q1.domains as 'last5days', 
       q2.domains as 'last10days', 
       q3.domains as 'last30days', 
       q4.dpmains as 'last60days', 
       q5.domains as 'last90days'
From q1
Join q2 on q1.line_item_id = q2.line_item_id
Join q3 on q1.line_item_id = q3.line_item_id
Join q4 on q1.line_item_id = q4.line_item_id
Join q4 on q1.line_item_id = q5.line_item_id


select q1.line_item_id, count(*) domains
from (
      select distinct line_item_id, domain
      from rpt_domain_by_campaign
      where event_date between DATE_SUB(curdate(), INTERVAL 5 DAY) 
      and now()
      ) q1
group by q1.line_item_id


select q1.line_item_id, count(*) domains
from (
      select distinct line_item_id, domain
      from rpt_domain_by_campaign
      where event_date between DATE_SUB(curdate(), INTERVAL 10 DAY) 
      and now()
      ) q2
group by q1.line_item_id


select q1.line_item_id, count(*) domains
from (
      select distinct line_item_id, domain
      from rpt_domain_by_campaign
      where event_date between DATE_SUB(curdate(), INTERVAL 30 DAY) 
      and now()
      ) q3
group by q1.line_item_id


select q1.line_item_id, count(*) domains
from (
      select distinct line_item_id, domain
      from rpt_domain_by_campaign
      where event_date between DATE_SUB(curdate(), INTERVAL 60 DAY) 
      and now()
      ) q4
group by q1.line_item_id


select q1.line_item_id, count(*) domains
from (
      select distinct line_item_id, domain
      from rpt_domain_by_campaign
      where event_date between DATE_SUB(curdate(), INTERVAL 90 DAY) 
      and now()
      ) q5
group by q1.line_item_id

3 个答案:

答案 0 :(得分:0)

只需使用sum和case更容易: dplyr

答案 1 :(得分:0)

我建议使用count(distinct...)来避免在每个子查询中添加额外的嵌套层。

我还建议切换子查询的顺序并使用外部联接,因为过去90天内每行都没有行的行也会在过去60天内有行等等。

尝试这样的事情:

Select q1.line_item_id, 
       q1.domains as last90days, 
       coalesce(q2.domains,0) as last60days, 
       coalesce(q3.domains,0) as last30days, 
       coalesce(q4.domains,0) as last10days, 
       coalesce(q5.domains,0) as last5days
from 
(
  select line_item_id, count(distinct domain) as domains
  from rpt_domain_by_campaign
  where event_date between DATE_SUB(curdate(), INTERVAL 90 DAY) 
  and now()
  group by line_item_id
) q1 
left outer join
(
  select line_item_id, count(distinct domain) as domains
  from rpt_domain_by_campaign
  where event_date between DATE_SUB(curdate(), INTERVAL 60 DAY) 
  and now()
  group by line_item_id
) q2 on q1.line_item_id = q2.line_item_id
left outer join
(
  select line_item_id, count(distinct domain) as domains
  from rpt_domain_by_campaign
  where event_date between DATE_SUB(curdate(), INTERVAL 30 DAY) 
  and now()
  group by line_item_id
) q3 on q1.line_item_id = q3.line_item_id
left outer join
(
  select line_item_id, count(distinct domain) as domains
  from rpt_domain_by_campaign
  where event_date between DATE_SUB(curdate(), INTERVAL 10 DAY) 
  and now()
  group by line_item_id
) q4 on q1.line_item_id = q4.line_item_id
left outer join
(
  select line_item_id, count(distinct domain) as domains
  from rpt_domain_by_campaign
  where event_date between DATE_SUB(curdate(), INTERVAL 5 DAY) 
  and now()
  group by line_item_id
) q5 on q1.line_item_id = q5.line_item_id

答案 2 :(得分:0)

只需按最大日期分组即可 select line_item_id, Sum( CASE WHEN DATEDIFF(curdate(), event_date) <= 5 then 1 ELSE 0 END ) 'last5days', Sum( CASE WHEN DATEDIFF(curdate(), event_date) <= 10 then 1 ELSE 0 END ) 'last10days', Sum( CASE WHEN DATEDIFF(curdate(), event_date) <= 30 then 1 ELSE 0 END ) 'last30days', Sum( CASE WHEN DATEDIFF(curdate(), event_date) <= 60 then 1 ELSE 0 END ) 'last60days', Sum( CASE WHEN DATEDIFF(curdate(), event_date) <= 90 then 1 ELSE 0 END ) 'last90days' from ( select line_item_id, max(event_date) event_date from rpt_domain_by_campaign where event_date < curdate() group by line_item_id, domain ) a Group by line_item_id