Monday, 12 January 2015

select Only one row from second table

SELECT * FROM tblGroup a
INNER JOIN (
SELECT * FROM tblGroupMatrix where GroupMatrixId
IN (
SELECT MIN(GroupMatrixId) OVER (PARTITION BY GroupId)
                             FROM tblGroupMatrix)) b
                             ON a.GroupId=b.GroupId


OPTIMIZED :

SELECT  *
FROM    tblGroup A
INNER JOIN tblGroupMatrix B ON A.GroupId = B.GroupId
WHERE   GroupMatrixId = ( SELECT    MIN(GroupMatrixId)
                              FROM      tblGroupMatrix
                              WHERE     GroupId = A.GroupId)