简体   繁体   中英

Inner Join mysql in single table

I have created table where on date there will be 3 status entered, Solved, Unsolved and OnHold. I have created following query from this I want to create bar chart which should show 3 bars of status date wise.

select a.date, a.Solved,b.Unsolved, c.OnHold 
from (select count(prob_stat) as Solved, date from delivery 
where prob_stat='Solved' group by date) a 
inner join (select count(prob_stat) as Unsolved, date from delivery 
where prob_stat='Unsolved' group by date) b on a.date=b.date 
inner join (select count(prob_stat) as OnHold, date from delivery 
where prob_stat='OnHold' group by date) c on a.date=c.date

but it doesn't work, I can see only 1 record as a result. Can anybody help?

Pravin

Try this.

Use CASE expression.

Query

select `date`,
sum(case prob_stat when 'Solved' then 1 end) as Solved,
sum(case prob_stat when 'Unsolved' then 1 end) as Unsolved,
sum(case prob_stat when 'OnHold' then 1 end) as OnHold
from delivery
group by `date`;

You can try like... That working fine

SELECT 
  date,
  COUNT(CASE prob_stat WHEN 'Solved' THEN 1 END ) AS Solved, 
  COUNT(CASE prob_stat WHEN 'Unsolved' THEN 1 END) as Unsolved,
  COUNT(CASE prob_stat WHEN 'OnH' THEN 1 END) AS OnHold 
FROM delivery 
GROUP BY date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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