| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-03 : 11:23:13
|
| I am trying to link a table A to table B using service_dt : service_dt closer to asessment_dt Table AReg_key service_dt1 07/01/072 08/11/07 3 07/23/07Table BReg_key ID assessment_dt1 1001 07/11/071 1111 07/01/072 1012 08/01/072 1013 08/30/072 1187 09/12/073 657 07/20/073 789 07/20/073 1999 09/11/07I am looking for a matching assessment_dt.. if it doesn;t exist .. i have to grab the closer assessment_dt..if there is more than one same record , then grap the lowest ID..so the output should look likeREg_key service_dt ID assessment_dt1 07/01/07 1111 07/01/072 08/11/07 1012 08/01/072 07/23/07 657 07/20/07 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-03 : 11:52:58
|
| [code]SELECT tmp.Reg_Key,tmp.service_dt,MIN(t.ID) AS ID,t.assessment_dtFROM TableB tINNER JOIN (SELECT a.Reg_Key,a.service_dt,b.assessment_dtFROM [Table A] aCROSS APPLY (SELECT MAX(assessment_dt) AS assessment_dt FROM Table B WHERE Reg_key=a.Reg_key AND assessment_dt<=service_dt)b)tmpON tmp.Reg_Key=t.Reg_KeyAND tmp.assessment_dt=t.assessment_dtGROOUP BY tmp.Reg_Key,tmp.service_dt,t.assessment_dt[/code] |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-03 : 12:23:25
|
| Thank you ..can you explain to me what CROSS Apply doing in this query..? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-03 : 12:45:03
|
quote: Originally posted by jung1975 Thank you ..can you explain to me what CROSS Apply doing in this query..?
It gets for each record in table A the maximun assessment_dt value which is either equal to or just less than service_dt value. |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-03 : 12:55:43
|
| problem is there can be a record greater than the service date ..but closer than the earier date... please see the below example..Table AReg_key service_dt1 07/01/072 08/11/07 3 07/23/074 08/05/07Table BReg_key ID assessment_dt1 1001 07/11/071 1111 07/01/072 1012 08/01/072 1013 08/30/072 1187 09/12/073 657 07/20/073 789 07/20/073 1999 09/11/074 566 07/11/074 777 07/20/074 888 08/07/074 889 08/11/07REg_key service_dt ID assessment_dt1 07/01/07 1111 07/01/072 08/11/07 1012 08/01/073 07/23/07 657 07/20/074 08/05/07 888 08/07/07Do i have to use some kind of date diff function to compare the previous dates and next dates? how can i do this? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-03 : 13:11:03
|
Then try this:-SELECT tmp.Reg_Key,tmp.service_dt,MIN(t.ID) AS ID,t.assessment_dtFROM TableB tINNER JOIN (SELECT a.Reg_Key,a.service_dt,CASE WHEN DATEDIFF(dd,ISNULL(b.assessment_dt,a.service_dt),a.service_dt) < DATEDIFF(dd,a.service_dt,ISNULL(c.assessment_dt,a.service_dt)) THENb.assessment_dtELSE c.assessment_dt END AS assessment_dtFROM [Table A] aOUTER APPLY (SELECT MAX(assessment_dt) AS assessment_dt FROM Table B WHERE Reg_key=a.Reg_key AND assessment_dt<=a.service_dt)bOUTER APPLY (SELECT MIN(assessment_dt) AS assessment_dt FROM Table B WHERE Reg_key=a.Reg_key AND assessment_dt>a.service_dt)c)tmpON tmp.Reg_Key=t.Reg_KeyAND tmp.assessment_dt=t.assessment_dtGROUP BY tmp.Reg_Key,tmp.service_dt,t.assessment_dt |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-03 : 19:27:21
|
| thanks.. but i am getting the null values even though the date fall into one of cross apply condition .. i am not sure why?.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-03 : 21:46:58
|
quote: Originally posted by jung1975 thanks.. but i am getting the null values even though the date fall into one of cross apply condition .. i am not sure why?..
Ok. Make slight modification and try:-SELECT tmp.Reg_Key,tmp.service_dt,MIN(t.ID) AS ID,t.assessment_dtFROM TableB tINNER JOIN (SELECT a.Reg_Key,a.service_dt,CASE WHEN b.assessment_dt IS NULL THEN c.assessment_dtWHEN c.assessment_dt IS NULL THEN b.assessment_dtWHEN DATEDIFF(dd,b.assessment_dt,a.service_dt) < DATEDIFF(dd,a.service_dt,c.assessment_dt) THENb.assessment_dtELSE c.assessment_dt END AS assessment_dtFROM [Table A] aOUTER APPLY (SELECT MAX(assessment_dt) AS assessment_dt FROM Table B WHERE Reg_key=a.Reg_key AND assessment_dt<=a.service_dt)bOUTER APPLY (SELECT MIN(assessment_dt) AS assessment_dt FROM Table B WHERE Reg_key=a.Reg_key AND assessment_dt>a.service_dt)c)tmpON tmp.Reg_Key=t.Reg_KeyAND tmp.assessment_dt=t.assessment_dtGROUP BY tmp.Reg_Key,tmp.service_dt,t.assessment_dt |
 |
|
|
|