troubleshooting Question

Best practice for iterating rows

Avatar of singhch
singhch asked on
Microsoft SQL ServerMicrosoft SQL Server 2005
8 Comments1 Solution226 ViewsLast Modified:
Hi there,

what would be the best way to achieve the following?

TABLE A
code  x  y
a        0  1
b        0  0
a        1  1
b        1  1
c        1  0

desired result is

TABLE B
code  type
a        x
a        y
b        x
b       y
c        x

The logic is .....
row 1 in table A we create a row in table B where the code is a and type is 'x' since such combination doesnt exist in table B
row 2 in table A, we dont create a row in table B becoz both col x and y in table A is zero
row 3 in table A, we only create a row in table B for code = a and type = y.  we dont create another row for the column x becoz that already exists in table B
row 4 in table A, we create 2 rows in table B for code = b and type = x, and code = b and type = y
row 5 in table A, we create 1 row in table B for code = c and type = x

Is using cursor for the only way to achieve this? or is this a better approach?  thanks
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros