sql - Right syntax to do an INSERT in PostgreSQL with multiple IF conditions -


i have quite large table of sporting events , cleaning trying arrange in smaller , cleaner tables. getting there, found obstacle. original table structure:

date  |  team_name  |  team_id  |  opposition  |  opposition_id  |  venue ------------------------------------------------------------------------- 

and here want it. part of write pseudocode, because not sure how can express in postgresql:

insert new_table(date, home, away) select distinct date, (...) 

for next 2 fields after date, want insert in column home of new table values of original column team_id if value in original column venue "home". if value of venue not "home", in new column home of new table insert values of original opposition_id.

can done easily? found question dealing multiple ifs in mysql here, not sure how adapt method case.

you can use case:

insert  newtable         (date, home) select  date ,       case venue          when 'home' team_id         else opposition_id         end    yourtable 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -