aneilg
asked on
SQL Update
i am using a slowly changing dimension to update my table. but i need to incluse a few checks.
so after the table has been updated i am using another sp to do the checks.
what i need to do is, if the value in th ecode is NULL then use the previous code.
rowid datecreated validfrom validto iscurrent businesskey code recdel
1 18/09 18/09 17/09 0 C3 CW NO
2 18/09 18/09 NULL 1 C3 NULL NO
Thanks.
so after the table has been updated i am using another sp to do the checks.
what i need to do is, if the value in th ecode is NULL then use the previous code.
rowid datecreated validfrom validto iscurrent businesskey code recdel
1 18/09 18/09 17/09 0 C3 CW NO
2 18/09 18/09 NULL 1 C3 NULL NO
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
all its done is set all the values in the code column to null.
i want to use previous value if null
i want to use previous value if null
ASKER
i've been looking at CTE and coalesce
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
still not what i am looking for.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry to be a pain.
Incorrect syntax near the keyword 'from'.
Incorrect syntax near the keyword 'from'.
ASKER
this works for reference.
UPDATE TABLE
SET Code = c2.Code
OUTPUT Inserted.*
FROM TABLEc
INNER JOIN (
SELECT c.[DateCreated]
, c.[BusinessKey]
, Code = COALESCE(c.Code, cprevious.Code)
FROM TABLEc
INNER JOIN (
SELECT c.[DateCreated]
, c.[BusinessKey]
, MaxDate = MAX(cdates.[DateCreated])
FROM TABLEc
LEFT OUTER JOIN (
SELECT [DateCreated]
, [BusinessKey]
FROM TABLE
) cdates ON cdates.[DateCreated] < c.[DateCreated] AND cdates.[BusinessKey] = c.[BusinessKey]
GROUP BY
c.[DateCreated], c.[BusinessKey]
) cmax ON cmax.[DateCreated] = c.[DateCreated] AND cmax.[BusinessKey] = c.[BusinessKey]
LEFT OUTER JOIN TABLE cprevious ON cprevious.[DateCreated] = cmax.MaxDate AND cprevious.[BusinessKey] = cmax.[BusinessKey]
) c2 ON c2.[DateCreated] = c.[DateCreated]
AND c2.[BusinessKey] = c.[BusinessKey]
UPDATE TABLE
SET Code = c2.Code
OUTPUT Inserted.*
FROM TABLEc
INNER JOIN (
SELECT c.[DateCreated]
, c.[BusinessKey]
, Code = COALESCE(c.Code, cprevious.Code)
FROM TABLEc
INNER JOIN (
SELECT c.[DateCreated]
, c.[BusinessKey]
, MaxDate = MAX(cdates.[DateCreated])
FROM TABLEc
LEFT OUTER JOIN (
SELECT [DateCreated]
, [BusinessKey]
FROM TABLE
) cdates ON cdates.[DateCreated] < c.[DateCreated] AND cdates.[BusinessKey] = c.[BusinessKey]
GROUP BY
c.[DateCreated], c.[BusinessKey]
) cmax ON cmax.[DateCreated] = c.[DateCreated] AND cmax.[BusinessKey] = c.[BusinessKey]
LEFT OUTER JOIN TABLE cprevious ON cprevious.[DateCreated] = cmax.MaxDate AND cprevious.[BusinessKey] = cmax.[BusinessKey]
) c2 ON c2.[DateCreated] = c.[DateCreated]
AND c2.[BusinessKey] = c.[BusinessKey]
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
partly answered.
ASKER
update [WARD_SCD2_HISTORY]
set
Open in new window
the code chould be any value, so i cannot use the same value