select
departments.id,
departments.department,
count(*) as count
from departments
join employees on employees.department_id = departments.id
group by departments.id, departments.name;
select
departments.id,
departments.department,
count(*) as count
from departments
left join employees on employees.department_id = departments.id
group by departments.id, departments.name;
select * from (select
least(city.id, city2.id) as id1,
greatest(city.id, city2.id) as id2
from city
join city as city2 on city.id != city2.id
group by least(city.id, city2.id), greatest(city.id, city2.id)
) as t join city as city1 on city1.id = id1 join city as city2 on city2.id = id2;
select
LEAST(city.name, city1.name), GREATEST(city.name, city1.name)
from city
inner join city as city1 on city1.id != city.id
GROUP BY LEAST(city.name, city1.name), GREATEST(city.name, city1.name);
SELECT materials.id,
materials.title,
materials.author,
materials.description,
types.name AS type,
categories.name AS category
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
WHERE materials.author LIKE '$str%'
OR materials.title LIKE '$str%'
OR categories.name LIKE '$str%'
OR tags.name LIKE '$str%'
select materials.id,
materials.title,
materials.author,
materials.description,
types.name AS type,
categories.name AS category
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
where materials.id in (
SELECT materials.id
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
WHERE materials.author LIKE '$str%'
OR materials.title LIKE '$str%'
OR categories.name LIKE '$str%'
OR tags.name LIKE '$str%' group by materials.id);
SELECT user.*
FROM user
join
(
SELECT name, profession, city
FROM user
GROUP BY name, profession, city
HAVING COUNT(*) > 1
) as groupped on
groupped.name = user.name and COALESCE(groupped.profession = user.profession, 1) and COALESCE(groupped.city = user.city, 1)
ORDER BY name;
if(isset($red_status)=='Выбыл')
if(isset($red_status)&&$red_status=='Выбыл')
SELECT
id
FROM t, jsonb_each_text(to_jsonb(t)) as x("key",val)
group by id
having
count(*) filter(where val::int = 0) <=3 and
count(*) filter(where val::int = 1) <=4;
select
category_contact.contact_id,
LAST_VALUE(category_contact.category_id) over(order by total_points) as category_id
from category_contact
group by category_contact.contact_id;
select
category_contact.contact_id,
substring_index(group_concat(category_contact.category_id order by total_points desc), ',', 1)
from category_contact
group by category_contact.contact_id