finki-main
main
|
Last change
on this file since 06ebe74 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago |
|
Project Handcraft Marketplace
|
-
Property mode
set to
100644
|
|
File size:
1002 bytes
|
| Line | |
|---|
| 1 | CREATE OR REPLACE FUNCTION get_orders_by_total()
|
|---|
| 2 | RETURNS TABLE (
|
|---|
| 3 | order_num INT,
|
|---|
| 4 | client_id INT,
|
|---|
| 5 | client_name TEXT,
|
|---|
| 6 | order_quantity INT,
|
|---|
| 7 | order_status TEXT,
|
|---|
| 8 | payment_method TEXT,
|
|---|
| 9 | discount NUMERIC,
|
|---|
| 10 | order_total NUMERIC
|
|---|
| 11 | )
|
|---|
| 12 | LANGUAGE plpgsql
|
|---|
| 13 | AS $$
|
|---|
| 14 | BEGIN
|
|---|
| 15 | RETURN QUERY
|
|---|
| 16 | SELECT
|
|---|
| 17 | o.order_num,
|
|---|
| 18 | c.client_id,
|
|---|
| 19 | c.name,
|
|---|
| 20 | o.quantity,
|
|---|
| 21 | o.status,
|
|---|
| 22 | o.payment_method,
|
|---|
| 23 | COALESCE(o.discount, 0) AS discount,
|
|---|
| 24 | SUM(p.price * o.quantity)
|
|---|
| 25 | * (1 - COALESCE(o.discount, 0) / 100) AS order_total
|
|---|
| 26 | FROM "order" o
|
|---|
| 27 | JOIN makes_order mo
|
|---|
| 28 | ON o.order_num = mo.order_num
|
|---|
| 29 | JOIN client c
|
|---|
| 30 | ON mo.client_id = c.client_id
|
|---|
| 31 | JOIN includes i
|
|---|
| 32 | ON o.order_num = i.order_num
|
|---|
| 33 | JOIN product p
|
|---|
| 34 | ON i.code = p.code
|
|---|
| 35 | GROUP BY
|
|---|
| 36 | o.order_num,
|
|---|
| 37 | c.client_id,
|
|---|
| 38 | c.name,
|
|---|
| 39 | o.quantity,
|
|---|
| 40 | o.status,
|
|---|
| 41 | o.payment_method,
|
|---|
| 42 | o.discount
|
|---|
| 43 | ORDER BY order_total DESC;
|
|---|
| 44 | END;
|
|---|
| 45 | $$;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.