|
|
Question : oracle sql greatest/least question.
|
|
Hi.. I'm trying to return in a single row 2 values which are in 2 different rows but the same column
SELECT c.CUSTOMER_NO, GREATEST (hw.hardware_no) AS item1, LEAST (hw.hardware_no) AS item2 FROM jan.customer c, jan.hardware hw WHERE hw.customer_no = c.customer_no And c.customer_no = '1234567'
Table view CUSTOMER NO hardware_no serial_no 1234567 899999999999999 NULL 1234567 3333333333333 12344 1234567 877777777777777 NULL 1234567 2222222222222 12345
returns CUSTOMER NO ITEM1 ITEM2 1234567 899999999999999 899999999999999 1234567 333333333333333 333333333333333 1234567 877777777777777 877777777777777 1234567 222222222222222 222222222222222
Where what I would want is... CUSTOMER NO ITEM1 ITEM2 1234567 899999999999999 333333333333333 1234567 877777777777777 222222222222222
item1 is always larger that item2 serial_no which will always be null for item 1, and is always present for item 2, though I doubt that this is necessary to solve this problem.
Thanks
|
Answer : oracle sql greatest/least question.
|
|
don't use greatest and least, use max and min instead:
try this:
SELECT c.CUSTOMER_NO, max (hw.hardware_no) AS item1, min(hw.hardware_no) AS item2 FROM jan.customer c, jan.hardware hw WHERE hw.customer_no = c.customer_no And c.customer_no = '1234567' group by c.customer_no;
|
|
|
|
|