Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

UPDATE common table expression Expand / Collapse
Author
Message
Posted Wednesday, January 09, 2013 9:01 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 05, 2013 4:59 AM
Points: 5, Visits: 14
Hi all
Can anyone advise please, I have a TSQL 2008r2 headache...
I'm calculating the geo distance between a previous geo point and current geo point.
The code below, as a SELECT statement, returns 1.1 M rows and seems to be good.
When run as an UPDATE it sets all 1.8 M rows and with a zero value
WITH tblCTE AS -- create common table expression
(SELECT
Row_Number() OVER (ORDER BY varVehicleReg, dteEvent) AS RowNumber
,dteEvent, varVehicleReg, geoPoint, fltDistanceMoved
FROM stageCoAvlsLog WHERE intFix > 0 -- all geoPoint are populated.
)
SELECT Cur.dteEvent, Cur.varVehicleReg, Cur.fltDistanceMoved,
-- or ...
--UPDATE stageCoAvlsLog SET fltDistanceMoved =
Cur.geoPoint.STDistance(Prv.geoPoint)-- AS fltMetres
FROM tblCTE AS Cur
LEFT OUTER JOIN tblCTE AS Prv
ON Cur.RowNumber = Prv.RowNumber + 1 -- join on current and previous row
AND Cur.varVehicleReg=Prv.varVehicleReg

-- the table defs are
[dteEvent] [datetime] NOT NULL,
[varVehicleReg] [varchar](32) NOT NULL,
[geoPoint] [geography] NULL,
[fltDistanceMoved] [real] NULL,

The http://msdn.microsoft.com/en-us/library/ms177523(v=sql.105).aspx says "Only the rows returned by the common table expression are modified." Hmm... 1 issue or 2?


Dave
Post #1404851
Posted Wednesday, January 09, 2013 9:10 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 8:08 AM
Points: 8,553, Visits: 8,207
dave harris 45446 (1/9/2013)
Hi all
Can anyone advise please, I have a TSQL 2008r2 headache...
I'm calculating the geo distance between a previous geo point and current geo point.
The code below, as a SELECT statement, returns 1.1 M rows and seems to be good.
When run as an UPDATE it sets all 1.8 M rows and with a zero value
WITH tblCTE AS -- create common table expression
(SELECT
Row_Number() OVER (ORDER BY varVehicleReg, dteEvent) AS RowNumber
,dteEvent, varVehicleReg, geoPoint, fltDistanceMoved
FROM stageCoAvlsLog WHERE intFix > 0 -- all geoPoint are populated.
)
SELECT Cur.dteEvent, Cur.varVehicleReg, Cur.fltDistanceMoved,
-- or ...
--UPDATE stageCoAvlsLog SET fltDistanceMoved =
Cur.geoPoint.STDistance(Prv.geoPoint)-- AS fltMetres
FROM tblCTE AS Cur
LEFT OUTER JOIN tblCTE AS Prv
ON Cur.RowNumber = Prv.RowNumber + 1 -- join on current and previous row
AND Cur.varVehicleReg=Prv.varVehicleReg

-- the table defs are
[dteEvent] [datetime] NOT NULL,
[varVehicleReg] [varchar](32) NOT NULL,
[geoPoint] [geography] NULL,
[fltDistanceMoved] [real] NULL,

The http://msdn.microsoft.com/en-us/library/ms177523(v=sql.105).aspx says "Only the rows returned by the common table expression are modified." Hmm... 1 issue or 2?


Dave


--edit--

Misread the issue...


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1404855
Posted Wednesday, January 09, 2013 9:17 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, May 13, 2013 6:43 AM
Points: 304, Visits: 310
dave harris 45446 (1/9/2013)
Hi all
Can anyone advise please, I have a TSQL 2008r2 headache...
I'm calculating the geo distance between a previous geo point and current geo point.
The code below, as a SELECT statement, returns 1.1 M rows and seems to be good.
When run as an UPDATE it sets all 1.8 M rows and with a zero value
WITH tblCTE AS -- create common table expression
(SELECT
Row_Number() OVER (ORDER BY varVehicleReg, dteEvent) AS RowNumber
,dteEvent, varVehicleReg, geoPoint, fltDistanceMoved
FROM stageCoAvlsLog WHERE intFix > 0 -- all geoPoint are populated.
)
SELECT Cur.dteEvent, Cur.varVehicleReg, Cur.fltDistanceMoved,
-- or ...
--UPDATE stageCoAvlsLog SET fltDistanceMoved =
Cur.geoPoint.STDistance(Prv.geoPoint)-- AS fltMetres
FROM tblCTE AS Cur
LEFT OUTER JOIN tblCTE AS Prv
ON Cur.RowNumber = Prv.RowNumber + 1 -- join on current and previous row
AND Cur.varVehicleReg=Prv.varVehicleReg

-- the table defs are
[dteEvent] [datetime] NOT NULL,
[varVehicleReg] [varchar](32) NOT NULL,
[geoPoint] [geography] NULL,
[fltDistanceMoved] [real] NULL,

The http://msdn.microsoft.com/en-us/library/ms177523(v=sql.105).aspx says "Only the rows returned by the common table expression are modified." Hmm... 1 issue or 2?


Dave


As far as that last comment goes, the code you're referring to does an INNER JOIN on the CTE. You're doing a LEFT JOIN, which means that not only the rows that match will get updated, but also the rows that DON'T match. Change LEFT JOIN to INNER JOIN if you want only the rows specified in the CTE to be updated.


Roland Alexander
The Developing World


There are two means of refuge from the miseries of life: music and cats. ~ Albert Schweitzer
Post #1404857
Posted Wednesday, January 09, 2013 11:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235, Visits: 5,389
The problem is that the table you are updating is not referenced (directly) in your FROM clause, so it is updating every row in that table. You want to change the updated table to one of the CTE aliases.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Post #1404925
Posted Thursday, January 10, 2013 2:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 05, 2013 4:59 AM
Points: 5, Visits: 14
The UPDATE of the current CTE works. So simple, yet hard to see.
Many thanks.

Dave
Post #1405291
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse