Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through a table one row at a time..... 2

Status
Not open for further replies.

Garreth21

Programmer
May 19, 2005
29
Hi everyone

I need to go through a table of data and look at 3 specific columns. If the value is greater then 0 for a given column, I need to make a new record in another table.

Here's an example

Table 1
ID Total Bonus A Bonus B Bonus
TEST1 125.25 500 250
TEST2 340 0 100

Table 2
ID TYPE AMOUNT
TEST1 Total Bonus 125.25
TEST2 Total Bonus 340
TEST1 A Bonus 500
TEST1 B Bonus 250
TEST2 B Bonus 100

I can't seem to figure it out. I would like to do it with out cursors if possible. Anyone have any advice on how to go about this?

Thanks in advance.
 
insert into table2
select id,'Total Bonus',[Total Bonus]
from Table1 where [Total Bonus] >0
union all
select id,'A Bonus',[A Bonus]
from Table1 where [A Bonus] >0
union all
select id,'B Bonus',[B Bonus]
from Table1 where [B Bonus] >0

Denis The SQL Menace
SQL blog:
Personal Blog:
 
I recommend you use the union all statement to accomplish this.

Code:
Select Id,
       'Total Bonus' As Type,
       [Total Bonus]
From   Table1
Where  IsNull([Total Bonus], 0) > 0

Union All

Select Id,
       'A Bonus' As Type,
       [A Bonus]
From   Table1
Where  IsNull([A Bonus], 0) > 0

Union All

Select Id,
       'B Bonus' As Type,
       [B Bonus]
From   Table1
Where  IsNull([B Bonus], 0) > 0

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
That's why you posted first! You didn't include the IsNull check. And, of course you're right IsNull isn't necessary here.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top