question

db042190 avatar image
db042190 asked

joining a lookup on row start date

Hi , we run 2014 STD. I have an account status lookup table with changing statuses by row start date. i have a fact table whose create date and account needs to join to the one/first most appropriate "in effect" account lookup row based on fact create date and lookup row start.

You can see the creates, inserts and my lame attempt at doing this in the attached. in the sub select, the where clause is invalid. if i comment that out and instead put the predicate in the on clause, i only get one hit.

what is the correct syntax for this class of problem?

fact create date >= row start date on the first account record whose account is the same as fact account and whose row start dates are sorted descending is what i think i want

i suppose i could attach a row end date to each dim account record using the lag function and storing the results in a temp table would be one way of going about this. then the on condition could be

da1.account=f.account and f.createdate>da1.rowstartdate and f.createdate<=da1.rowendatae...and the join would be straight to the temp, not a sub select. i purposely said > instead of >= so a create date cant match on 2 dim account records. Honestly i'd like to avoid this lag approach.

,top1Question.sql


sql2014transact
top1question.sql (1.8 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
db042190 avatar image
db042190 answered

Here is basically what I did. I don't know if its the best performing approach out there ... no guarantees here...



10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.