jeudi 23 juin 2016

Indetragte nested SELECT query when querying for objects in same table?

Say I have a table Table1 constructed similar to below, where * is Primary Key GROUPID*    ATTR1     ATTR2       ...Other fields           1                  45                89                       1                  37                27           2                  69                19 And given a pair of values for ATTR1 and ATTR2, I want my query to return a list of all rows that belong to the GroupID for a given ATTR1 and ATTR2's GROUPID. Assume for each pair of ATTR1 and ATTR2 will have zero or one GroupID. Is there a way to formulate this query to not have a nested query (and therefore speed it up)? Here is the query I have so far: SELECT [GROUPID], [ATTR1], [ATTR2] FROM [TABLE1] WHERE [GROUPID] = ( SELECT TOP 1 GROUPID FROM [TABLE1] WHERE (ATTR1 = @attr1) and (ATTR2 = @attr2) );

Aucun commentaire:

Enregistrer un commentaire