Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select statement

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2012-08-22 : 13:20:11
I have the following records in a table

How do I write a script that will select the latest record (only one) that has the TERM_DATE between 1/1/12 and 7/31/12.

DATE_TIME NUMBER EFF_DATE EXP_DATE TERM_DATE
2/29/2008 C 10102 2/20/2008 2/20/2009 NULL
8/22/2008 C 10102 2/20/2008 2/20/2009 NULL
2/17/2009 C 10102 2/20/2009 2/20/2010 NULL
11/9/2009 C 10102 2/20/2010 2/20/2011 NULL
11/9/2009 C 10102 2/20/2010 2/20/2011 NULL
9/15/2010 C 10102 2/20/2010 2/20/2011 NULL
11/12/2010 C 10102 2/20/2011 2/20/2012 NULL
11/18/2011 C 10102 2/20/2012 2/20/2013 NULL
11/18/2011 C 10102 2/20/2012 2/20/2013 NULL
12/29/2011 C 10102 2/20/2012 2/20/2013 2/20/2012
12/29/2011 C 10102 2/20/2011 2/20/2012 NULL

2/19/2009 C 10344 2/2/2009 2/2/2010 NULL
2/27/2009 C 10344 2/2/2009 2/2/2010 NULL
11/2/2009 C 10344 2/2/2010 2/2/2011 NULL
11/2/2009 C 10344 2/2/2010 2/2/2011 NULL
11/1/2010 C 10344 2/2/2011 2/2/2012 NULL
6/8/2011 C 10344 2/2/2011 2/2/2012 NULL
11/1/2011 C 10344 2/2/2012 2/2/2013 NULL
3/9/2012 C 10344 2/2/2012 2/2/2013 NULL
3/9/2012 C 10344 2/2/2012 2/2/2013 NULL
6/21/2012 C 10344 2/2/2012 2/2/2013 3/1/2012
6/21/2012 C 10344 2/2/2012 2/2/2013 NULL
6/21/2012 C 10344 2/2/2012 2/2/2013 NULL
6/21/2012 C 10344 2/2/2012 2/2/2013 3/1/2012

4/20/2011 C 10711 3/15/2011 3/15/2012 NULL
12/16/2011 C 10711 3/15/2012 3/15/2013 NULL
12/16/2011 C 10711 3/15/2012 3/15/2013 NULL
12/16/2011 C 10711 3/15/2012 3/15/2013 NULL
1/6/2012 C 10711 3/15/2012 3/15/2013 NULL
2/28/2012 C 10711 3/15/2011 3/15/2012 NULL
2/28/2012 C 10711 3/15/2011 3/15/2012 NULL
2/28/2012 C 10711 3/15/2012 3/15/2013 NULL
3/13/2012 C 10711 3/15/2011 3/15/2012 NULL
3/13/2012 C 10711 3/15/2011 3/15/2012 3/1/2012
3/13/2012 C 10711 3/15/2012 3/15/2013 3/15/2012




xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-22 : 13:32:48
This should do the work:

select MAX(column)
from yourtable
where column> '1/1/12' and column<'7/31/12' and column is not null
OR
select MAX(column)
from dates
where column between '1/1/12' and '7/31/12' and column is not null

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-08-22 : 13:48:11
THIS ONLY SELECTS ONE RECORD.. I NEED THE SCRIPT TO SELECT ONE RECORD FROM EACH NUMBER IN OTHER WORDS AFTER THE SCRIPT IS RUN I NEED TO HAVE THE FOLLOWING DISPLAYED

12/29/2011 C 10102 2/20/2012 2/20/2013 2/20/2012
6/21/2012 C 10344 2/2/2012 2/2/2013 3/1/2012
3/13/2012 C 10711 3/15/2012 3/15/2013 3/15/2012
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-22 : 14:05:50
[code]select NUMBER,MAX(column)
from yourtable
where column> '1/1/12' and column<'7/31/12' and column is not null
group by NUMBER
OR
select NUMBER,MAX(column)
from dates
where column between '1/1/12' and '7/31/12' and column is not null
group by NUMBER [/code]

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-22 : 14:39:20
[code]
SELECT DATE_TIME, NUMBER, EFF_DATE, EXP_DATE, TERM_DATE
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY NUMBER ORDER BY EXP_DATE DESC,TERM_DATE DESC) AS Seq,*
FROM Table
)t
WHERE Seq=1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -