tags:

views:

36

answers:

1

Hi,

I have two tables in a teradata database that look like this

accounts
account_number integer
date_updated_last datetime
delinquency_code varchar(3)

payments
account_number integer
statement_date datetime
delinquency_code varchar(3)

the delinquency code column is populated in accounts, but is not populated in payments. I would like to update payments with the delinquency code based on date_updated_last and statement_date. the problem is that statement_date is sequential, say a given account was opened in july of 2009, there would be one record for every month between then and now, but an account record is only added when the information changes, so there may be, for example, only 3 records in the account table for the same account. Say, august 2009, january 2010, and march 2010. so I would want to update all the payment records between august 2009 and january 2010 with the data from the august 2009 record in accounts. can anyone point me to an easy way to do this?

thank you :)

-C

+2  A: 

Ok, here's another try at the Teradata syntax:

UPDATE
    Payments
FROM
    (
        SELECT
            A1.account_number,
            A1.date_updated_last AS begin_date,
            A2.date_updated_last AS end_date,
            A1.delinquency_code
        FROM
            Accounts A1
        INNER JOIN Accounts A2 ON
            A2.account_number = A1.account_number AND
            A2.date_updated_last > A1.date_updated_last
        WHERE
            NOT EXISTS
            (
                SELECT *
                FROM
                    Accounts A3
                WHERE
                    A3.account_number = A1.account_number AND
                    A3.date_updated_last > A1.date_updated_last AND
                    A3.date_updated_last < A2.date_updated_last
            )
    ) AS SQ (account_number, begin_date, end_date, delinquency_code)
SET
    delinquency_code = SQ.delinquency_code
WHERE
    account_number = SQ.account_number AND
    statement_date >= SQ.begin_date AND
    statement_date < SQ.end_date
Tom H.
what is A in the set statement?
Chris Drappier
Sorry, corrected that. I started with just A as the table alias, then changed it without changing it in the SET portion.
Tom H.
this does appear to work. thx tom :)
Chris Drappier
actually, i spoke too soon. I tried it as a select first to make sure the values lined up, and they did but the update throws an error : joined tables are not allowed in from clause. also, the set statement has to be right before the where.
Chris Drappier
Ugh... what a lousy translation(?) of SQL. It looks like you might be stuck using subqueries (which would be a mess) or using an outside application to do the conversion for you. I could write a correlated subquery method, but I can't imagine that performance would be acceptable. I'll try to add it to my answer just in case though.
Tom H.
Ok, I just checked the Teradata docs and it looks like it may be a syntax thing, and not actual functionality. I'll try to correct my original statement for Teradata syntax.
Tom H.