sql - MySQL - Using subquery to find an average -


i having trouble syntax of query. trying return largest total 1 company each country.

the tables like:

orders  columns   orderid   customerid   employeeid   orderdate   requireddate  orderdetails  columns   orderid   productid   unitprice   quantity  products  columns   productid   productname   quantityperunit   unitprice  customers  columns   customerid   companyname   contactname   country 

i have tried following:

select     t1.country,     companyname,     t1.ordersum     (select         c.country,         c.companyname,         sum(unitprice * quantity) ordersum     customers c         join orders o             on c.customerid = o.customerid         join orderdetails d             on d.orderid = o.orderid     group c.country) t1             join              -- top payment totals country             (select country,                      max(ordersum) ordersum                          -- payment totals customer                 (select     c.country,                             c.companyname,                             sum(unitprice * quantity) ordersum                  customers c                     join orders o1                         on o1.customerid = c.customerid                     join orderdetails d1                         on d1.orderid = o1.orderid                  group c.country, c.companyname) t2              group country) t3         on t1.country = t3.country             , t1.ordersum = t3.ordersum order country; 

this query returns 3 countries:

ireland hungry owl all-night grocers    57317.3900 norway  sant gourmet    5735.1500 poland  wolski  zajazd  3531.9500 

but, query tried, returns countries, not sure if correct because did not include 'max' value did in previous query:

select     t1.country,     companyname,     t1.ordersum     (select         c.country,         c.companyname,         sum(unitprice * quantity) ordersum     customers c         join orders o             on c.customerid = o.customerid         join orderdetails d             on d.orderid = o.orderid     group c.companyname) t1 group country order country; 

i unsure if calculating order totals correctly, may mistake on part. trying find company each country has largest order total. sorry text.

the following query list companies largest order total per country:

select a.country, a.companyname, a.ordersum (     select         c.country,         c.companyname,         sum(d.unitprice * d.quantity) ordersum     customers c         join orders o on c.customerid = o.customerid         join orderdetails d on d.orderid = o.orderid     group         c.country, c.customerid ) join (     select         s.country, max(s.ordersum) maxsum     (         select             c.country,             c.companyname,             sum(d.unitprice * d.quantity) ordersum         customers c             join orders o on c.customerid = o.customerid             join orderdetails d on d.orderid = o.orderid         group             c.country, c.customerid         ) s     group         s.country ) b on a.country = b.country     a.country = b.country ,     a.ordersum = b.maxsum order     a.country, a.companyname ; 

[update]

note above sql follows way ordersum calculated in listed queries. given table products has quantityperunit , unitprice, suspect ordersum should multiplied quantityperunit – in case you'll need revise math ordersum.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -