Solved

Sql Query using except order by

Posted on 2009-04-12
8
1,902 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
8 Comments
 
LVL 39

Expert Comment

by:appari
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:lwadwell
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility

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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now