473,419 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

ORDER BY NEWID() Problem

I have the follwoing query:

Expand|Select|Wrap|Line Numbers
  1. SELECT Column1, Column2
  2. FROM Table1
  3. WHERE 
  4. Table1.Column1 IN (
  5.     SELECT TOP (5) Column1
  6.     FROM Table2
  7.     ORDER BY NEWID()
  8. )
My problem is that the Top(5) part of the query is being ignored and instead a random number of rows is returned. Somtimes no rows, sometimes 5, sometimes 10. If I change the Order by NEWID() to Order by Column1 then the Query works fine and the expected number of rows is returned. This leads me to think there is some kind of conflict between the Top function and the Order by NEWID().

Any ideas what might be happening?

Thanks,
Steve
Sep 1 '10 #1
6 4989
ck9663
2,878 Expert 2GB
When you said the TOP 5 of the query is ignored, are you talking about the subquery or the entire query? Since your ORDER BY will generate a random number every time, the subquery will return 5 different rows every time. Therefore, your IN clause will return unexpected result as the list of the Column1 in your subquery can change every time you run it.

Hope that make sense...

Happy Coding!!!

~~CK
Sep 1 '10 #2
Jerry Winston
145 Expert 100+
CK nailed it.

There's nothing sequential about NEWID() that's just not what it does. What were you trying to accomplish by using ORDER BY NEWID()?
Sep 1 '10 #3
It's the top 5 of the subquery. There are 5 rows that should be returned by the subquery but I need them in a random order. I can order by a specific column and the subquery will return 5 rows but when ordering by NEWID I get a different number of rows each time from the subquery.

As I understand it the ORDER BY in the subquery should just return the same number of rows but in a different order. Is that correct?

Thanks,
Steve
Sep 2 '10 #4
gpl
152 100+
Then this is what you want
Expand|Select|Wrap|Line Numbers
  1. SELECT Column1, Column2
  2. FROM Table1
  3. WHERE 
  4. Table1.Column1 IN (
  5. SELECT TOP (5) Column1
  6. FROM Table2 
  7. ORDER BY Column1 
  8. )
  9. ORDER BY NEWID()
  10.  
Moving the ORDER BY NEWID() to the outside of your where subselect means the top 5 values will be supplied in a random order
Sep 2 '10 #5
ck9663
2,878 Expert 2GB
The IN clause does not care of the order. It just checks for existence. So even if your sub-query returns the top 5 in whatever order the the IN clause will always return true and your outer query should return the same result set.

If your requirement is for the outer query to return random records, then GPL's code will do.

Happy Coding!!!

~~ CK
Sep 2 '10 #6
NeoPa
32,556 Expert Mod 16PB
I have no idea why you would get a differnt number of rows Steve. It is possible for the TOP predicate to return more rows than the value specified, if there are a number of records that have matching values around the nth record (in whichever sequence is used). I'm guessing this is not the case here (from your description of the problem).

Does this even matter now? Gpl's solution should provide exactly what you require anyway. Let us know how it works for you.
Sep 6 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: the Mill | last post by:
When i ORDER names containing characters like â or ê it places them behind all other a's or e's. So instead of Aaron, Aâst, Abces, i get Aaron, Abces, Aâst. Does anyone know a solution to my...
5
by: tilak.negi | last post by:
We have one single hash (#) table, in which we insert data processing priority wise (after calculating priority). for. e.g. Company Product Priority Prod. Qty Prod_Plan_Date C1...
2
by: champ.supernova | last post by:
Hi, I was hoping someone could help me with what I'm sure is a very simple problem...I just can't seem to find the syntax! I'm wanting to update the rows in 'tbl_consolidate' from 'tbl_hold',...
2
by: Donal McWeeney | last post by:
Hi, I think I may have a slight encoding problem with what I am doing. I have a test console app that takes an xml document and splits it into a number of smaller xml documents. First thing...
4
by: Thomas Beutin | last post by:
Hi, when i do the following query i get an wrong result order on my postgres system (PostgreSQL 7.2.4 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2.2) tb_test=# SELECT name, upper(name)...
1
by: Marie-Christine Bechara | last post by:
SELECT * FROM tblCountry WHERE Country_Id = 26 UNION ALL SELECT * FROM tblCountry WHERE Country_Id <26 --order by country_Name
0
by: dushy | last post by:
hello all, I have a table that in ms access has a "first_name" and "last_name" fields all the data is in utf-8 encoding when i try to make a query like: SELECT * FROM myTable order by...
5
by: spamfilteraccount | last post by:
Hi, I have a PHP file which outputs javascript code. It works well in Firefox, but Opera chokes on it. The problem is the PHP file contains UTF characters, so when I output javascript code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.