Solved

Sql Query using except order by

Posted on 2009-04-12
8
2,037 Views
Last Modified: 2013-12-07
Hi ,

I am using Sql Server 2005

What I want is I am having two queries
I want to retrieve the links other than first 15.

Total links - first 15 links. I want to use order by in both the queries.
Order by not working with except.

thanks


select * from Links order by DateStamp asc
except
select top 15 * from Links order by DateStamp asc

Open in new window

0
Comment
Question by:Saroj13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 39

Expert Comment

by:appari
ID: 24127481
try this


Select * from (select top 100 percent * from Links order by DateStamp asc) A
except
Select * from (select  top 15 * from Links order by DateStamp asc) A
order by DateStamp asc
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24127492
Hi Saroj13,

The order by would be applied to each SQL prior to the except - after the except the order can be different.  To get the order you want you should do an order by on the result , i.e. SELECT * FROM (your query) as v ORDER BY DateStamp asc.  In this case the ORDER BY in your top SQL may be unnecessary.

lwadwell
0
 

Author Comment

by:Saroj13
ID: 24127647
Hi lwadwell:,

I need order by in both the queries.

What I want is . I want all the data according to date.
Then i want top 15 from this all data. Then I want to subtract it.

Thanks
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 25

Expert Comment

by:lwadwell
ID: 24127664
Saroj13,

I understand that you need the ORDER BY for the TOP 15 ... that is fine.  The EXCEPT returns those rows from the top query not returned from the bottom query - it does not need the top query to be sorted (it will do that itself).  I was suggesting:

SELECT * FROM (
select * from Links
except
select top 15 * from Links order by DateStamp asc
) as v ORDER BY DateStamp asc

lwadwell
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 24127715

There are various ways of doing this. You can try appari post. You can also try like this.
SELECT *
  FROM Links
 WHERE NOT EXISTS (SELECT TOP 15 * FROM Links ORDER BY DateStamp DESC)
 
Or like this
SELECT *
  FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY DateStamp DESC) AS RN FROM Links) T1
 WHERE T1.RN > 16
0
 
LVL 2

Expert Comment

by:TejasShahMscIT
ID: 24128125
Hi,

Try this:


SELECT * 
FROM (
select * from Links 
except
select top 15 * from Links order by DateStamp asc
) x 
ORDER BY order by DateStamp asc
 
Thanks,
Tejas

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24128634
this would do also:
select * from Links 
where primary_key_field not in (  select top 15 primary_key_field from Links order by DateStamp asc )
order by DateStamp asc

Open in new window

0
 
LVL 3

Expert Comment

by:servicegroup
ID: 24196513
I just stumbed upon something while trying to fix a similar problem.  You can order by SQL's name for this field.  In my case I had, SELECT field1 AS field1, field2 AS field2...Except...

I ordered by 00004 (the 4th field) at the end of the statement.

I hope this helps someone
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question