Monday 3 September 2018

Mysql aggregating data with the if statement

Simplified example of a problem I am trying to solve:

EDIT - With PK
---
I have two tables:
People
--------
| Name |
--------
| Mike |
| Jim  |
| Fred |
--------

HasBanana
------------------------
| Name  | Has_a_banana |
------------------------
| Mike  | N            |
| Mike  | N            |
| Mike  | N            |
| Mike  | N            |
| Mike  | Y            |
| Mike  | N            |
| Jim   | N            |
| Jim   | N            |
| Jim   | Y            |
| Jim   | N            |
| Jim   | N            |
| Fred  | N            |
| Fred  | N            |
| Fred  | N            |
| Fred  | N            |
| Fred  | N            |
| Fred  | N            |
------------------------

And a query:
select * from People
left join HasBanana on HasBanana.name = People.name;

This produces a table exactly the same as the HasBanana table above. What I would like to produce is a table grouped by People.name and showing Y if the person has ever been marked as having a banana.
So something like this:
HadBanana
------------------------
| Name  | Had_a_banana |
------------------------
| Mike  | Y            |
| Jim   | Y            |
| Fred  | N            |
------------------------

I know I can use the following to group by name
group by people.name;

but having issues doing the logic to say for each name if has_a_banana has ever been Y then set had_a_banana to Y.

select
   p.name
   , case when x.name is null then 'N' else 'Y' end as had_a_banana
from people p
left outer join
(
  select
     name
  from bananas
  where has_a_banana = 'Y'
  group by name
) x
on x.name = p.name
group by p.name, x.name

I've checked this using your SQLFiddle link, although your Fiddle data does not match your question, so I get:
Fred  N
Jim   Y
Mike  N

(which seems to match your Fiddle data).

0 comments:

Post a Comment