473,837 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can we use two orderBy in single Select statement ?

50 New Member
Hi every one !

Im workin on SQL05, My Question is !

i have a single field "PosId" in the DB,which i use as two fields one as PosId and PosID2 , Even though Both PosID & PosID2 are from the same fields ,i want to sort the PosID in ascending and PosID2 in desc,
This is what i tried , But ???

"SELECT PosID,PosID as PosID2
FROM tableXX
WHERE (condition = 'XX')
ORDER BY PosID ASC,PosID2 DESC"

Can we use two order by in a single select statement ?
Or is there any alternative .

Plz provide me with a valid soln.
Thanks in advance,
Apr 18 '08 #1
3 1474
ck9663
2,878 Recognized Expert Specialist
Hi every one !

Im workin on SQL05, My Question is !

i have a single field "PosId" in the DB,which i use as two fields one as PosId and PosID2 , Even though Both PosID & PosID2 are from the same fields ,i want to sort the PosID in ascending and PosID2 in desc,
This is what i tried , But ???

"SELECT PosID,PosID as PosID2
FROM tableXX
WHERE (condition = 'XX')
ORDER BY PosID ASC,PosID2 DESC"

Can we use two order by in a single select statement ?
Or is there any alternative .

Plz provide me with a valid soln.
Thanks in advance,
I don't know what you're trying to accomplished. But if that's really your requirement, the answer is NO. You can not do it in a single select statement. You can, however, do it using subquery

Something like:

Expand|Select|Wrap|Line Numbers
  1. select * from 
  2. (SELECT     PosID,PosID as PosID2
  3. FROM         tableXX
  4. WHERE     (condition = 'XX')) YourTableXX
  5. ORDER BY PosID ASC,PosID2 DESC
  6.  
-- CK
Apr 18 '08 #2
hellboss
50 New Member
I don't know what you're trying to accomplished. But if that's really your requirement, the answer is NO. You can not do it in a single select statement. You can, however, do it using subquery

Something like:

Expand|Select|Wrap|Line Numbers
  1. select * from 
  2. (SELECT     PosID,PosID as PosID2
  3. FROM         tableXX
  4. WHERE     (condition = 'XX')) YourTableXX
  5. ORDER BY PosID ASC,PosID2 DESC
  6.  
-- CK
Thanks for your response, Well ! it worked partialy in displaying the same field in the data set but, one should be in ascending and other should be in descending order !

The above code didnt sort the PosID in desc order ?
Jun 3 '08 #3
ck9663
2,878 Recognized Expert Specialist
Could you post some sample data?

If you are sorting the same field in different order, it will not work. Since your POSID will always be equal to POSID2 (since POSID2 is an alias), it will always be sorted by POSID.

-- CK
Jun 3 '08 #4

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

Similar topics

5
11460
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. Queston: How do you handle a string that contains both single & double quotes (i.e. 12'X7") Here's the function:
6
6962
by: P. Emigh | last post by:
By default in more recent versions, Access forms keep users' last sort request in the "orderby" property. That can slow things down considerably, especially when the last user has chosen a non-indexed field. I have put in a lot of OrderBy = "" code in OnOpen form events. Being able to disable that "service" (seems like more and more Microsoft insists on helping when we don't need or want the help) would be a much more efficient...
2
1867
by: DFS | last post by:
Access97: I opened a report, then opened a small form that floats above it and lets you choose report fields to sort on (by issuing the OrderBy statement). Well, apparently OrderBy actually closes and reopens the report in the chosen sort order, because my Report_Close and Report_Open events were firing each time I set the OrderBy value from the form.
2
6170
by: Todd | last post by:
Hello, I'm curious if anyone knows of a way (if one exists) to tell a form (in Access 2002 VBA) to sort on an unbound column of a combo box on the form. Here's what I want to do: A combo box on my form contains a category ID (bound column, not visible, long integer) for the items listed on the form and a description (unbound column, visible, string.) I can "Sort Ascending" and "Sort Descending" on the visible description in the...
2
3358
by: Dutchy | last post by:
Hi there, After spending several hours trying all I could imagine and search for in Google I gave up. In a continuous form I want to sort the choosen column by clicking the header (label) of that column. I even want to sort up and down if one clicks again on the same header. No problem so far, all works well for one column. Now I want to sort on the first choosen column ASC or DESC and additionally on a second column ASC. I use the...
1
7972
by: fecket | last post by:
The following code is used in my db to change the sort order of the report to various fields. I want to take this one stage further and use maybe a Case Select to give 2 or 3 different options to sort the report. Is ther any way I can adress the report name as is used in strDocName and strSQL so instead of having Reports!.OrderBy = strSQL
7
2994
by: pbd22 | last post by:
Hi. I really need some advice on fine-tuning a stored procedure that is the meat of the search logic on my site. Customers are allowed to save searches, which dumps the search logic in a table called SavedSearches for later access to the search. My problem started with the ORDERBY condition used for zipcode searches. The condition did something like: "order by CASE WHEN userID=67 THEN 1 WHEN userID=103 THEN 2 WHEN
0
2214
by: dhascuba | last post by:
The @name field can contain a single quote in it such as: Mike O'Grady. Since this is creating and SQL statement on the server side, it will not process the name if it has a single quote in it. Id' like to replace that single quote with 2 single quotes without destroying th rest of th data in the field. Currently I replace a single quote with a "^" and then do a replace of that on the server side, see last <zz>. That statement is ecexuted on...
4
3569
by: Marc Gravell | last post by:
Ah right - I just saw your explanation of "Source"; yes - I'd have 2 queries, then: switch(Source) { case "File": viewData.blah = (from t in database.Tags where t.FileCount != 0 orderby t.FileCount select new {...}).ToList(); break;
0
9827
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...
0
9678
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10263
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5838
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4468
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
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
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.