I have the follwoing query: - SELECT Column1, Column2
-
FROM Table1
-
WHERE
-
Table1.Column1 IN (
-
SELECT TOP (5) Column1
-
FROM Table2
-
ORDER BY NEWID()
-
)
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
6 4989
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
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()?
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
Then this is what you want - SELECT Column1, Column2
-
FROM Table1
-
WHERE
-
Table1.Column1 IN (
-
SELECT TOP (5) Column1
-
FROM Table2
-
ORDER BY Column1
-
)
-
ORDER BY NEWID()
-
Moving the ORDER BY NEWID() to the outside of your where subselect means the top 5 values will be supplied in a random order
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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',...
|
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...
|
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)...
|
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
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |