sql server - Join two tables where a date is in the other tables date range -


i'm trying write sql in ssms join 2 tables date.

table holds actual date transaction happened

    select  a.client,     a.trndate,        dbo.transation      client      trndate     00011547    2001-07-17 00:00:00.000     00011547    2013-07-04 00:00:00.000     00011547    2013-11-13 00:00:00.000     00011547    2014-12-14 00:00:00.000     00011547    2014-12-14 00:00:00.000 

table b holds rates transactions should cost effective date.

     select     b.client,         b.effdate,         b.rate            dbo.chargerate b       client      effdate                 rate     00011547    2008-04-01 00:00:00.000 1223     00011547    2013-05-28 00:00:00.000 1224     00011547    2013-10-01 00:00:00.000 1302     00011547    2014-01-01 00:00:00.000 1355     00011547    2014-04-01 00:00:00.000 1376     00011547    2014-07-01 00:00:00.000 1397 

as can see transaction date can fall inbetween effective periods joined table below.

    client      trndate                  rate     00011547    2001-07-17 00:00:00.000  1223     00011547    2013-07-04 00:00:00.000  1224     00011547    2013-11-13 00:00:00.000  1302     00011547    2014-12-14 00:00:00.000  1397     00011547    2014-12-14 00:00:00.000  1397 

i appreciate help.

thanks in advance phil

you can easly apply operator

select     [t].[client],     [t].[trndate],     [cr].[rate] dbo.transaction [t] outer apply (     select top 1         [cr].[rate]     dbo.chargerate [cr]             [cr].effdate <= [trndate]     order         [effdate] desc ) [cr] 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -