Пытался сделать через ROW_NUMBERS() OVER (PATRITION BY date ORDER BY date, time, check), ну тут, конечно, получается он игнорит check и просто проставляет 1,2,3,4,5.
Если добавить PATRITION BY date, check, то он, блин, сортирует по check потом и тоже не то выходит
tuples = ((123, "foo"), (42, "bar"), (23, "baz"))
cur.executemany("INSERT INTO test (num, data) VALUES (%s, %s)", tuples)
SELECT json_build_object(
'account_id', id,
'first_name', first_name,
'last_name', last_name
) AS author
FROM accounts
json_build_object(
'post_id', p.id,
'body', p.body,
'author', json_build_object(
'account_id', a.id,
'first_name', a.first_name,
'last_name', a.last_name
)
)
при объединении таблиц posts и accountsSELECT json_agg(json_build_object(
'post_id', p.id,
'body', p.body,
'author', json_build_object(
'account_id', a.id,
'first_name', a.first_name,
'last_name', a.last_name
)
)) AS posts
FROM posts p
JOIN accounts a ON a.id = p.author_id
SELECT json_agg(json_build_object(
'comment_id', c.id,
'body', c.body,
'author', json_build_object(
'account_id', a.id,
'first_name', a.first_name,
'last_name', a.last_name
)
)) AS comments
FROM comments c
JOIN accounts a ON a.id = c.author_id
GROUP BY post_id
WITH cmts AS (
SELECT json_agg(json_build_object(
'comment_id', c.id,
'body', c.body,
'author', json_build_object(
'account_id', a.id,
'first_name', a.first_name,
'last_name', a.last_name
)
)) AS comments,
post_id
FROM comments c
JOIN accounts a ON a.id = c.author_id
GROUP BY post_id
)
SELECT json_agg(json_build_object(
'post_id', p.id,
'body', p.body,
'author', json_build_object(
'account_id', a.id,
'first_name', a.first_name,
'last_name', a.last_name
),
'comments', COALESCE(c.comments, '[]')
)) AS "posts with comments"
FROM posts p
JOIN accounts a ON a.id = p.author_id
LEFT JOIN cmts c ON c.post_id = p.id
population ~ '^[0-9]+$'
-- Выбираем по population где только число и больше 4000
SELECT *
FROM planet_osm_point
WHERE place = 'town'
AND population ~ '^[0-9]+$'
AND population::int > 4000
-- Отбираем нечисловые значение в population и вставляем в таблицу contr_planet_osm_point
INSERT INTO contr_planet_osm_point
SELECT *
FROM planet_osm_point
WHERE place = 'town'
AND population !~ '^[0-9]+$'
SELECT *
FROM (
SELECT *
FROM planet_osm_point
WHERE place = 'town'
AND population ~ '^[0-9]+$'
) t
WHERE population::int > 4000
WHERE CASE WHEN x > 0 THEN y/x > 1.5 ELSE false END;
SELECT *
FROM planet_osm_point
WHERE place = 'town'
AND CASE WHEN population ~ '^[0-9]+$' THEN population::int > 4000 ELSE false END
;
ALTER TABLE persons
ADD CONSTRAINT persons_check_rols CHECK (rols in('registrator', 'coordinator', 'user'));
ALTER TABLE salary ALTER COLUMN salary_date SET DEFAULT current_date;
ALTER TABLE salary
ADD CONSTRAINT FK_Salary_Employee_ID FOREIGN KEY(employee_id)
REFERENCES EMPLOYEE(employee_id)
ON UPDATE CASCADE
ON DELETE CASCADE
select
case
when c1 = 0 then ''
else rtrim(to_char (c1, 'FM9999999999999999D99'), '.')
end result
from newtable
SELECT
t.a1, t.a2, t.a3,
CASE WHEN ( ARRAY_AGG(t2.value) )[1] IS null THEN null ELSE ARRAY_AGG(t2.value) END AS arr
FROM (
SELECT
t1.a1,
t1.a2,
t1.a3
FROM table1 t1
GROUP BY t1.a1, t1.a2, t1.a3
) t
LEFT JOIN table2 t2 ON t.a1 = t2.b1 AND t.a2 = t2.b2 AND t.a3 = t2.b3
GROUP BY t.a1, t.a2, t.a3
SELECT
t.a1, t.a2, t.a3,
CASE WHEN
(ARRAY_DIMS(ARRAY_AGG(t2.value)) = '[1:1]')
AND ( ARRAY_AGG(t2.value) )[1] IS null
THEN null ELSE ARRAY_AGG(t2.value)
END AS arr
FROM (
SELECT
t1.a1,
t1.a2,
t1.a3
FROM table1 t1
GROUP BY t1.a1, t1.a2, t1.a3
) t
LEFT JOIN table2 t2 ON t.a1 = t2.b1 AND t.a2 = t2.b2 AND t.a3 = t2.b3
GROUP BY t.a1, t.a2, t.a3
CASE WHEN
(ARRAY_DIMS(ARRAY_AGG(t2.value)) = '[1:1]')
AND ( ARRAY_AGG(t2.value) )[1] IS null
THEN null ELSE ARRAY_AGG(t2.value)
END
SELECT
t2.b1,
t2.b2,
t2.b3,
ARRAY_AGG(t2.value)
FROM table2 t2
WHERE EXISTS (
SELECT 1
FROM table1 t1
WHERE t1.a1 = t2.b1
AND t1.a2 = t2.b2
AND t1.a3 = t2.b3
)
GROUP BY t2.b1, t2.b2, t2.b3
Подскажите пожалуйста, как в колонке array_agg получить уникальные значения (в виде массива)