5 | | select u.firstname, u.lastname, u.username, u.email, max(ti.totalPrice) as topInvoiceSum, |
6 | | (select a.articlename from articles a |
7 | | left join invoicedarticles i2 on i2.articleid=a.articleid |
8 | | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticleName, |
9 | | (select i2.price from articles a |
10 | | left join invoicedarticles i2 on i2.articleid=a.articleid |
11 | | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticlePrice, |
12 | | (select i2.quantity from articles a |
13 | | left join invoicedarticles i2 on i2.articleid=a.articleid |
14 | | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticleQuantity, |
15 | | max(ba.totalPrice) as topArticleTotalPrice from workers w |
16 | | left join users u on u.userid=w.userid |
17 | | left join |
18 | | ( |
19 | | select i.invoiceid, i.workeruserid , sum(ia.price*ia.quantity) as totalPrice from invoices i |
20 | | left join invoicedarticles ia on ia.invoiceid=i.invoiceid |
21 | | group by i.invoiceid, i.workeruserid |
22 | | ) ti on ti.workeruserid=u.userid |
23 | | left join |
24 | | ( |
25 | | select ia2.invoiceid, a.articlename, ia2.price as price, ia2.quantity as quantity, ia2.price*ia2.quantity as totalPrice from invoicedarticles ia2 |
26 | | left join articles a on a.articleid=ia2.articleid |
27 | | ) ba on ba.invoiceid=ti.invoiceid |
28 | | group by u.userid |
29 | | order by topInvoiceSum desc |
30 | | limit 5 |
| 5 | select u.firstname, u.lastname, u.username, u.email, coalesce(max(ti.totalPrice), 0) as topInvoiceSum, |
| 6 | (select a.articlename from project.articles a |
| 7 | left join project.invoicedarticles i2 on i2.articleid=a.articleid |
| 8 | where i2.price*i2.quantity=max(ba.totalPrice) |
| 9 | limit 1) as topArticleName, |
| 10 | (select i2.price from project.articles a |
| 11 | left join project.invoicedarticles i2 on i2.articleid=a.articleid |
| 12 | where i2.price*i2.quantity=max(ba.totalPrice) |
| 13 | limit 1) as topArticlePrice, |
| 14 | (select i2.quantity from project.articles a |
| 15 | left join project.invoicedarticles i2 on i2.articleid=a.articleid |
| 16 | where i2.price*i2.quantity=max(ba.totalPrice) |
| 17 | limit 1) as topArticleQuantity, |
| 18 | max(ba.totalPrice) as topArticleTotalPrice from project.workers w |
| 19 | left join project.users u on u.userid=w.userid |
| 20 | left join |
| 21 | ( |
| 22 | select i.invoiceid, i.workeruserid , sum(ia.price*ia.quantity) as totalPrice from project.invoices i |
| 23 | left join project.invoicedarticles ia on ia.invoiceid=i.invoiceid |
| 24 | group by i.invoiceid, i.workeruserid |
| 25 | ) ti on ti.workeruserid=u.userid |
| 26 | left join |
| 27 | ( |
| 28 | select ia2.invoiceid, a.articlename, ia2.price as price, ia2.quantity as quantity, ia2.price*ia2.quantity as totalPrice from project.invoicedarticles ia2 |
| 29 | left join project.articles a on a.articleid=ia2.articleid |
| 30 | ) ba on ba.invoiceid=ti.invoiceid |
| 31 | group by u.userid |
| 32 | order by topInvoiceSum desc |
| 33 | limit 5 |