AK
Если строго 2 позиции, то точно без with ties
Size: a a a
AK
Л
Н
with cte as (
select ROW_NUMBER() OVER(PARTITION BY cust_id ORDER BY cust_id, sum_ ),
prod_id,
sum(price * quantity) as sum_
from Orders
where YEAR(dat) = 2020
group by cust_id,prod_id
)
select Customers.FIO,
Products.product
from cte
LEFT JOIN Customers ON cte.cust_id = Customers.cust_id
LEFT JOIN Products ON cte.prod_id = Products.prod_id
where cte.ROW_NUMBER <=2
AK
Н
Н
ROW_NUMBER() OVER(PARTITION BY cust_id ORDER BY sum_ ),?
Л
А
AK
ROW_NUMBER() OVER(PARTITION BY cust_id ORDER BY sum_ ),?
Н
AK
ROW_NUMBER() OVER(PARTITION BY cust_id ORDER BY sum_ ),?
А
order by (Select NULL) чтобы явно указать что сортировка нас не интересуетН
with cte as (
select DENSE_RANK() OVER(PARTITION BY cust_id ORDER BY cust_id, sum_ DESC) as RANK,
cust_id,
prod_id,
sum(price * quantity) as sum_
from Orders
where YEAR(dat) = 2020
group by cust_id,prod_id,RANK
)
select Customers.FIO,
Products.product
from cte
LEFT JOIN Customers ON cte.cust_id = Customers.cust_id
LEFT JOIN Products ON cte.prod_id = Products.prod_id
where cte.RANK <=2
AK
order by (Select NULL) чтобы явно указать что сортировка нас не интересуетAK
with cte as (
select DENSE_RANK() OVER(PARTITION BY cust_id ORDER BY cust_id, sum_ DESC) as RANK,
cust_id,
prod_id,
sum(price * quantity) as sum_
from Orders
where YEAR(dat) = 2020
group by cust_id,prod_id,RANK
)
select Customers.FIO,
Products.product
from cte
LEFT JOIN Customers ON cte.cust_id = Customers.cust_id
LEFT JOIN Products ON cte.prod_id = Products.prod_id
where cte.RANK <=2
g
Н
AK
g
AK