I have this table whereby the data of the same customer id is in a new row:
x
order_id | cust_id | order_date | menu_item
------------------------------------------------------------
order1 | cust1 | January 19, 2020, 1:00 PM |
| cust1 | | food_A
Is it possible to get a one row of the data like below:
order_id | cust_id | order_date | menu_item
------------------------------------------------------------
order1 | cust1 | January 19, 2020, 1:00 PM | food_A
I tried joining but then i get the same as the table where it prints out two data instead of one. Might be my join isn’t correct. Appreciate the help. thanks
select s.cust_id, s.menu_item from tableA s inner join tableA t on s.cust_id = t.cust_id
Advertisement
Answer
In your case, assuming the missing values be NULL
, you may just aggregate by order_id
and cust_id
and then take the max of the other two columns:
SELECT
order_id,
cust_id,
MAX(order_date) AS order_date,
MAX(menu_item) AS menu_item
FROM tableA
GROUP BY
order_id,
cust_id;