繁体   English   中英

MySql在日期上使用Max on Date排除特定条件

[英]MySql Use of Max on Date for Aging Report with excluding a specific condition

我需要为未来几个月完成的项目准备一份预测老化报告。 数据位于各种表中-所有表都有一个公用的post_id字段。 首先让我们看一下我的表结构(我只会提到相关的列)。

  • table1:01c_posts(ID,post_title,......)
  • table2:01c_project(id,post_id,end_date,.....)
  • table3:01c_enquairy_products(id,post_id,confirmed_amount,.....)
  • table4:01c_update(id,post_id,status_id,status_date,.....)
  • table5:01c_status(status_id,status,.....)

01c_update对每个`post_id具有多个状态。

现在,我已经准备好下面的SQL它没有错误,但给多行根据他们的状态数字每个项目update table -这样的sumconfirmed_amounttable3将有多个用于相关项目的状态数目。

    SET SQL_BIG_SELECTS=1;
    select a.post_id, b.post_title, 
    sum(if(month(a.end_date) = 1 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Jan", 
    sum(if(month(a.end_date) = 2 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Feb", 
    sum(if(month(a.end_date) = 3 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Mar", 
    sum(if(month(a.end_date) = 4 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Apr", 
    sum(if(month(a.end_date) = 5 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "May", 
    sum(if(month(a.end_date) = 6 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jun", 
    sum(if(month(a.end_date) = 7 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jul", 
    sum(if(month(a.end_date) = 8 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Aug",

    max(d.update_date), d.status_id
    from 01c_projects a 
    inner join 01c_posts b on a.post_id = b.ID 
    inner join 01c_enquairy_product c on a.post_id = c.post_id 
    inner join 01c_updates d on a.post_id = d.post_id 
    where a.post_id not in (select post_id from 01c_updates where status_id = 25 or status_id = 24)
    group by b.post_title
    having max(d.update_date)
    order by a.end_date

有人可以指导我我想念的地方吗?

好,解决了。 最终解决方案如下所示,因此其他人可以在需要时获得帮助

    select a.post_id, b.post_title, 
sum(if(month(a.end_date) = 1 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Jan", 
sum(if(month(a.end_date) = 2 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Feb", 
sum(if(month(a.end_date) = 3 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Mar", 
sum(if(month(a.end_date) = 4 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Apr", 
sum(if(month(a.end_date) = 5 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "May", 
sum(if(month(a.end_date) = 6 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jun", 
sum(if(month(a.end_date) = 7 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jul", 
sum(if(month(a.end_date) = 8 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Aug",

d.update_date, d.status_id
from 01c_projects a 
inner join 01c_posts b on a.post_id = b.ID 
inner join 01c_enquairy_product c on a.post_id = c.post_id 
inner join 01c_updates d on a.post_id = d.post_id 
where a.post_id not in (select post_id from 01c_updates where status_id = 25 or status_id = 24)
AND d.id in 
(select id from 01c_updates filter join (select post_id, max(update_date) as update_date from 01c_updates
    group by post_id ) sfilter on 
    filter.post_id = sfilter.post_id
    and filter.update_date = sfilter.update_date)
group by b.post_title
order by a.end_date

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM