user1101264
user1101264

Reputation: 1

Sorting data from one table to another table

I have the following tables:

  1. main_table

    • stu_name
    • stu_mark
  2. mark_table

    • s_name
    • pos_mark
    • neg_mark

With data:

main_table

stu_name  stu_mark
--------  --------
Rob        20
sally     -10
Dave       30
Merve     -50

Desired Output:

mark_table

s_name   pos_mark  neg_mark
-------  --------  --------
Rob       20       NULL
Sally     NULL     -10
Dave      30       NULL
Merve     NULL      -5

Upvotes: 0

Views: 80

Answers (1)

user359040
user359040

Reputation:

Try:

INSERT INTO mark_table
select stu_name s_name,
       case when stu_mark >= 0 then stu_mark end pos_mark,
       case when stu_mark < 0 then stu_mark end neg_mark
from main_table

(Assuming you want marks of 0 to be populated in the pos_mark but not the neg_mark column.)

Upvotes: 1

Related Questions