select admin_id
count(*) filter(where created_at >= '2019-01-01' and created_at < '2019-02-01'),
count(*) filter(where created_at >= '2019-02-01' and created_at < '2019-03-01'),
....
from tablename where ... group by admin_id
Как Вы поняли в поле "а" вписывается ответ "no" или "yes" .
В моем бизнесе вопросы собраны в Репорт (Репорты разные. В зависимости от Репорта имеют от 10 до 70 вопросов). Таким образом ответы заходящие в таблицу user нужно разделять по Репортам.
select ...
from product as p
join lateral (
select title, description from product_translate as pt
where pt.product_id = p.id order by language_id = ? limit 1
) on true
where ....
alter table tablename add constraint tablename_p1_xor_p2 check((p1 is null and p2 is not null) or (p2 is null and p1 is not null));
Возможно ли не встравлять в таблицу записи, которые уже присутствуют в таблице?
SELECT Prop.*, coalesce("counters".countAllUnits, 0) AS "countAllUnits", coalesce("counters".countVacantUnits, 0) AS "countVacantUnits"
FROM "Property" AS Prop
INNER JOIN LATERAL
(
SELECT COUNT(*) AS countAllUnits,
count(*) filter(where "Unit".status = 'Vacant') as countVacantUnits
FROM "Unit"
WHERE Prop."id" = "Unit"."propertyId"
) AS "counters" ON true
WHERE Prop."userId" IN (
SELECT id FROM "User" WHERE "companyId" = 200001
)
ORDER BY Prop."userId" ASC LIMIT 10 OFFSET 0
create table new_city (like city including all);
insert into new_city select * from city;
begin;
alter table tablename drop column foo;
alter table tablename add column foo ...
commit;