Here below some sample 3 data, using PostgreSQL and rails
Stock
id: 42324
name: 'n1'
stock_items
id: 57889359
stock_id: 42324
check_id: 14123
turn: 5
mock_id: 57889357
id: 57889360
stock_id: 42324
check_id: 14141
turn: 3
mock_id: 0
Stock
id: 42325
name: 'n1'
stock_items
id: 57889361
stock_id: 42325
check_id: 19499
turn: 5
mock_id: 57889359
id: 57889362
stock_id: 42325
check_id: 19500
turn: 3
mock_id: 0
Here i have stock table and stock_items table i am trying to take the result like if mock_id is 0 then get the check_id else other check_id whose mock_id is not zero.
So i tried one query
SELECT
check_id1,
CASE
WHEN stock_items.mock_id = 0 THEN stock_items.check_id
ELSE stock_items.check_id
END as check_id2
FROM
stock_items
INNER JOIN stocks on stocks.id = stock_items.stock_id ;
but the above query fails, but i need it like below, any suggestions?
check_id1 (mock_id=0) | check_id2 (mock_id !=0)
----------------------+-----------------------
14141 | 14123
19500 | 19499