FIX: Unexpected Results Occur When You Use a Sort and a Stream Aggregate (306893)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q306893
BUG #: 354892 (SHILOH_BUGS)

SYMPTOMS

SQL Server may return unexpected results for a query if all the following conditions are true:
  • The query contains an EXISTS clause.
  • The optimizer chooses a Merge or a Hash join.
  • The query plan uses a Stream Aggregate.

RESOLUTION

To resolve this problem, obtain the latest service pack for Microsoft SQL Server 2000. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

290211 INF: How to Obtain the Latest SQL Server 2000 Service Pack

Hotfix

NOTE: The following hotfix was created prior to Microsoft SQL Server 2000 Service Pack 2.

The English version of this fix should have the following file attributes or later:
   Date       Time      Version     Size      File name     Platform
   -----------------------------------------------------------------
   08/07/01   01:00 PM   8.00.426   8167 KB   s80426i.exe   x86     
NOTE: Due to file dependencies, the most recent hotfix or feature that contains the preceding files may also contain additional files.

WORKAROUND

This problem is specific to queries where the query plan contains a Sort operator that outputs a Rank column, followed by a Stream Aggregate operator with an output list that does not contain the Rank column, followed by a Merge or Hash Join operator.

To work around this problem either:
  • Rewrite the query to use JOINs rather than an EXISTS clause.

    -or-

  • Force a LOOP JOIN by using the OPTION (LOOP JOIN) hint.

STATUS

Microsoft has confirmed this to be a problem in SQL Server 2000. This problem was first corrected in Microsoft SQL Server 2000 Service Pack 2.

MORE INFORMATION

Here is an example of a query that may encounter this problem:
  SELECT t2.PrimaryID, t1.Date
  FROM table1 t1 INNER JOIN table2 t2
  ON EXISTS (SELECT * FROM table3 WHERE PrimaryID = t2.PrimaryID AND Date = t1.Date)
  WHERE t2.PrimaryID = t2.ID
When you rewrite the query by using JOINs, the query might look something like this (depending on your data structure, the DISTINCT keyword or other variations might be necessary to get the same results):
  SELECT t2.PrimaryID, t1.Date
  FROM table1 t1 
  INNER JOIN table2 t2
  INNER JOIN table3 t3 ON t3.PrimaryID = t2.PrimaryID AND t3.Date = t1.Date  
  WHERE t2.PrimaryID = t2.ID

Modification Type:MajorLast Reviewed:10/9/2003
Keywords:kbBug kbfix kbSQLServ2000preSP2Fix KB306893