Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Task: We need the query for daily total report. Report should contain list of ordered courses (name and cost), count for each course, cost subtotal for each course and a total cost. Data should be queried on the daily basis.
  3. Optionally - data should be queried using the dates range, between Start date and End date.
  4. */
  5.  
  6. --View creation
  7. CREATE OR REPLACE VIEW "FoodOrder".daily_order_report AS
  8.  SELECT oc.order_date,
  9.     co.title,
  10.     co.price,
  11.     sum(oc.portions) AS portions,
  12.     sum(oc.portions)::numeric * co.price AS cost,
  13.     cu.first_name,
  14.     cu.last_name
  15.    FROM course as co
  16.      JOIN order_course as oc ON co.id = oc.course_id
  17.      JOIN customer as cu ON cu.id = oc.customer_id
  18.   GROUP BY oc.order_date, co.id, oc.course_id, cu.first_name, cu.last_name;
  19.  
  20. --Select query
  21. select title, price, portions, cost
  22. from daily_order_report
  23. where order_date IN('2001-02-16') --or BETWEEN begin_date AND end_date
  24. UNION
  25. select 'Total', 0, 0, sum(cost)
  26. from daily_order_report
  27. where order_date IN('2001-02-16') --or BETWEEN begin_date AND end_date
  28. order by 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement