簡體   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