Untitled

From Edgy Cockroach, 4 Months ago, written in SQL, viewed 67 times.
URL http://codebin.org/view/651cb289 Embed
Download Paste or View Raw
  1. SELECT
  2.       DISTINCT billing_country country,
  3.       COUNT(DISTINCT invoice_id) total_invoice,
  4.       total_customer
  5.      
  6. FROM invoice i
  7.  
  8. JOIN(SELECT country,
  9.            COUNT(customer_id) total_customer
  10.      FROM client
  11.      GROUP BY country) AS customer_country ON customer_country.country=i.billing_country
  12.  
  13. WHERE EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) =
  14.             (SELECT year_number
  15.              FROM (SELECT EXTRACT(YEAR FROM CAST(invoice_date AS DATE)) year_number,
  16.                           EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) month_number,
  17.                           SUM(total) AS sum_total
  18.                    FROM invoice
  19.                    WHERE EXTRACT(MONTH FROM CAST(invoice_date AS DATE)) BETWEEN 6 AND 8
  20.                    GROUP BY year_number, month_number) AS yms
  21.              
  22.             GROUP BY year_number
  23.             ORDER BY SUM(sum_total) DESC
  24.             LIMIT 1)
  25.            
  26. GROUP BY billing_country, total_customer
  27. ORDER BY total_invoice DESC, country ASC

Reply to "Untitled"

Here you can reply to the paste above