0

I have a pretty long query in which, at some point, I have this :

LEFT OUTER JOIN T1 ON T1.ID = T2.ID

Since I got incorrect results from my query (too many rows), I was working on it and when I tried this :

LEFT OUTER JOIN (SELECT * FROM T1) T1 ON T1.ID = T2.ID

then got what I wanted. I would like to know why these two lines give different results. Can somebody explain it to me?

P.S. I use SQL Server 2008.

Edit 2: I posted a video on YouTube showing the problem.

Edit 1: here is the complete minimal query that shows the problem:

SELECT
    Project.ProjectID,
    Contract.ContractID,
    BookletStatus.PrintStatusID AS StatusID
FROM
    Project
    INNER JOIN Contract ON Contract.ContractID = Project.SignedContractID
    INNER JOIN BookProject ON BookProject.ProjectID = Project.ProjectID
    LEFT OUTER JOIN (SELECT * FROM Booklet) Booklet1 ON Booklet1.ContractID = Contract.ContractID

    INNER JOIN PrintStatus AS CoverStatus ON BookProject.CoverStatusID = CoverStatus.PrintStatusID

    INNER JOIN PrintStatus AS BookletStatus ON 
    (CASE
        WHEN Booklet1.Qty > 0 THEN BookProject.BookletStatusID 
        ELSE -10
    END) = BookletStatus.PrintStatusID

Here are the results, with the inner query "(SELECT * FROM Booklet) Booklet1":

ProjectID ContractID StatusID
  501       1356       -10
  502       1317       -10
  503       1371       -10
  ...

And the results without the inner query (using just "Booklet Booklet1"):

ProjectID ContractID StatusID
  501       1356       -10
  501       1356        0
  501       1356        10
  501       1356        15
  501       1356        20
  ...
  502       1317       -10
  502       1317        0
  502       1317        10
  502       1317        15
  502       1317        20
  ...
  503       1371       -10
  503       1371        0
  503       1371        10
  503       1371        15
  503       1371        20
  ...
6
  • 3
    Probably not going to get much help unless you can reproduce the issue with a small and complete query, and explain what kind of duplicates you were getting. Perhaps try a smaller query with just the previous table SELECT * FROM T2 LEFT OUTER JOIN T1 ... with the two variations, and see if it demonstrates the issue, then you'll have a smaller query, and can post the complete query. If you can't reproduce the issue like that, then it's an indication that the rest of the query contributes somehow to the behavior you're seeing than just the snippet you posted.
    – AaronLS
    Apr 1, 2014 at 0:43
  • Out of curiosity, does it make any difference if you change the alias on you subquery from T1 to T1_Derived? LEFT OUTER JOIN (SELECT * FROM T1) T1_Derived ON T1_Derived.TheID = T2.TheID. It shouldn't, but in a trivial difference I see it affecting the actual execution plan. Apr 1, 2014 at 1:03
  • @KarlKieninger I tried that and it does not make a difference (I get distinct rows with one alias or the other). Something that makes me scratch my head: I get these results on my development machine, with SQL Server 2008; but when I backup/restore the full DB on the production server, running SQL Server 2008 R2, then I always get distinct rows. In other words, my original query (without the inner query) does not behave the same on both machines.
    – ConnorsFan
    Apr 1, 2014 at 1:12
  • Are you sure the result sets are different? Or do they only look different? The result sets should be exactly the same but the ordering of the rows might be different. Apr 1, 2014 at 1:55
  • That's the strange thing: the results should be the same but they are very different (23 rows vs 343 rows).
    – ConnorsFan
    Apr 1, 2014 at 3:25

2 Answers 2

0

CAN you be suffering from an issue dealing with different settings for handling nulls? I coudl see where the code below might evaluate differntly if QTY can be null on machines where the ansi null settings are differnt.

ON 
    (CASE
        WHEN Booklet1.Qty > 0 THEN BookProject.BookletStatusID 
        ELSE -10
    END) = BookletStatus.PrintStatusID
1
  • There is a statement SET ANSI_NULLS ON at the start of the query. Also, in the real production query (much longer than the simplified one shown in this post), the line reads: WHEN (Booklet1.Qty IS NOT NULL) AND (Booklet1.Qty > 0) THEN... I removed the "IS NOT NULL" part because it did not have any observable influence on the problem that I submitted here.
    – ConnorsFan
    Apr 1, 2014 at 18:47
-1

As soon as I can, I will check if an update to SQL Server 2008 makes a difference. I have seen a few comments on the Web about a bug in some 2008 versions of SQL Server.

12
  • It's never a bug in SQL Server... Especially not a bug in JOIN. How many trillions of joins has SQL Server executed till today? The guy in the forum blamed SQL Server for his mistake.; Your query is probably non-deterministic. Therefore, random changes in the query can produce changes in results.
    – usr
    Apr 1, 2014 at 12:56
  • Then, why are the results different on the two servers? I remind you that they are two copies of the same database (made with backup/restore). So, same table structure, same queries, same data; but different results.
    – ConnorsFan
    Apr 1, 2014 at 13:19
  • Different query plan, or the query plan is non-deterministic to begin with (parallelism, unordered prefetch, case-insensitive grouping ...). Query results can be non-deterministic easily.
    – usr
    Apr 1, 2014 at 13:30
  • I edited the description of the problem. Maybe you can help me to detect the non-deterministic part of the query.
    – ConnorsFan
    Apr 1, 2014 at 17:22
  • The query looks very safe regarding undefined behavior. I'm pretty sure it's there, though. Check what @HLGEM said, it is worth a try. Are you relying on views?
    – usr
    Apr 1, 2014 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.