48
loading...
This website collects cookies to deliver better user experience
SELECT <colum 1>, <column 2>, ...
<window function>() OVER (
PARTITION BY <...>
ORDER BY <...>
<window frame>
);
SELECT invoice.customer_id,
customer.first_name,
customer.last_name,
SUM(total),
DENSE_RANK() OVER (ORDER BY SUM(total) DESC)
FROM invoice
INNER JOIN customer ON invoice.customer_id = customer.customer_id
GROUP BY invoice.customer_id, customer.first_name, customer.last_name;
customer_id | first_name | last_name | sum | dense_rank
-------------+------------+------------+-------+------------
6 | Helena | Holý | 49.62 | 1
26 | Richard | Cunningham | 47.62 | 2
57 | Luis | Rojas | 46.62 | 3
45 | Ladislav | Kovács | 45.62 | 4
46 | Hugh | O'Reilly | 45.62 | 4
28 | Julia | Barnett | 43.62 | 5
37 | Fynn | Zimmermann | 43.62 | 5
<------------------ TRUNCATED -------------------->
SELECT <colum 1>, <column 2>
<window function>() OVER <window_name>
FROM <table_name>
WHERE <...>
GROUP BY <...>
HAVING <...>
WINDOW <window_name> AS (
PARTITION BY <...>
ORDER BY <...>
<window frame>)
ORDER BY <...>;
SELECT invoice.customer_id,
customer.first_name,
customer.last_name,
SUM(total),
DENSE_RANK() OVER dense_rank_window
FROM invoice
INNER JOIN customer ON invoice.customer_id = customer.customer_id
GROUP BY invoice.customer_id, customer.first_name, customer.last_name
WINDOW dense_rank_window as (
ORDER BY SUM(total) DESC
);
billing _city
and generate the total sum for that city.SELECT customer_id,
billing_city,
total,
SUM(total) OVER(PARTITION BY billing_city)
FROM invoice;
customer_id | billing_city | total | sum
-------------+--------------+-------+-------
48 | Amsterdam | 1.98 | 40.62
48 | Amsterdam | 13.86 | 40.62
< ..........TRUNCATED.......... >
59 | Bangalore | 1.99 | 36.64
59 | Bangalore | 8.91 | 36.64
< ..........TRUNCATED.......... >
38 | Berlin | 1.98 | 75.24
38 | Berlin | 13.86 | 75.24
< ..........TRUNCATED.......... >
SELECT customer_id,
billing_city,
total,
ROW_NUMBER() OVER(PARTITION BY billing_city ORDER BY total)
FROM invoice;
customer_id | billing_city | total | row_number
-------------+--------------+-------+------------
48 | Amsterdam | 0.99 | 1
48 | Amsterdam | 1.98 | 2
< ...........TRUNCATED........ >
59 | Bangalore | 1.98 | 1
59 | Bangalore | 1.99 | 2
< ...........TRUNCATED........ >
38 | Berlin | 0.99 | 1
36 | Berlin | 0.99 | 2
36 | Berlin | 1.98 | 3
< ...........TRUNCATED........ >
SELECT customer_id as cid,
billing_city as city,
total,
SUM(total) OVER(PARTITION BY billing_city
ORDER BY total) AS DEFAULT,
SUM(total) OVER(PARTITION BY billing_city
ORDER BY total
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS rows,
SUM(total) OVER(PARTITION BY billing_city
ORDER BY total
RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS range,
SUM(total) OVER(PARTITION BY billing_city
ORDER BY total GROUPS
BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS groups
FROM invoice;
cid | city | total | default | rows | range | groups
~~~~~+--------+-------+---------+-------+-------+--------
<----------------------- TRUNCATED --------------------->
36 | Berlin | 0.99 | 1.98 | 1.98 | 9.90 | 9.90
38 | Berlin | 0.99 | 1.98 | 3.96 | 9.90 | 9.90
36 | Berlin | 1.98 | 9.90 | 4.95 | 9.90 | 17.82
36 | Berlin | 1.98 | 9.90 | 5.94 | 9.90 | 17.82
38 | Berlin | 1.98 | 9.90 | 5.94 | 9.90 | 17.82
38 | Berlin | 1.98 | 9.90 | 7.92 | 9.90 | 17.82
36 | Berlin | 3.96 | 17.82 | 9.90 | 7.92 | 27.72
38 | Berlin | 3.96 | 17.82 | 13.86 | 7.92 | 27.72
36 | Berlin | 5.94 | 29.70 | 15.84 | 11.88 | 37.62
38 | Berlin | 5.94 | 29.70 | 20.79 | 11.88 | 37.62
36 | Berlin | 8.91 | 47.52 | 23.76 | 17.82 | 57.42
38 | Berlin | 8.91 | 47.52 | 31.68 | 17.82 | 57.42
38 | Berlin | 13.86 | 75.24 | 36.63 | 27.72 | 45.54
36 | Berlin | 13.86 | 75.24 | 27.72 | 27.72 | 45.54
<----------------------- TRUNCATED --------------------->
n PRECEDING
or n FOLLOWING
statement.RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
. But if your ordering column data type is date or timestamp, you would need to use an interval. Something like, RANGE BETWEEN '2 days' PRECEDING AND '2 days' FOLLOWING
.