I have to generate simple report from three tables named customer, product, and order. My report should have a column that shows whether the customer ordered that product or not. How could I get the Y or N value based on customer id that exists on order with the product id? Sample table with data is as below:
customer
customer_id name address phone
123 govinda nepal 16910833
234 arjun nepal 15546235
product
product_id name brand type price
1 samsung gayalexy s5 samsung smartphone 55000
2 samsung gayalexy s6 samsung smartphone 65000
order
date product_id customer_id
1/1/2016 1 123
5/1/2016 2 123
And want the report column as below
cust_id name product_id order_stat
and order_stat should be as Y or N.
Best Answer
Seems like you need all combinations of products and customers (so a
CROSS JOIN
between the two tables) and then a correlatedEXISTS
subquery. This could either be a correlated subquery in theSELECT
list:or a
LEFT JOIN
to a dummy table with one row (thedual
does that in Oracle):Random notes:
order
. Because you'll have to quote them every time they are used.