Selecting record based on max date

Reply

Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Selecting record based on max date

 
0
  #1
Mar 10th, 2009
I am working on an ASP.Net website with SQL Server database and C# 2005 as the programming language.

In my database I have two fields namely RDate and RTime. RDate stores the date of the record and RTime stores a time like 09:00 AM, 09:15 AM, 09:30 AM etc. The RTime field is in char data type. I also have a SrNo field of int data type and of identity property. Earlier I used to use the SrNo field to select the latest record. But unfortunately, the user added records in no particular order and my logic failed (this was an oversight from my part). Now I am not sure how to select the record (only 1 record) with the latest RDate and RTime value. (I have taken care to ensure that for a combination of RDate + RTime only one record can be added).

I think I have to use two select statements. One as max(RDate) to get the latest RDate and then use that value with another select statement to get the latest RTime. But I am not sure how to implement it.

Lalit Kumar Barik
India
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Selecting record based on max date

 
0
  #2
Mar 11th, 2009
I am using the following query:

  1. SELECT * FROM Results RT1
  2. WHERE RT1.RDate = (SELECT max(RT2.RDate) FROM Results RT2)
  3. AND RT1.RTime = (SELECT max(RT3.RTime) FROM Results RT3)
  4. WHERE RT3.RDate = (SELECT max(RT4.RDate) FROM Results RT4)

and I am getting the following error:

  1. Msg 156, Level 15, State 1, Line 4
  2. Incorrect syntax near the keyword 'where'.

Where am I going wrong??

Please help me.

Lalit Kumar Barik
Last edited by peter_budo; Mar 18th, 2009 at 7:25 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Selecting record based on max date

 
1
  #3
Mar 11th, 2009
i'm not quite sure what you are trying to do here, but there are 2 things

first you are referencing R3 outside of its scope, second you have a where instead of an and, here is my shot at what i think you are trying to do

  1. SELECT * FROM Results RT1
  2. WHERE RT1.RDate = (SELECT max(RT2.RDate) FROM Results RT2)
  3. AND RT1.RTime =
  4. (SELECT max(RT3.RTime) FROM Results RT3
  5. WHERE RT3.RDate = (SELECT max(RT4.RDate) FROM Results RT4))
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Selecting record based on max date

 
0
  #4
Mar 11th, 2009
@"dickersonka". Thank you for pointing out the mistake in placing the crucial closing parenthesis in the proper place. Now the query is giving the proper result.

I wrote anothe query as :

  1. SELECT top 1 * FROM Results RT1
  2. WHERE RT1.RDate = (SELECT max(RT2.RDate) FROM Results RT2)
  3. ORDER BY cast(RTime as smalldatetime) desc

and still another as :

  1. SELECT TOP 1 * FROM Results ORDER BY RDate DESC, cast(RTime as smalldatetime) DESC

ALL 3 queries above are giving the same result. Which one is better in terms of accuracy and performance??

Thank you.

Lalit Kumar Barik
Last edited by peter_budo; Mar 18th, 2009 at 7:27 am. Reason: Please use [code][/code] instead of [quote][/quote]
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Selecting record based on max date

 
0
  #5
Mar 11th, 2009
Is the query taking that long to execute anyway?

I would suggest the bottom one, the less tables that are involved generally the more speed achieved, if performance is really that big of an issue with this query, then the cast will be slower but I don't think its that big of a deal here
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum


Views: 1701 | Replies: 4
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC