Witam,
Studiuję na 1 roku i dostałem takie zadanie:
Pracujemy na bazie Northwind ma ona kilka tabel połączonych ze sobą i trzeba wyciągnąć pewne informacje:
Podaj nazwy najtańszego produktu(produktów jeżeli kilka ma taką samą cenę) z każdej kategorii, nazwy tych kategorii i ich ceny (Najtańsze produkty z każdej kategorii)
No i ja to zrobiłem, ale takim rzemieślniczym sposobem:
select distinct ProductName,
CategoryName,
p.UnitPrice
from Categories c join Products p on c.CategoryID = p.CategoryID
join [Order Details] od on p.ProductID = od.ProductID
where
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 1
order by unitprice asc)
and c.CategoryID = 1
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 2
order by unitprice asc)
and c.CategoryID = 2
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 3
order by unitprice asc)
and c.CategoryID = 3
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 4
order by unitprice asc)
and c.CategoryID = 4
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 5
order by unitprice asc)
and c.CategoryID = 5
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 6
order by unitprice asc)
and c.CategoryID = 6
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 7
order by unitprice asc)
and c.CategoryID = 7
or
p.unitprice = (select top 1 UnitPrice from products
where CategoryID = 8
order by unitprice asc)
and c.CategoryID = 8
order by p.UnitPrice
Moja prośba jest taka. Nie chcę gotowej odpowiedzi, ale jakiegoś naprowadzenia co mógłbym zrobić, aby to zapytanie było proste.