Hi, I want a simple query to pick the MIN(Id) by doing a self join on a table.
Table1
ID(PK) RootId FittID Price SourceID 1 44 55 $100 3 2 45 65 $200 4 3 44 55 $300 3 4 45 65 $400 4
Table2
SourceID PathId trimID 3 10 4 4 40 5
WITH CTE as( SELECT t1.ID,t1.RootID,t1.FittID,t1.Price,t2.PathId,t2.trimId FROM dbo.table1 t1 JOIN dbo.table2 t2 ON t1.SourceId = t2.SourceId ) SELECT MIN(c.ID),c.RootId, c.FittID,c.Price,c.PathId,c.TrimId FROM CTE c JOIN dbo.table1 t12 ON c.RootID = t12.RootID AND c.FittID = t12.fittID JOIN dbo.table2 t22 ON t22.PathId = c.PathId AND t22.trimId = c.trimId GROUP BY c.ID,c.RootId, c.FittID,c.Price,c.PathId,c.TrimId ORDER BY c.ID
Result I'm looking for is
ID RootID FittId PathID trimId Price 1 44 55 10 4 $100 3 45 65 40 5 $300