| 124 | {{{#!sql |
| 125 | select c2.categoryname, |
| 126 | (select a.articlename from articles a |
| 127 | left join invoicedarticles i on i.articleid=a.articleid |
| 128 | left join article_belongs_to_category abtc on abtc.articleid=a.articleid |
| 129 | where abtc.categoryid = c2.categoryid |
| 130 | group by a.articleid |
| 131 | having coalesce(sum(i.quantity),0)=max(q1.vkolicina) limit 1), |
| 132 | max(q1.vkolicina) as vkolicinia from categories c2 |
| 133 | left join( |
| 134 | select c.categoryid, c.categoryname, articlename, (coalesce(sum(i.quantity),0)) as vkolicina from categories c |
| 135 | left join article_belongs_to_category abtc on abtc.categoryid=c.categoryid |
| 136 | left join articles a on a.articleid=abtc.articleid |
| 137 | left join invoicedarticles i on i.articleid=a.articleid |
| 138 | group by c.categoryid, a.articleid ) q1 on q1.categoryid=c2.categoryid |
| 139 | group by c2.categoryid |