mardi 12 juillet 2016

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

Say I have a table Table1 constructed similar to below, where * is Key ID* GROUPID* ATTR1 ATTR2 (...additional columns) -- ------- ----- ----- 1 100 45 89 2 100 37 27 3 101 69 19 4 102 37 22 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