| Author |
Topic |
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-15 : 15:21:38
|
OK Updated version...I have the following table that i want to update.. and get values from..table1 What i need to do is loop through this table.. one row at a time getting the value from column 'branch', and using that value, put it in the select query that will give me the results i need. Then put that result needed into column 'branch_count' in its respective row that the original value was gotten from. And move on to the next column 'branch's, row which would be row number 2 and do this until there are no more rows..This is what i currently have but it is not working correctly..DECLARE branchComparison CURSOR FOR SELECT branch FROM table1DECLARE @e_branch integerOPEN branchComparisonFETCH FROM branchComparison INTO @e_branchWHILE @@FETCH_STATUS = 0BEGIN UPDATE table1 SET branch_count = (SELECT count(distinct calculatedValue) FROM tablea a, tableb b, JOIN tablec c ON b.valueX = c.valueY WHERE b.valueZ > '01/01/2000' AND a.branch = @e_branch AND b.valueC = 1 AND c.valueD IS NOT NULL)FETCH NEXT FROM branchComparison INTO @e_branchENDCLOSE branchComparisonDEALLOCATE branchComparison If I run the select query by itself and get the desired result buy manually replacing the@e_branch value with the respective column and row value.. The result of SELECT count(distinct calculatedValue) FROM tablea a, tableb b, JOIN tablec c ON b.valueX = c.valueY WHERE b.valueZ > '01/01/2000' AND a.branch = @e_branch <--- Which would now be "AND a.branch = 1" AND b.valueC = 1 AND c.valueD IS NOT NULL Running that select query gives me this result... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 15:31:25
|
UPDATE MyTableSET Col3 = COALESCE(Col3, Col1),Col4 = COALESCE(Col4, Col2) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-15 : 15:58:38
|
| How will that get the new value from the select query inside the loop? Which i called newValue?Definitely need some response rather than just code.. I want to learn. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 16:09:03
|
Then you need to supply some proper sample data and also post your expected output. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-15 : 16:26:25
|
| Updated original post ^ Scroll UP ^ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 16:33:20
|
Can you also post an image of your expected output?How will the table look when you are done? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 16:38:49
|
[code]UPDATE t1 SET t1.branch_count = e.xFROM Table1 AS t1INNER JOIN ( SELECT a.Branch, COUNT(DISTINCT calculatedValue) AS x FROM TableA AS a INNER JOIN TableB AS b ON b.ValueZ >= '01/01/2000' AND b.ValueC = 1 INNER JOIN TableC AS c ON c.ValueY = b.ValueX WHERE c.valueD IS NOT NULL GROUP BY a.Branch ) AS e ON e.Branch = t1.Branch[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-15 : 16:47:29
|
| Original post updated |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 16:51:01
|
See post made 04/15/2009 : 16:38:49 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-15 : 17:23:57
|
quote: Originally posted by Peso
UPDATE t1 SET t1.branch_count = e.xFROM Table1 AS t1INNER JOIN ( SELECT a.Branch, COUNT(DISTINCT calculatedValue) AS x FROM TableA AS a INNER JOIN TableB AS b ON b.ValueZ >= '01/01/2000' AND b.ValueC = 1 INNER JOIN TableC AS c ON c.ValueY = b.ValueX WHERE c.valueD IS NOT NULL GROUP BY a.Branch ) AS e ON e.Branch = t1.Branch E 12°55'05.63"N 56°04'39.26"
Im confused on this line "SET t1.branch_count = e.x"where did the e.x come from? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 17:29:31
|
It comes from the derived table e.The x column is the distinct count of calculatedValue for any given Branch. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-16 : 08:46:26
|
This is exactly how i want it to work..DECLARE branchComparisonCURSOR FOR SELECT branchFROM table1DECLARE @e_branch integerDECLARE @branch_count INTEGEROPEN branchComparisonFETCH FROM branchComparison INTO @e_branchWHILE @@FETCH_STATUS = 0BEGINSELECT @branch_count = count(distinct calculatedValue)FROM tablea a,tableb b,JOIN tablec cON b.valueX = c.valueYWHERE b.valueZ > '01/01/2000'AND a.branch = @e_branchAND b.valueC = 1AND c.valueD IS NOT NULLUPDATE table1 SET branch_count = @branch_count WHERE branch = @e_branchFETCH NEXT FROM branchComparison INTO @e_branchENDCLOSE branchComparisonDEALLOCATE branchComparison So now i am trying to make it so that query doesn't take 20+ minutes and perhaps do column4 at the same time, where column4 gets its calcuated value from column2.. soo something like this?DECLARE branchComparisonCURSOR FOR SELECT branch, codeFROM table1DECLARE @e_branch INTEGERDECLARE @branch_count INTEGERDECLARE @e_code VARCHAR(100)DECLARE @code_count INTEGEROPEN branchComparisonFETCH FROM branchComparison INTO @e_branch,@e_codeWHILE @@FETCH_STATUS = 0BEGINSELECT @branch_count = count(distinct calculatedValue)FROM tablea a,tableb b,JOIN tablec cON b.valueX = c.valueYWHERE b.valueZ > '01/01/2000'AND a.branch = @e_branchAND b.valueC = 1AND c.valueD IS NOT NULLUPDATE table1 SET branch_count = @branch_count WHERE branch = @e_branchSELECT @code_count = count(distinct calculatedValue)FROM tablea a,tableb b,JOIN tablec cON b.valueX = c.valueYWHERE b.valueZ > '01/01/2000'AND b.Branch_ID = @e_codeAND b.valueC = 1AND c.valueD IS NOT NULLUPDATE table1 SET code_count = @code_count WHERE code= @e_codeFETCH NEXT FROM branchComparison INTO @e_branch,@e_codeENDCLOSE branchComparisonDEALLOCATE branchComparison Or is there a simpler way to do this? Cause this takes forever! |
 |
|
|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2009-04-16 : 09:36:38
|
| Why does this seem suspiciously like the "%fox%" dude who didn't want a set based soultion because cursors were "better" - in his opinion?????Did you test Peso's solution? If so, did it provide you with your expected results?Terry-- Procrastinate now! |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-16 : 10:04:23
|
| tosscrosby.. I am pretty new to Advanced SQL and I am seeing this query is taking while. As i stated in the first post, if you can show me a easier or more clearer way to get to the results i am looking for.. Take me to school.. Im here to learn and not argue.. So if you can duplicate what i need.. please show me a way.. The last post i submitted has the code to get the EXACT results i need but only does it one column at a time and secondly takes FOREVER..p.s. I dunno who this %fox% fella is but i assure you i have opinions and they aren't code related, much more political based, and when it comes to programming, i have no opinions only questions.. Thanks guys |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-16 : 11:28:49
|
| what were results when you used peso's suggestion on 04/15/2009 : 16:38:49? |
 |
|
|
techglider
Starting Member
26 Posts |
Posted - 2009-04-16 : 12:30:38
|
| The query kept going for over an hour so i stopped it but, it finished that column with i believe the correct values |
 |
|
|
|