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.
| Author |
Topic |
|
divan
Posting Yak Master
153 Posts |
Posted - 2012-08-22 : 13:20:11
|
| I have the following records in a tableHow 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_DATE2/29/2008 C 10102 2/20/2008 2/20/2009 NULL8/22/2008 C 10102 2/20/2008 2/20/2009 NULL2/17/2009 C 10102 2/20/2009 2/20/2010 NULL11/9/2009 C 10102 2/20/2010 2/20/2011 NULL11/9/2009 C 10102 2/20/2010 2/20/2011 NULL9/15/2010 C 10102 2/20/2010 2/20/2011 NULL11/12/2010 C 10102 2/20/2011 2/20/2012 NULL11/18/2011 C 10102 2/20/2012 2/20/2013 NULL11/18/2011 C 10102 2/20/2012 2/20/2013 NULL12/29/2011 C 10102 2/20/2012 2/20/2013 2/20/201212/29/2011 C 10102 2/20/2011 2/20/2012 NULL 2/19/2009 C 10344 2/2/2009 2/2/2010 NULL2/27/2009 C 10344 2/2/2009 2/2/2010 NULL11/2/2009 C 10344 2/2/2010 2/2/2011 NULL11/2/2009 C 10344 2/2/2010 2/2/2011 NULL11/1/2010 C 10344 2/2/2011 2/2/2012 NULL6/8/2011 C 10344 2/2/2011 2/2/2012 NULL11/1/2011 C 10344 2/2/2012 2/2/2013 NULL3/9/2012 C 10344 2/2/2012 2/2/2013 NULL3/9/2012 C 10344 2/2/2012 2/2/2013 NULL6/21/2012 C 10344 2/2/2012 2/2/2013 3/1/20126/21/2012 C 10344 2/2/2012 2/2/2013 NULL6/21/2012 C 10344 2/2/2012 2/2/2013 NULL6/21/2012 C 10344 2/2/2012 2/2/2013 3/1/2012 4/20/2011 C 10711 3/15/2011 3/15/2012 NULL12/16/2011 C 10711 3/15/2012 3/15/2013 NULL12/16/2011 C 10711 3/15/2012 3/15/2013 NULL12/16/2011 C 10711 3/15/2012 3/15/2013 NULL1/6/2012 C 10711 3/15/2012 3/15/2013 NULL2/28/2012 C 10711 3/15/2011 3/15/2012 NULL2/28/2012 C 10711 3/15/2011 3/15/2012 NULL2/28/2012 C 10711 3/15/2012 3/15/2013 NULL3/13/2012 C 10711 3/15/2011 3/15/2012 NULL3/13/2012 C 10711 3/15/2011 3/15/2012 3/1/20123/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 yourtablewhere column> '1/1/12' and column<'7/31/12' and column is not nullORselect 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 |
 |
|
|
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 DISPLAYED12/29/2011 C 10102 2/20/2012 2/20/2013 2/20/20126/21/2012 C 10344 2/2/2012 2/2/2013 3/1/20123/13/2012 C 10711 3/15/2012 3/15/2013 3/15/2012 |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-22 : 14:05:50
|
| [code]select NUMBER,MAX(column)from yourtablewhere column> '1/1/12' and column<'7/31/12' and column is not nullgroup by NUMBER ORselect NUMBER,MAX(column)from dates where column between '1/1/12' and '7/31/12' and column is not nullgroup by NUMBER [/code]--------------------------Joins are what RDBMS's do for a living |
 |
|
|
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_DATEFROM(SELECT ROW_NUMBER() OVER (PARTITION BY NUMBER ORDER BY EXP_DATE DESC,TERM_DATE DESC) AS Seq,*FROM Table)tWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|