October 5, 2011 at 4:56 pm
Hi,
Is it possible to do this:
update stg_PAreporting
set [jobname] = [jobname] + '-' + cast(row_number() over (partition by [jobname] order by [date]) as varchar(3))
I want to append -1, -2, -3 etc to the end of the job name depending on how many times it shows up in the table ordered by oldest date to newest.
It works fine in a select:
select [jobname],[jobname] + '-' + cast(row_number() over (partition by [jobname] order by [date]) as varchar(3))
from stg_PAreporting
but with an update, it fails. "Windowed functions can only appear in the SELECT or ORDER BY clauses."
Is there a solution for this?
Thanks much,
Howard
October 6, 2011 at 4:36 am
October 6, 2011 at 6:08 am
Assuming ID is the PK of stg_PAreporting:
WITH JobOrder
AS
(
SELECT ID
jobname
,jobname + '-' + CAST(ROW_NUMBER() OVER (PARTITION BY jobname ORDER BY [date]) AS varchar(3)) AS NewJobName
FROM stg_PAreporting
)
UPDATE JobOrder
SET jobname = NewJobName
October 7, 2011 at 1:57 pm
Thanks for the responses.
I am always really happy to learn something new. Using CTE, there is a lot that can be accomplished.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply