Date range issues

  • Morning all, Im new to this site and SQL in general, however I have been given a challenge to write some reports, and I have hit a small issue ok, it goes like this...

    table 1 contains:

    initials, effective date, rate per hour

    aaaa, 01/01/05, 150

    aaaa, 01/07/05, 200

    aaaa, 01/09/05, 275 and so on, so all charges between 01/01/05 and 30/06/05 standard rate per hour would be 150....

    table 2 contains

    initials, date, adjusted rate per hour

    aaaa, 01/03/05, 120

    aaaa, 01/04/05, 135

    What I need to get out of the system would be each table 2 row with the corresponding 'rate per hour' for that date from table 1.

    any ideas?

    Many thanks

    Mike

  • -- *** Test Data ***

    DECLARE @t1 TABLE

    (

    &nbsp&nbsp&nbsp&nbspInitials varchar(10) NOT NULL

    &nbsp&nbsp&nbsp&nbsp,EffectiveDate datetime NOT NULL

    &nbsp&nbsp&nbsp&nbsp,RatePerHour money NOT NULL

    )

    INSERT INTO @t1

    SELECT 'aaaa', '20050101', 150 UNION ALL

    SELECT 'aaaa', '20050705', 200 UNION ALL

    SELECT 'aaaa', '20050901', 275

    DECLARE @t2 TABLE

    (

    &nbsp&nbsp&nbsp&nbspInitials varchar(10) NOT NULL

    &nbsp&nbsp&nbsp&nbsp,EffectiveDate datetime NOT NULL

    &nbsp&nbsp&nbsp&nbsp,AdjustedRatePerHour money NOT NULL

    )

    INSERT INTO @t2

    SELECT 'aaaa', '20050301', 120 UNION ALL

    SELECT 'aaaa', '20050401', 135

    -- *** End Test Data ***

    SELECT T2.*, T1.RatePerHour

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT T21.Initials, T21.EffectiveDate, MAX(T11.EffectiveDate) AS T1Date

    &nbsp&nbsp&nbsp&nbspFROM @t2 T21

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN @t1 T11

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON T21.Initials = T11.Initials

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T11.EffectiveDate <= T21.EffectiveDate

    &nbsp&nbsp&nbsp&nbspGROUP BY T21.Initials, T21.EffectiveDate

    ) D

    &nbsp&nbsp&nbsp&nbspJOIN @t1 T1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON D.Initials = T1.Initials

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND D.T1Date = T1.EffectiveDate

    &nbsp&nbsp&nbsp&nbspJOIN @t2 T2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON D.Initials = T2.Initials

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND D.EffectiveDate = T2.EffectiveDate

    or with SQL2005

    SELECT Initials, EffectiveDate, AdjustedRatePerHour, RatePerHour

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspROW_NUMBER() OVER (PARTITION BY T2.Initials, T2.EffectiveDate ORDER BY T1.EffectiveDate DESC) AS Row

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp,T2.*

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp,T1.RatePerHour

    &nbsp&nbsp&nbsp&nbspFROM @t1 T1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN @t2 T2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON T1.Initials = T2.Initials

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T1.EffectiveDate <= T2.EffectiveDate

    ) D

    WHERE Row =1

  • This is just a pointer I've slapped up fast:

    [Code]SET DATEFORMAT DMY

    DROP TABLE TABLE1

    DROP TABLE TABLE2

    CREATE TABLE TABLE1 (INITS VARCHAR(10), EFFDATE DATETIME, RATE NUMERIC)

    INSERT INTO TABLE1 (INITS, EFFDATE, RATE)

    SELECT 'aaaa', '01/01/2005', 150 UNION ALL

    SELECT 'aaaa', '01/07/2005', 200 UNION ALL

    SELECT 'aaaa', '01/09/2005', 275

    CREATE TABLE TABLE2 (INITS VARCHAR(10), ADJDATE DATETIME, ADJRATE NUMERIC)

    INSERT INTO TABLE2 (INITS, ADJDATE, ADJRATE)

    SELECT 'aaaa', '01/03/2005', 120 UNION ALL

    SELECT 'aaaa', '01/04/2005', 135

    GO

    -- I need to get the corresponding 'rate per hour' from TABLE2 for the date from TABLE1

    SELECT b.INITS, b.ADJDATE, b.ADJRATE, a.RATE

    FROM TABLE2 b

    INNER JOIN TABLE1 a

    ON b.ADJDATE >= a.EFFDATE

    AND b.INITS = a.INITS[/Code]


    Regards,

    goodguy

    Experience is a bad teacher whose exams precede its lessons

  • thanks for all your help guys, but unfortunatly although this will work, it would take me about a year to write this query, there are many thousands of rows in table one, and hundreds of thousands in table two. If anyone can think of a way of getting the needed output without having to do untold numbers of unions that would be great! oh, and just to make it a real pain, the periods between the effective dates are not constant per person (initials) within the table!:w00t:

    thanks again

    Mike

  • The UNIONS are test data! (Read the comments.)

    Just use the query and replace the test tables with your tables.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply