Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Sybase > HAVING column=MAX(column) ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-28-07, 17:54
teterin teterin is offline
Registered User
 
Join Date: Apr 2003
Posts: 62
Question HAVING column=MAX(column) ?

Hello!

I'd like to be able to extract the most recent entries from the table and the construct in subject works with Sybase. For example:

Code:
select * from _DBF having M_IDENTITY=MAX(M_IDENTITY)

When trying to port the same query to the SQL-server, however, I get an error:

Code:
1> select * from dbo.t_mx_bond_quotes having ID=MAX(ID) 2> go Msg 8121, Level 16, State 1, Server ADSRV215, Line 1 Column 'dbo.t_mx_bond_quotes.ID' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

This suggests, my query is somehow not quite fully standard -- how do I change it to be proper SQL?

Thanks!
__________________
If you ever back up Sybase, you want this backup-server plugin.
Reply With Quote
  #2 (permalink)  
Old 11-28-07, 23:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Code:
select * from dbo.t_mx_bond_quotes where ID = ( select MAX(ID) from dbo.t_mx_bond_quotes )
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 11-28-07, 23:50
teterin teterin is offline
Registered User
 
Join Date: Apr 2003
Posts: 62
Quote:
Originally Posted by r937
Code:
select * from dbo.t_mx_bond_quotes where ID = ( select MAX(ID) from dbo.t_mx_bond_quotes )

Yes, I know, but this scans the table twice -- the number of "logical reads" equals twice the number of rows. Is there really no way to avoid a subquery? Thanks!
__________________
If you ever back up Sybase, you want this backup-server plugin.
Reply With Quote
  #4 (permalink)  
Old 11-29-07, 00:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
put an index on ID and the subquery should do an index seek (or whatever that's called in sybase)

alternatively,
Code:
select top 1 * from dbo.t_mx_bond_quotes order by ID desc
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On