SELECT t1.id, Автомобили.name, t1.category_id from
(
SELECT MAX(id) as id, category_id FROM Автомобили WHERE category_id IN
(SELECT id FROM Категории WHERE parent_id = 0)
GROUP BY category_id
) t1 left join Автомобили on Автомобили.id = t1.id
Select idпродукта from
(
Select idпродукта from table where
(Значение = 200 and Idатрибута = 2 ) or
(Значение = 10 and Idатрибута = 3) or
(Значение = "Ель" and Idатрибута = 1)
) t1
group by idпродукта
having count(idпродукта)>2
Select DISTINCT t1.* from table as t1
join table as t2 on t1.col1 = t2.col2 and t1.col2 = t2.col1 and t1.col1="text1"
select t1.id,t1.number, case when t2.id is not null then 'blacklist' when t3.id is not null then 'whitelist' else '-' end as list
from journal t1
left join blacklist t2 on t1.number = t2.number
left join whitelist t3 on t1.number = t3.number
Select a.item, a.price,SUM(b.left_items) as left_items from
(Select item, MAX(price) as price from dbo.exchange group by item) a
left join
dbo.exchange b
on a.item=b.item and a.price=b.price
group by a.item, a.price