0

I have a question how to write query to show me top 2 total by num for each id and department (dep). here is example of data:

  id    dep num
288610  101 95
334028  101 64
480492  101 61
259007  102 215
333655  102 177
369079  102 146
128672  102 103
398319  103 247
384462  103 222
448798  103 204
430841  103 133
387829  103 62

and my select

select  a.id, a.dep, COUNT(b.units) as num
from id a
  JOIN dev b ON(a.id = b.id)
where  b.units is null
group by a.id, a.dep
having COUNT(a.enota) >10
order by  a.dep, COUNT(b.units) desc

the result should be:

id  dep num
288610  101 95
334028  101 64
259007  102 215
333655  102 177
398319  103 247
384462  103 222
Phantom
  • 87
  • 3
  • 13
  • 1
    Tip of today: Always use modern, explicit `JOIN` syntax. Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed! – jarlh Dec 29 '22 at 10:38
  • 1
    Tip 2: Have table aliases that make sense, like `d` for `DEJAVNOST_SKD`. – jarlh Dec 29 '22 at 10:39

0 Answers0