mercredi 15 avril 2015

Group/Sort SQL by two columns or more. Most common values

I am trying to figure out the most occurring values within a table in groups. This is for SQL


Headers: Part, location, PartDesc



A | 2 | Part A
A | 2 | Part A
A | 2 | Part A
A | 1 | Part A
A | 1 | Part A
B | 1 | Part B
B | 2 | Part B


So the output needs to show


Headers: Part, Location, PartDesc, Occurrence



A | 2 | Part A | 3
A | 1 | Part A | 2
B | 1 | Part B | 1
B | 2 | Part B | 1


So far I have



Select Part, count(*) as occurrence
from
table1
group by Part
order by count(*desc)


Hope this is clear. Many thanks


Aucun commentaire:

Enregistrer un commentaire