Change column type without changing order
From here
CREATE TEMP TABLE temp_table AS SELECT * FROM original_table;
DROP TABLE original_table;
CREATE TABLE original_table ...
INSERT INTO original_table SELECT * FROM temp_table;
Search column which has value in range
SELECT
a.created, b.*
FROM
table_b b
LEFT JOIN table_a a
ON a.p_uuid = b.pv_production_uuid
WHERE
-- a.created >= '2020-10-01'::date
-- and a.created <= '2020-10-21'::date
a.created BETWEEN '2020-10-01'::date and '2020-10-21'::date
List tables
SELECT * FROM pg_catalog.pg_tables
where tablename like '%patata%' ;
SELECT * FROM pg_catalog.pg_tables
where schema like '%some_model%' ;
Describe table
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name = 'table_name_without_schema'
Capitalize first letter of each word
select initcap(lower(deli.short_title))
from deli
Select top N from groups
Select top 2 subcategories from each category, according to the movie screentime
with grouped_stuff as
( select movie.category, movie.subcategory,
sum(movie.screentime) as total_screentime
from movies movie
where upload_date = '2017-09-18 00:00:00'
group by movie.subcategory, movie.category),
ranking as
(
select grouped_stuff.*,
rank() over
(
partition by category
order by total_screentime desc
)
from
grouped_stuff
)
select * from ranking where rank < 3