I have two tables with inner join. I have to conditionlly select only one row from right table based on the existence of a value in categorical column.
Condition : If blue exists, select blue else select green
Table-A:
| ID | Name |
| ---| ------|
| 01 | row |
| 02 | row |
| 03 | row |
Table-B:
| ID | CatCol |
| ---| --------|
| 01 | blue |
| 01 | green |
| 01 | red |
| 02 | green |
| 02 | red |
| 03 | blue |
Expected :
| ID | CatCol |
| ---| --------|
| 01 | blue |
| 02 | green |
| 03 | blue |
This should work for you:
SELECT b.ID, MIN(b.CatCol) as CatCol
FROM table_b b
INNER JOIN table_a a ON
b.id = a.id
GROUP BY b.ID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.