| | |
SELECT only the latest date yet within the defined period
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
Hi all experts,
Currently i have a problem coding in sql query and hope someone expert can answer me.
I have a table with the following values (simplified)
Name in_date out_date
able 12/03/2008 14/03/2008
bravo 10/02/2008 17/03/2008
able 18/03/2008 null
Ok so my result if i want to obtain results for a period from 01/03/2008 to 31/03/2008
able 18/03/2008
bravo 10/02/2008 17/03/2008
SO how do we code for this? thanx
Currently i have a problem coding in sql query and hope someone expert can answer me.
I have a table with the following values (simplified)
Name in_date out_date
able 12/03/2008 14/03/2008
bravo 10/02/2008 17/03/2008
able 18/03/2008 null
Ok so my result if i want to obtain results for a period from 01/03/2008 to 31/03/2008
able 18/03/2008
bravo 10/02/2008 17/03/2008
SO how do we code for this? thanx
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
hi stonyheng,
a simple way to archive a result is a correlated select like this:
Unfortunately this does not work properly if you have periods where out_date is not set, that is where out_date is NULL. I will think it over how to factor in NULL values, maybe there is a solution with coalesce(out_date, NOW(*), out_date).
krs,
tesu
a simple way to archive a result is a correlated select like this:
sql Syntax (Toggle Plain Text)
SELECT * FROM date_table x WHERE out_date = (SELECT max(out_date) FROM date_table y WHERE x.name = y.name)
krs,
tesu
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
hi again,
this achieves it:
With WITH clause table date_table will be duplicated and all null values of out_date replaced by funny date 9th September, 9090. Replacement is necessary because max() ignores NULL values. I didn't test it with MS SQL Server, but I know SQL Server 2005 also features WITH clause and coalesce function. Possibly date constant must be rewritten, or replace it by getdate().
You can give me a reply, if this also works on MS SQL Server 2005 properly.
krs,
tesu
this achieves it:
sql Syntax (Toggle Plain Text)
with temp_table ( name, in_date, out_date ) as ( SELECT name, in_date, coalesce(out_date, '9090-09-09') as out_date FROM date_table) SELECT * FROM temp_table x WHERE out_date = (SELECT max(out_date) FROM temp_table y WHERE x.name = y.name)
You can give me a reply, if this also works on MS SQL Server 2005 properly.
krs,
tesu
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
hi again,
this achieves it:
With WITH clause table date_table will be duplicated and all null values of out_date replaced by funny date 9th September, 9090. Replacement is necessary because max() ignores NULL values. I didn't test it with MS SQL Server, but I know SQL Server 2005 also features WITH clause and coalesce function. Possibly date constant must be rewritten, or replace it by getdate().sql Syntax (Toggle Plain Text)
with temp_table ( name, in_date, out_date ) as ( SELECT name, in_date, coalesce(out_date, '9090-09-09') as out_date FROM date_table) SELECT * FROM temp_table x WHERE out_date = (SELECT max(out_date) FROM temp_table y WHERE x.name = y.name)
You can give me a reply, if this also works on MS SQL Server 2005 properly.
krs,
tesu
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: Problem using JOIN with six tables
- Next Thread: SQL 2000 database table replication
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last autogrowth business connectingtodatabaseinuse count cursor data database dateadd datepart day" dbsize deadlock delete_trigger getdate highperformancecomputing hpc hpcserver2008 ibm iis limit loop maximum microsoft ms mssql multiple multithreading news number permission position query reporting result server services sets single source sql sqlserver sqlserver2005 supercomputing tables uniqueid update view weekday





