TechGlider
04-15-2009, 10:30 PM
I have the following table that i want to update.. and get values from..
table1
http://www.umsl.edu/~cjgwb3/data.jpg
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 table1
DECLARE @e_branch integer
OPEN branchComparison
FETCH FROM branchComparison INTO @e_branch
WHILE @@FETCH_STATUS = 0
BEGIN
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_branch
END
CLOSE branchComparison
DEALLOCATE 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...
http://www.umsl.edu/~cjgwb3/data2.jpg
table1
http://www.umsl.edu/~cjgwb3/data.jpg
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 table1
DECLARE @e_branch integer
OPEN branchComparison
FETCH FROM branchComparison INTO @e_branch
WHILE @@FETCH_STATUS = 0
BEGIN
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_branch
END
CLOSE branchComparison
DEALLOCATE 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...
http://www.umsl.edu/~cjgwb3/data2.jpg