आप नीचे दिए गए तरीके से कोशिश कर सकते हैं-
with cte as
(
SELECT
Departments.Name, SALES.Date_sale, SUM(GOODS.Price * SALES.Quantity)
AS profit FROM DEPARTMENTS inner join GOODS on DEPARTMENTS.Dept_id = GOODS.Dept_id
inner join SALES on GOODS.Good_id = SALES.Good_id
GROUP BY DEPARTMENTs.Name, SALES.Date_sale
)A
select * from cte a
where profit =
(select max(profit) from cte b on a.department=b.department)
या आप row_number()
. का उपयोग कर सकते हैं
select * from
(
select *, row_number() over(partition by department oder by profit desc) as rn
from cte
)A where rn=1