Ryu
Ryu

Reputation: 75

PostgreSQL function for each row

I have a query which return to me a table like that:

id | VAL | Type
-- | --- | ----
1  | 10  |  A
2  | 20  |  B 
3  | 30  |  C
4  | 40  |  B

I want to call some function for each row, which will check type and do some stuff for each type, like IF in C#:

if(type=='A'){}
if(type=='B'){}
if(type=='C'){}

How can I make these 2 things in Postgresql using only sql?

Upvotes: 1

Views: 3002

Answers (1)

Patrick
Patrick

Reputation: 32364

In standard SQL you can use a CASE phrase here:

SELECT id, val, "type",
       CASE "type"
       WHEN 'A' THEN funcA()
       WHEN 'B' THEN funcB()
       WHEN 'C' THEN funcC()
       END AS func_result,
FROM <table>;

All functions should return a scalar value (a single value).

Upvotes: 1

Related Questions