At times we might have to select values from multiple columns conditionally.
We can use CASE
 and WHEN
 for that.
Let us implement this conditional logic to come up with derived order_status.
If order_status is COMPLETE or CLOSED, set COMPLETED
If order_status have PENDING in it, then we will say PENDING
If order_status have PROCESSING or PAYMENT_REVIEW in it, then we will say PENDING
We will set all others as OTHER
We can also have ELSE
 as part of CASE
 and WHEN
.
order_id | order_date | order_customer_id | order_status | updated_order_status |
---|---|---|---|---|
1021 | 2013-07-30 00:00:00 | 10118 | COMPLETE | COMPLETED |
4068 | 2013-08-17 00:00:00 | 12293 | PENDING | None |
5881 | 2013-08-30 00:00:00 | 3715 | CLOSED | COMPLETED |
7564 | 2013-09-09 00:00:00 | 8648 | CLOSED | COMPLETED |
8766 | 2013-09-18 00:00:00 | 855 | COMPLETE | COMPLETED |
8926 | 2013-09-19 00:00:00 | 10517 | ON_HOLD | None |
9290 | 2013-09-21 00:00:00 | 11879 | COMPLETE | COMPLETED |
9793 | 2013-09-24 00:00:00 | 9809 | COMPLETE | COMPLETED |
9816 | 2013-09-24 00:00:00 | 1753 | COMPLETE | COMPLETED |
14047 | 2013-10-20 00:00:00 | 6473 | CLOSED | COMPLETED |
order_id | order_date | order_customer_id | order_status | updated_order_status |
---|---|---|---|---|
1021 | 2013-07-30 00:00:00 | 10118 | COMPLETE | COMPLETED |
4068 | 2013-08-17 00:00:00 | 12293 | PENDING | PENDING |
5881 | 2013-08-30 00:00:00 | 3715 | CLOSED | COMPLETED |
7564 | 2013-09-09 00:00:00 | 8648 | CLOSED | COMPLETED |
8766 | 2013-09-18 00:00:00 | 855 | COMPLETE | COMPLETED |
8926 | 2013-09-19 00:00:00 | 10517 | ON_HOLD | ON_HOLD |
9290 | 2013-09-21 00:00:00 | 11879 | COMPLETE | COMPLETED |
9793 | 2013-09-24 00:00:00 | 9809 | COMPLETE | COMPLETED |
9816 | 2013-09-24 00:00:00 | 1753 | COMPLETE | COMPLETED |
14047 | 2013-10-20 00:00:00 | 6473 | CLOSED | COMPLETED |
order_id | order_date | order_customer_id | order_status | updated_order_status |
---|---|---|---|---|
1021 | 2013-07-30 00:00:00 | 10118 | COMPLETE | COMPLETED |
4068 | 2013-08-17 00:00:00 | 12293 | PENDING | PENDING |
5881 | 2013-08-30 00:00:00 | 3715 | CLOSED | COMPLETED |
7564 | 2013-09-09 00:00:00 | 8648 | CLOSED | COMPLETED |
8766 | 2013-09-18 00:00:00 | 855 | COMPLETE | COMPLETED |
8926 | 2013-09-19 00:00:00 | 10517 | ON_HOLD | OTHER |
9290 | 2013-09-21 00:00:00 | 11879 | COMPLETE | COMPLETED |
9793 | 2013-09-24 00:00:00 | 9809 | COMPLETE | COMPLETED |
9816 | 2013-09-24 00:00:00 | 1753 | COMPLETE | COMPLETED |
14047 | 2013-10-20 00:00:00 | 6473 | CLOSED | COMPLETED |
order_id | order_date | order_customer_id | order_status | updated_order_status |
---|---|---|---|---|
1021 | 2013-07-30 00:00:00 | 10118 | COMPLETE | COMPLETED |
4068 | 2013-08-17 00:00:00 | 12293 | PENDING | PENDING |
5881 | 2013-08-30 00:00:00 | 3715 | CLOSED | COMPLETED |
7564 | 2013-09-09 00:00:00 | 8648 | CLOSED | COMPLETED |
8766 | 2013-09-18 00:00:00 | 855 | COMPLETE | COMPLETED |
8926 | 2013-09-19 00:00:00 | 10517 | ON_HOLD | OTHER |
9290 | 2013-09-21 00:00:00 | 11879 | COMPLETE | COMPLETED |
9793 | 2013-09-24 00:00:00 | 9809 | COMPLETE | COMPLETED |
9816 | 2013-09-24 00:00:00 | 1753 | COMPLETE | COMPLETED |
14047 | 2013-10-20 00:00:00 | 6473 | CLOSED | COMPLETED |