MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 21st, 2004, 03:32 PM
jmarshll jmarshll is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 jmarshll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Order By messes up TOP 1

I'm using SQL Server 2000.

I have a basic select statement as follows:

select top 1 envelopeid
from envelopes
where queueid = somenumber
order by priority asc

However, when I run this I get not the first row, but the second. If I remove the TOP 1 and just use
SET ROWCOUNT 1, it works correctly. Also, if I remove the Order By, using the TOP 1 works correctly, but this won't work for me in all cases, so it's not a solution.

Anybody had this problem before? Thanks.

Reply With Quote
  #2  
Old April 21st, 2004, 08:51 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Not sure why you need the TOP when you're only looking for 1 row? Is it a prob to just use filtering in your where clause to get the row you want? Just curious.

Reply With Quote
  #3  
Old April 21st, 2004, 09:35 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
Quote:
Originally Posted by jmarshll
However, when I run this I get not the first row, but the second. If I remove the TOP 1 and just use SET ROWCOUNT 1, it works correctly.

define "correctly"

there is no "second row" without an ORDER BY

there is no concept of position in a relational table, only in a result set after the ORDER BY has been applied

if you then choose TOP 1, then that's what you get
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old April 22nd, 2004, 01:56 AM
jmarshll jmarshll is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 jmarshll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the replies. To clarify, I should mention that this problem arises only when all the rows returned have the same Priority value.

What I have is this query...

select envelopeid
from envelopes
where queueid = somenumber
order by priority asc

returns data like this...

row1
row2
row3

When I add "TOP 1" like this...

select top 1 envelopeid
from envelopes
where queueid = somenumber
order by priority asc

I get returned this...

row2

When I run this...

set rowcount 1
select envelopeid
from envelopes
where queueid = somenumber
order by priority asc

I get this...

row1

I know the order of the data in the db does not matter, and I don't care about it. I only mentioned it because when I run this query without the "ORDER BY"...

select top 1 envelopeid
from envelopes
where queueid = somenumber

I still just happen to get the following order, which I know is just dumb luck...

row1
row2
row3

Since all rows have the same Priority, adding the ORDER BY should not cause the order to change, but it does when using the TOP 1, but not when I use the SET ROWCOUNT 1.

So, there appear to be 2 issues here. First, the SET ROWCOUNT 1 query returns row1 with the ORDER BY where the TOP 1 query returns row2. Second, executing the TOP 1 query without the ORDER BY returns a different order of rows than it does with the ORDER BY, even though all rows have the same value for the order criterion. Again, ORDER BY with SET ROWCOUNT 1 returns row1, where ORDER BY with TOP 1 returns row2.

You might think it shouldn't matter which row I get as long as all rows have the same priority, but the users are seeing these envelopes listed in a queue on their screens in a certain order (row1, row2, row3, as they come out of the db), and the one on the top of the list never gets picked up to be worked until it's the last one in the queue. Functionally it's ok, but it looks weird to the users. Did I make that clear as mud? Thanks for your help.

Jim

Reply With Quote
  #5  
Old April 22nd, 2004, 02:39 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
Quote:
adding the ORDER BY should not cause the order to change


There is no order to change. All your reasoning is still based on the assumption that the rows has a certain order in the table.

Add more columns to the order by clause to distinguish between rows with the same priority.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Order By messes up TOP 1


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway