Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Updating the most recent records in a Table

Author  Topic 

eljapo4
Posting Yak Master

100 Posts

Posted - 2011-11-17 : 06:55:17
hi, I want to update the most recent issue no. of a Resource with the relevant PPM data which is in another table. Here's my Update statement so far:

UPDATE dbo.mp_RPINSpecifications
SET SpecpacksPerMinute = mp_RPINUpload.PPM
WHERE mp_RPINUpload.Reource = WITH CTE AS
(
SELECT --use the row_number function to get the newest record
*, ROW_NUMBER() OVER(PARTITION BY s.SpecRecordID_rc ORDER BY s.SpecIssueNo DESC) AS seq
FROM dbo.mp_RPINSpecifications s (NOLOCK)
)
SELECT s.SpecID,
s.SpecRecordID_rc,
r.RecordID,
r.Resource,
s.SpecIssueNo,
s.SpecpacksPerMinute
FROM CTE s (NOLOCK)
INNER JOIN mp_rcResourceData r (NOLOCK) ON s.SpecRecordID_rc = r.RecordID
WHERE seq = 1 --filter for the newest record only
ORDER BY r.Resource


Is this the best way to do this particular update? If so I'm getting this:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'WITH'.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 11:14:34
[code]UPDATE t
SET t.SpecpacksPerMinute = r.PPM
FROM (
SELECT --use the row_number function to get the newest record
SpecpacksPerMinute,SpecRecordID_rc, ROW_NUMBER() OVER(PARTITION BY s.SpecRecordID_rc ORDER BY s.SpecIssueNo DESC) AS seq
FROM dbo.mp_RPINSpecifications s (NOLOCK)
)t
INNER JOIN mp_rcResourceData r (NOLOCK) ON t.SpecRecordID_rc = r.RecordID
AND t.seq = 1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

eljapo4
Posting Yak Master

100 Posts

Posted - 2011-11-17 : 11:40:20
Thanks for the reply Visakh16, with the help of the company DBA I ended up with this:

CREATE TABLE #CTE
(
[SpecRecordID_rc] BIGINT NULL ,
[SpecIssueNo] INT NULL ,
[Resource] [varchar](35) NULL ,
[PPM] FLOAT NULL
)

WITH CTE
AS ( SELECT
* ,
ROW_NUMBER() OVER ( PARTITION BY s.SpecRecordID_rc ORDER BY s.SpecIssueNo DESC ) AS seq
FROM dbo.mp_RPINSpecifications s ( NOLOCK )
)
INSERT INTO #CTE
SELECT CTE.SpecRecordID_rc ,
CTE.SpecIssueNo ,
r.Resource ,
u.PPM
FROM CTE (NOLOCK)
INNER JOIN mp_rcResourceData r ( NOLOCK ) ON CTE.SpecRecordID_rc = r.RecordID
INNER JOIN mp_RPINUpload u ( NOLOCK ) ON r.Resource = u.Resource
WHERE seq = 1
ORDER BY CTE.SpecRecordID_rc


UPDATE dbo.mp_RPINSpecifications
SET SpecpacksPerMinute = #CTE.PPM
FROM dbo.mp_RPINSpecifications spec
INNER JOIN #CTE ON spec.SpecRecordID_rc = #CTE.SpecRecordID_rc
AND spec.SpecIssueNo = #CTE.SpecIssueNo
WHERE spec.SpecRecordID_rc IN ( SELECT [SpecRecordID_rc]
FROM #CTE )
AND EXISTS ( SELECT 1
FROM mp_RPINUpload
INNER JOIN #CTE ON dbo.mp_RPINUpload.Resource = #CTE.Resource
AND #CTE.SpecRecordID_rc = spec.SpecRecordID_rc
AND #CTE.SpecIssueNo = spec.SpecIssueNo )

DROP TABLE #CTE

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 12:02:46
the below condition check is redundant

spec.SpecRecordID_rc IN ( SELECT [SpecRecordID_rc]
FROM #CTE )

as its already included inside join itself

ie INNER JOIN #CTE ON spec.SpecRecordID_rc = #CTE.SpecRecordID_rc
AND spec.SpecIssueNo = #CTE.SpecIssueNo



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -