alter table %I alter column row_num type int4 using row_num::int, alter column row_num set not null;
DO $ $ BEGIN EXECUTE (
- лишнее, это же просто SELECT.DO $$DECLARE q TEXT;
BEGIN
FOR q IN SELECT format(
'alter table %I alter column row_num type int4 using row_num::int, alter column row_num set not null',
tablename
)
FROM pg_tables WHERE schemaname = 'public'
LOOP
EXECUTE q;
END LOOP;
END$$;
возвращать одно рандомное из дублей, но тогда есть вероятность вывода неправильного результата
SELECT DISTINCT ON (project_id) *
FROM tasks
WHERE tasks.id = ANY (...)
ORDER BY project_id, created
CREATE OR REPLACE FUNCTION public.foo(str character varying)
RETURNS SETOF record
LANGUAGE plpgsql
AS $$
BEGIN
IF str = 'i' THEN
RETURN QUERY SELECT i, i*i FROM generate_series(1, 10) i;
ELSE
RETURN QUERY SELECT i, SQRT(i::float) FROM generate_series(1, 10) i;
END IF;
END;
$$
# select * from foo('i') as (key int, value int);
key | value
-----+-------
1 | 1
2 | 4
3 | 9
4 | 16
5 | 25
6 | 36
7 | 49
8 | 64
9 | 81
10 | 100
(10 rows)
# select * from foo('x') as (key int, value float);
key | value
-----+--------------------
1 | 1
2 | 1.4142135623730951
3 | 1.7320508075688772
4 | 2
5 | 2.23606797749979
6 | 2.449489742783178
7 | 2.6457513110645907
8 | 2.8284271247461903
9 | 3
10 | 3.1622776601683795
(10 rows)
SELECT
t.str,
SUM(n)
FROM (
(SELECT str1 as str, COUNT(*) as n FROM tableName WHERE date BETWEEN '2021-01-01' AND '2021-12-31' GROUP BY str1)
UNION
(SELECT str2 as str, COUNT(*) as n FROM tableName WHERE date BETWEEN '2021-01-01' AND '2021-12-31' GROUP BY str2)
) as t
GROUP BY t.str
CREATE OR REPLACE FUNCTION Foo(st TIMESTAMP, fin TIMESTAMP)
RETURNS TABLE (tstamp timestamptz, val float) AS $$
...
RETURN QUERY SELECT tstamp, val FROM hyptab WHERE tstamp >= st AND tstamp < fin;
END;
$$ LANGUAGE plpgsql;
db.execute("PRAGMA journal_mode=WAL")
select generate_series(start_time, end_time, interval '1 second'), t.*
from t
insert into table_Number (key, tstamp)
select * from (
select row_number() over () n, t
from generate_series(NOW() - INTERVAL '90 days', NOW(),'1 min') t
) sq
where n <= 300
select status, generate_series(
date_trunc('hour', date_start),
date_trunc('hour', date_end),
interval '1 hour')
from tab
-- date('now', 'start of day')
date_trunc('day', now())
-- date('now', 'start of month')
date_trunc('month', now())
-- date('now', '-6 days')
now() - INTERVAL '6 days'
gener=connection.escape(gener)
вы получаете в запросе LIKE "%'blabla'%"
.def escape(self, obj, mapping=None):
"""Escape whatever value is passed.
Non-standard, for internal use; do not use this in your applications.
"""
cursor.execute(script.format(year_from=args.year_from,
year_to=args.year_to,
name="%{}%".format( (args.regexp.translate(str.maketrans({'%': '\%', '_': '\_'})),) ),
gener="%{}%".format( (gener.translate(str.maketrans({'%': '\%', '_': '\_'})),) ),
rating=1,
N=args.N))
SELECT m.title, year, avg(r.rating)
FROM movies AS m
JOIN rating AS r
ON m.movie_id = r.movie_id
WHERE m.year BETWEEN {year_from} and {year_to} AND m.title LIKE {name} AND m.genres LIKE {gener}
GROUP BY r.movie_id, m.title, m.year
HAVING avg(r.rating) > {rating}
ORDER BY avg(r.rating) DESC, m.year DESC, m.title asc
LIMIT {N};