1

I need to compare column user_id with another column order_id in the same table Orders.

If a user_id has multiple distinct order_id's, then I need to default order_id to '00000'. If user_id has two same order_id's, then that user_id should have only one entry.

Input table: Orders

user_id order_id
A234 87655
A234 89765
A234 98723
R678 09874
R678 09874

Expected output:

user_id order_id
A234 00000
R678 09874
2
  • Use GROUP BY user_id and HAVING COUNT(*) > 1 Commented Oct 5, 2021 at 18:48
  • Tag only the database that you use. Commented Oct 5, 2021 at 18:50

1 Answer 1

2

Simple aggregation would work with case expression :

select user_id, 
       case when min(order_id) <> max(order_id) then 00000 else min(order_id) end as order_id
from orders 
group by user_id;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.