473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select top 40 * from and Order By

Can you use Select top 40 * from ... and ORDER BY... in the same query?

I have the following 4 quearies that are.. for the most part, the same
except the order by clause, all return a differnt amount of records.
Thoughts?

select top 40 * from q_work_list where assigned_to = 10 order by batch_date,
remit_amt desc
40 records returned.
Sort by Reject Amt
select top 40 * from q_work_list where assigned_to = 10 Order by remit_amt
asc
41 records returned.

Sort By Batch Date
select top 40 * from q_work_list where assigned_to = 10 Order by batch_date
desc
110 records returned.
Sort by Batch Again
select top 40 * from q_work_list where assigned_to = 10 Order by batch_date
asc
143 records returned.

And it doesn't make a difference if I use * or the appropriate field names..
same results.

TIA
Jul 19 '05 #1
4 6182
Bryan Harrington wrote:
Can you use Select top 40 * from ... and ORDER BY... in the same
query?

It depends on the database, and sometimes the version of the database <hint>

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #2
"Bryan Harrington" <ne**@psacake.c om> wrote in message
news:OZ******** ******@TK2MSFTN GP12.phx.gbl...
Can you use Select top 40 * from ... and ORDER BY... in the same query?
I have the following 4 quearies that are.. for the most part, the same
except the order by clause, all return a differnt amount of records.
Thoughts?

select top 40 * from q_work_list where assigned_to = 10 order by batch_date, remit_amt desc
40 records returned.
Sort by Reject Amt
select top 40 * from q_work_list where assigned_to = 10 Order by remit_amt asc
41 records returned.

Sort By Batch Date
select top 40 * from q_work_list where assigned_to = 10 Order by batch_date desc
110 records returned.
Sort by Batch Again
select top 40 * from q_work_list where assigned_to = 10 Order by batch_date asc
143 records returned.

And it doesn't make a difference if I use * or the appropriate field names.. same results.

TIA


In future posts, please provide database, version and appropriate DDL
when asking a database related question. Also consider posting the
message to the m.p.i.asp.datab ase group instead.

The behavior of the queries seems to indicate that you are using MS
Access. In MS Access, the TOP predicate of the SELECT clause carries an
implicit WITH TIES. So in your second example, both the 40th and the
41st records have the same Reject Amt. Similarly in your 3rd example
records 40-110 all have the same Batch Date.

HTH
-Chris Hohmann
Jul 19 '05 #3
On Tue, 16 Dec 2003 12:16:50 -0500, "Bryan Harrington"
<ne**@psacake.c om> wrote:
Can you use Select top 40 * from ... and ORDER BY... in the same query?
Yes. Or no. (Depends on database and version...)
I have the following 4 quearies that are.. for the most part, the same
except the order by clause, all return a differnt amount of records.
Thoughts?


TOP 40 might return more than 40 on an ORDER BY if you have duplicates
in whatever field you order by. In other words, if you ORDER BY Price
and have 81 items at $1.00, *which* are the top 40?

Jeff
Jul 19 '05 #4
> TOP 40 might return more than 40 on an ORDER BY if you have duplicates
in whatever field you order by. In other words, if you ORDER BY Price
and have 81 items at $1.00, *which* are the top 40?


In SQL Server at least, you can alter this behavior by the optional WITH
TIES clause. I think Access just uses WITH TIES by default and you can't
override it, but I'm not certain; likewise, I'm not sure if similar optional
clauses are available in other RDBMSes.

And FWIW, TOP without ORDER BY makes absolutely no sense in a relational
database. You may as well say TOP 40 ARBITRARY...

--
Aaron Bertrand
SQL Server MVP
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
9767
by: war_wheelan | last post by:
I am very new to Transact-SQL programming and don't have a programming background and was hoping that someone could point me in the right direction. I have a SELECT statement SELECT FIXID, COUNT(*) AS IOIs and want to ORDER BY 'IOI's'. I have been combing through the BOL, but I don't even know what topic/heading this would fall under. USE INDII SELECT FIXID, COUNT(*) AS IOIs FROM . GROUP BY FIXID
26
17216
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND SET = :KEY.SET AND DATE <= :KEY.DATE
29
27614
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2 From SomeOtherTable Order By Col2, Col1 Select * from #TempPaging I can't provide a reproduceable scenario right now without making this
6
13580
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214, SQLSTATE: 42822, SQLERRMC: CASE...;ORDER BY;2 Message: An expression in the ORDER BY clause in the following position, or starting with "CASE..." in the "ORDER BY" clause is not valid. Reason code = "2".  More exceptions ... DB2 SQL error:...
5
9994
by: Nick Weisser | last post by:
Hi there, I'm not sure how to select the last 3 items in ascending order. This does the trick in descending order: select * from user_menu_main where deleted = 0 and hidden = 0 order by date desc limit 3
48
4274
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) * -1 Set oConn=Server.CreateObject("ADODB.Connection") Set oRS=Server.CreateObject("ADODB.recordset") oConn.Provider="Microsoft.Jet.OLEDB.4.0" oConn.Open Server.MapPath("temp.mdb")
6
2200
by: tuxedo | last post by:
I have a fixed html structure, where only one form and a simple select menu will exist on an html page, as follows: <form action="order" method="POST"> <select name="dinner"> <option value="1">Pizza</option> <option value="2">Hot Vindaloo</option> <option value="3">Fish-n-Chips</option> <option value="4">Currywurst</option>
0
7501
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i can make report from my database on the one sheet rather than querying each select statement ( I will use MS Query from the Excel) ========================================================= 1) select convert(varchar, create_date_time, 112) as Date,...
5
3728
by: ebsch94 | last post by:
I am executing the following code below. When we execute this on our TEST system (Redhat 4.0, DB2 V8.2 FP11) the rows are returned in order of dollar_range_from. We just moved our Production system from V7.2 (Redhat 2.1) to a RedHat 4.0 64-bit/Db2 V8.2 64-bit FP14 enviromnment and now the below code returns the rows out of order from the temp table even though they were inserted with an order by.
4
10683
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from products_description pd,...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.