| Author |
Topic |
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-06-16 : 05:57:38
|
| HelloI have an update statement along the lines of below..I need to read a value from the subquery (periodate) and use this to determine the field that is updated in the table (by using a case statement). I'm not sure if this is the correct way to do this. thanksUPDATE table1 set (CASE WHEN YEAR(PeriodDate) < year(getdate()) THEN lastyearbalance WHEN YEAR(Date) = year(getdate()) THEN thisyearbalance ELSE END) = sumbalanceFROM table1 LEFT OUTER JOIN (SELECT accountID, sumbalance, perioddateFROM from table2) as balsum ON table1.accountID = balsum.accountID |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-06-16 : 06:04:29
|
| What's the need of Else here?post some sample data... |
 |
|
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-06-16 : 06:12:54
|
| This is the sql I'm trying to get working, but it won't parse properly "Msg 102, Level 15, State 1, Line 1Incorrect syntax near '('.Msg 156, Level 15, State 1, Line 12Incorrect syntax near the keyword 'as'."UPDATE gl set (CASE WHEN YEAR(per.edt) < year(getdate()) THEN gl.bal1 WHEN YEAR(per.edt) = year(getdate()) THEN gl.newbal1 END) = sumbal1FROM gl LEFT OUTER JOIN (SELECT tj.glaccno, SUM(tj.bcamt) AS sumbal1, per.yr AS finyr, per.perid, per.perno, YEAR(per.edt) AS peryrFROM tj INNER JOIN per ON per.perid = tj.peridGROUP BY tj.glaccno, per.yr, per.glclosed, per.perid, per.perno, YEAR(per.edt)HAVING (per.glclosed = 0) AND (per.yr <= YEAR(GETDATE())) and (per.perno=1)) as balsum ON gl.glaccno = balsum.glaccno Thanks |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-06-16 : 06:29:21
|
| UPDATE gl set sumbal1 = (CASE WHEN YEAR(per.edt) < year(getdate()) THEN gl.bal1 WHEN YEAR(per.edt) = year(getdate()) THEN gl.newbal1 END) FROMgl LEFT OUTER JOIN (SELECT tj.glaccno, SUM(tj.bcamt) AS sumbal1, per.yr AS finyr, per.perid, per.perno, YEAR(per.edt) AS peryrFROM tj INNER JOINper ON per.perid = tj.peridGROUP BY tj.glaccno, per.yr, per.glclosed, per.perid, per.perno, YEAR(per.edt)HAVING (per.glclosed = 0) AND (per.yr <= YEAR(GETDATE())) and (per.perno=1)) as balsum ON gl.glaccno = balsum.glaccno |
 |
|
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-06-16 : 06:46:25
|
| THanks CSK but I'm trying to set the result of the case statement (field 'gl.bal1' or 'glnewbal1') equal to sumbal1 which is derived from the subqueryregards |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-06-16 : 06:56:21
|
| is ur Fiirst Update Workes..? |
 |
|
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-06-16 : 07:11:25
|
| CSKNo I do not want to update the sumbal1 field, I need to update either 'gl.bal1' or 'glnewbal1' with sumbal1 from the subquery depending on the result of the case statementthanks for ur hlp |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-16 : 08:51:03
|
you can use 2 update statement to do thisupdate glset bal1 = sumbal1from gl left outer join ( . . . ) where YEAR(per.edt) < year(getdate())update glset newbal1 = sumbal1from gl left outer join ( . . . ) where YEAR(per.edt) = year(getdate()) KH |
 |
|
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-06-16 : 08:56:49
|
| thank u khtan that does the job |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-16 : 09:00:14
|
if you really have to use one single update queryupdate glset bal1 = case when YEAR(per.edt) < year(getdate()) then sumbal1 else bal1 end, newbal1 = case when YEAR(per.edt) = year(getdate()) then sumbal1 else newbal1 endfrom gl left outer join ( . . . ) KH |
 |
|
|
|