Skip to content

Instantly share code, notes, and snippets.

@urusai88
Created April 8, 2019 14:25
Show Gist options
  • Save urusai88/d5a3d3802bbe44670751cf5c2c414fd7 to your computer and use it in GitHub Desktop.
Save urusai88/d5a3d3802bbe44670751cf5c2c414fd7 to your computer and use it in GitHub Desktop.
SELECT
id AS client_id,
(SELECT COUNT(*) FROM orders o1 WHERE o1.client_id = c.id AND o1.price < 1000) AS count1,
(SELECT COUNT(*) FROM orders o2 WHERE o2.client_id = c.id AND o2.price > 1000) AS count2
FROM clients c
SELECT c.* FROM (
SELECT *,
row_number() OVER (PARTITION BY client_id) AS num
FROM orders
) as c WHERE c.num % 3 = 0
SELECT c.* FROM (
SELECT *,
row_number() OVER (PARTITION BY client_id) AS num
FROM orders
WHERE price > 1000
) as c WHERE c.num % 3 = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment