If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > Help With query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-11, 19:41
mark5020 mark5020 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Help With query

Hi,

I have a query to get the latest record in a table as follows:

SELECT DISTINCT max(lcns_startdt) as lcns_startdt, max(appt_stopdt) as appt_stopdt, max(appt_startdt) as appt_startdt, agent_tax_id, co_id, state_id, lob_type
FROM license
GROUP BY agent_tax_id, co_id, state_id, lob_type
HAVING lcns_startdt = max(lcns_startdt) AND
appt_stopdt = max(appt_stopdt) AND
appt_startdt = max(appt_startdt)

Now the user wants to redefine a latest record. They want to first look at the latest appt_startdt field. If there is only one record for agent_tax_id, co_id, state_id, lob_type with the lastest appt_startdt then use that record. If there are two or more records using the latest appt_start_dt then look at the appt_stopdt. If there are two or more records that have the same appt_startdt and appt_stopdt then get the latest lcns_startdt. I am not sure how to code this in SQL. Can someone help me out?


Thanks,

Mark
Reply With Quote
  #2 (permalink)  
Old 10-20-11, 22:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
you're not going to like the ANSI SQL solution (the forum you posted in) because it probably won't work in your database system

what database system are you using?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-21-11, 00:11
mark5020 mark5020 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
I am using Sybase 11.9.2
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On