Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update statement using row_number() 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
OK, I seem to have come to a wall. I have this query
Code:
SELECT cast(study_site_id as varchar(10))+cast([STUDY_PID_SITE_TEXT] as varchar(10)) as sitepid
      ,[SPECIMEN_COLLECTION_SITEIDENTIFIER_TEXT] as b
      ,[PPT_SURG-HX_BIOPSY_DATE] as c
      ,row_number() over(partition by (cast(study_site_id as varchar(10))+cast([STUDY_PID_SITE_TEXT] as varchar(10))),[ppt_surg-hx_biopsy_date] order by [ppt_surg-hx_biopsy_date]) as newid
  FROM [dbEDRN353].[dbo].[tblCore]
  where SPECIMEN_COLLECTION_SITEIDENTIFIER_TEXT is null
order by sitepid

which gives me this
Code:
sitepid b       c           newid
4471498	NULL	6/99/1996	1
4471498	NULL	6/99/1996	2
4471498	NULL	6/99/1996	3
4471498	NULL	6/99/1996	4
4471498	NULL	6/99/1996	5
4471498	NULL	6/99/1996	6
4471498	NULL	6/99/1996	7
4471498	NULL	6/99/1996	8
4471498	NULL	9/99/1997	1
4471498	NULL	9/99/1997	2
4471498	NULL	9/99/1997	3
4471498	NULL	9/99/1997	4
4471498	NULL	9/99/1997	5
4471498	NULL	9/99/1997	6
4471498	NULL	9/99/1997	7

what I want to do is simply update b with newid in the underlying table (tblCore). I tried with a cte, but there was no unique record to join tblCore to the cte. Any ideas?

wb
 
Hi,

Try:

Code:
with CTE_R as
(
  SELECT cast(study_site_id as varchar(10))+cast([STUDY_PID_SITE_TEXT] as varchar(10)) as sitepid
        ,[SPECIMEN_COLLECTION_SITEIDENTIFIER_TEXT] as b
        ,[PPT_SURG-HX_BIOPSY_DATE] as c
        ,row_number() over(partition by (cast(study_site_id as varchar(10)) + 
                                         cast([STUDY_PID_SITE_TEXT] as varchar(10))),
                                        [ppt_surg-hx_biopsy_date] 
                           order by [ppt_surg-hx_biopsy_date]) as newid
  FROM [dbEDRN353].[dbo].[tblCore]
  where SPECIMEN_COLLECTION_SITEIDENTIFIER_TEXT is null
  order by sitepid
)

update CTE_R
set b = newid

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
Ohh... so I can update the CTE and that updates the underlying table. I thought it would treat the cte as a separate object and I couldn't update it. Very cool, I love to learn new stuff, thank you!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top