I am trying to execute the following query. And I am facing the above error.
An aggregate may not appear in the WHERE clause, unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
SELECT student_id,
class_id,
item_id,
(SELECT stock
FROM dbo.daily_closing_stock_details
WHERE stock_reporting_date = Min(stock_reporting_date)
AND item_id = dcs.non_mrp_item_id) AS Stock
FROM dbo.daily_closing_stock_details
GROUP BY student_id,
sap_customer_id,
item_id