Last post Oct 31, 2008 08:01 AM by ramireddyindia
Oct 29, 2008 05:26 AM|irene0711|LINK
Can anyone tell me what is the sql statement for getting the latest date set of record from a table.It's urgent and help will be appreciated.
Oct 29, 2008 05:43 AM|ramireddyindia|LINK
select * from tablename where datecolumn = (select max(datecolumn) from tablename)
Oct 29, 2008 11:33 AM|Naom|LINK
Same thing can be done with derived tables, especially if you need to get latest date for each group:
select myTable.* from myTable inner join (select max(date) as MaxDate from myTable) Max_Date on myTable.Date = Max_Date.MaxDate
There is another nice solution for this problem using a compound key. This is if you may have multiple records with the same max date and need to select only one record (probably last added). I'll post the other solution later.
Oct 31, 2008 07:55 AM|irene0711|LINK
dr = cmdDiastolic.ExecuteReader()
Oct 31, 2008 08:01 AM|ramireddyindia|LINK
First Don't concatenate your query with input from textbox text. Use parameterized approach.
see this tutorial, how to use parameters
i didn't understand your intention. the query may be like
select dia from BloodPressure where NRIC =
@NRIC AND dateTaken= (select max(datetaken) from BloodPRessure)
select dia,max(datetaken) from BloodPressure where NRIC = @NRIC group by dia
first query, will check the entire table for max date, where as second table check in that group "dia"