vendredi 31 juillet 2015

MySql Table Self Join

I have the table below

sku|date
---|---
A1 |Jan
A2 |Jan
A1 |Jan
A1 |Feb
A2 |Feb

I'm trying to get the count per month. I'd like to get the output below;

sku|JAN|FEB
---|---|--|
A1 |2  |1 |
A2 |1  |1 |

I've tried self join and left join with no success, and I'm getting a bit confused.

I get incorrect results with the code below

select s.sku, count(f.sku)
from database f, database s
where f.sku between '2015-01-01' and '2015-01-31'
and
s.sku=f.sku
group by s.sku

Please advise.

Aucun commentaire:

Enregistrer un commentaire