Last active
April 23, 2021 18:32
-
-
Save martinusso/30caab46ecd895457b976ed0cddb7e67 to your computer and use it in GitHub Desktop.
cola postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
pg_terminate_backend(pid) | |
FROM | |
pg_stat_activity | |
WHERE | |
-- don't kill my own connection! | |
pid <> pg_backend_pid() | |
-- don't kill the connections to other databases | |
AND datname = 'database_name' | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with calendar as ( | |
select | |
day::date | |
from pg_catalog.generate_series( | |
current_date - interval '1 month', | |
current_date, | |
interval '1 day') day | |
) | |
select | |
day, | |
count(id) | |
from calendar | |
left join invoices on created_at::date = day | |
where date_trunc('month', day) = date_trunc('month', current_date) | |
group by day | |
order by day; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
count(1) all, | |
count(1) filter (where created_at is not null), | |
count(1) filter (where updated_at is not null), | |
count(1) filter (where deleted_at is not null) | |
from table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
quantity, | |
rank() over (partition by quantity order by quantity desc) | |
from table; | |
select | |
created_at, | |
count(1), | |
sum(count(1)) over (order by created_at) | |
from table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment