Возможно, вам нужен какой-нибудь оконный агрегат:
with cte as
(
SELECT *,
-- "highest value" per quarter
max(whatever)
over (partition by DATEPART(QUARTER, o.OrderDate)) as max_value
FROM OrderDetails od INNER JOIN Orders o ON od.OrderID = o.OrderID
WHERE YEAR(o.OrderDate) = '1997'
)
select distinct
o.OrderDate, o.OrderID, DATEPART(QUARTER, o.OrderDate)
from cte
-- current value = highest value of the quarter
where whatever = max_value
Это вернет более одной строки в квартал, если существует несколько строк с одинаковым наибольшим значением . Если вам нужна только одна строка, вы должны переключиться на row_number
with cte as
(
SELECT *,
-- "highest value" per quarter = row_number 1
row_number()
over (partition by DATEPART(QUARTER, o.OrderDate)
order by whatever desc) as rn
FROM OrderDetails od INNER JOIN Orders o ON od.OrderID = o.OrderID
WHERE YEAR(o.OrderDate) = '1997'
)
select o.OrderDate, o.OrderID, DATEPART(QUARTER, o.OrderDate)
from cte
-- "highest value" per quarter = row_number 1
where rn = 1
Если вы настаиваете на использовании старого SQL, вы можете сделать то же самое без использования оконных агрегатов (но более сложным и, вероятно, менее эффективным). ):
with cte as
( -- prepare the base data to avoid writing the same join/where twice
SELECT o.OrderDate, o.OrderID, DATEPART(QUARTER, o.OrderDate) as q, whatever
FROM OrderDetails od INNER JOIN Orders o
ON od.OrderID = o.OrderID
WHERE YEAR(o.OrderDate) = '1997'
)
SELECT DISTINCT t1.OrderDate, t1.OrderID, t1.q
FROM cte
join
(
select q,
-- "highest value" per quarter
max(whatever) as max_value
from cte
group by q
) as maxperq
on cte.q = maxperq.q -- same quarter
and cte.whatever = maxperq.whatever -- highest value of the quarter