943,929 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 4379
  • MS SQL RSS
May 12th, 2008
0

SELECT only the latest date yet within the defined period

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonyheng is offline Offline
4 posts
since May 2008
May 12th, 2008
0

Re: SELECT only the latest date yet within the defined period

hi stonyheng,

a simple way to archive a result is a correlated select like this:
sql Syntax (Toggle Plain Text)
  1. SELECT * FROM date_table x
  2. WHERE out_date =
  3. (SELECT max(out_date) FROM date_table y WHERE x.name = y.name)
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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
May 12th, 2008
0

Re: SELECT only the latest date yet within the defined period

hi again,

this achieves it:
sql Syntax (Toggle Plain Text)
  1. with temp_table ( name, in_date, out_date )
  2. as ( SELECT name, in_date, coalesce(out_date, '9090-09-09')
  3. as out_date FROM date_table)
  4. SELECT * FROM temp_table x
  5. WHERE out_date = (SELECT max(out_date) FROM temp_table y
  6. WHERE x.name = y.name)
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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
May 13th, 2008
0

Re: SELECT only the latest date yet within the defined period

Click to Expand / Collapse  Quote originally posted by tesuji ...
hi again,

this achieves it:
sql Syntax (Toggle Plain Text)
  1. with temp_table ( name, in_date, out_date )
  2. as ( SELECT name, in_date, coalesce(out_date, '9090-09-09')
  3. as out_date FROM date_table)
  4. SELECT * FROM temp_table x
  5. WHERE out_date = (SELECT max(out_date) FROM temp_table y
  6. WHERE x.name = y.name)
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
Hey tesu, thanx alot for the suggestion. Really appreciate that. Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonyheng is offline Offline
4 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Problem using JOIN with six tables
Next Thread in MS SQL Forum Timeline: SQL 2000 database table replication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC