Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Links

Wiki
Blog

Forum Statistics

Users
Members:
1878
Members Online:
2
Guests Online:
11

Total Post History
Posts:
81448
Topics:
18714

7-Day Post History
New Posts:
1
New Topics:
0
Active Topics:
1

Our newest member
kowilmar1990z

Other

FAQ
All times are UTC [ DST ]

Google Ads

Query produces incorrect results when used as a SubQuery

Microsoft SQL Server
Please wait...

Query produces incorrect results when used as a SubQuery

Postby tarwn on Sat Apr 27, 2013 2:19 pm

So apparently attempting to do a query off of a non-deterministic sub-query is a really bad idea.

I'm trying to generate a random number of orders for each customer in a table and noticed that when I was joining my random number to the number table I was getting odd results. Broke it down further and realised that even though my sub-query was selecting directly from the customers table, it was producing rows with duplicate ids, missing ids, and wildly different random values. Played a bit more and was able to duplicate it with a basic little script.

All this script is doing is trying to generate a random number for each entry in TableA and join to the corresponding record in TableB whose Id matches that random number.

  1. CREATE TABLE TableA(
  2.     Id int IDENTITY(1,1) PRIMARY KEY
  3. );
  4.  
  5. CREATE TABLE TableB(
  6.     Id int IDENTITY(1,1) PRIMARY KEY
  7. );
  8.  
  9. Go
  10.  
  11. INSERT INTO TableA DEFAULT VALUES
  12. INSERT INTO TableB DEFAULT VALUES
  13.  
  14. Go 100
  15.  
  16. -- #1) Sub-query gives us really incorrect results
  17. SELECT *
  18. FROM (
  19.     SELECT A.Id,
  20.            RandNum = CAST(5 * (ABS(CHECKSUM(NEWID())) % 100000/100000.0) as int)
  21.     FROM TableA A
  22.     ) ARAND
  23.     INNER JOIN TableB B ON B.Id = ARAND.RandNum
  24. ORDER BY ARAND.Id;
  25.  
  26. -- #2) As opposed to the temp table which gives us what we expect
  27. SELECT A.Id,
  28.         RandNum = CAST(5 * (ABS(CHECKSUM(NEWID())) % 100000/100000.0) as int)
  29. INTO #ARAND
  30. FROM TableA A;
  31.  
  32. SELECT *
  33. FROM #ARAND AR
  34.     INNER JOIN TableB B ON B.Id = AR.RandNum
  35. ORDER BY AR.Id
  36.  
  37. DROP TABLE #ARAND

Um, yeah...I'm going to need you to come in on Saturday -- Bill Lumbergh, Office Space
User avatar
tarwn
LTD Admin
LTD Admin
LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897
LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897
LTD Gold - Rating: 897
 
Posts: 3838
Joined: Fri Oct 12, 2007 11:10 am
Location: Raleigh, NC, USA
Unrated