[英]MySql Use of Max on Date for Aging Report with excluding a specific condition
我需要为未来几个月完成的项目准备一份预测老化报告。 数据位于各种表中-所有表都有一个公用的post_id
字段。 首先让我们看一下我的表结构(我只会提到相关的列)。
01c_update
对每个`post_id具有多个状态。
现在,我已经准备好下面的SQL它没有错误,但给多行根据他们的状态数字每个项目update table
-这样的sum
的confirmed_amount
在table3
将有多个用于相关项目的状态数目。
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.