DECLARE @Test TABLE (id INT,smo NVARCHAR(100),grad NVARCHAR(100),lst int)
INSERT INTO @Test
SELECT 11,'Ali','G',1
UNION ALL
SELECT 11,'Ali','C',1
UNION ALL
SELECT 11,'Ali','F',0
UNION ALL
SELECT 12,'Veli','C',1
UNION ALL
SELECT 12,'Veli','G',1
SELECT t.id, t.smo, t.grad,c.* FROM @Test t
CROSS APPLY(
SELECT MIN(lst) glst FROM @Test t1
WHERE t1.id=t.id
GROUP BY id, smo
) c
The @Test table is actually my result from another query.
The thing that i want is, recieving the min value from 'lst' column for each 'smo' value.
With cross apply i can achieve what i want, when it is a table.
How can i solve it when i am in a condition like that.
SELECT * FROM (
SELECT * FROM @Test
) t
INPUT
Expected OUTPUT
Thank you in advance for your help...