Link to home
Start Free TrialLog in
Avatar of cozza13
cozza13

asked on

SQL HAVE statement using text

Given the following tables & Columns:

 

Customer

customer_id (PK)

customer_name

 

Order

Order_id (PK)

Customer_id (FK)

Order_Date

 

Order_Line

Order_id (PK) (FK)

Line_no (PK)

Item_id (FK)

Qty

Extended_price

 

Item

Item_id

Item_name

Item_description

Price

 I am trying to write the SQL statement that retrieves the list of customers (name) who did not have any orders for product name "X" on a given date? I am having some problems

Avatar of AdrianSRU
AdrianSRU

Try this:

SELECT Customer.customer_name FROM ((Customer LEFT JOIN Order ON (Customer.customer_id=Order.customer_id AND Order.order_date='yyyy-mm-dd')) LEFT JOIN Order_Line ON Order.order_id=Order_Line.order_id) LEFT JOIN Item ON (Order_Line.item_id=Item.item_id AND Item.item_name='product name') WHERE Item.item_id IS NULL ORDER BY Customer.customer_name;

By using a LEFT JOIN, all of the customers will be included in the result set before the WHERE condition is considered.  If the customer has an order for the product on the date specified, then Item.item_id will have a value and "WHERE Item.item_id IS NULL" will remove that customer from the final results.


--Adrian
Avatar of cozza13

ASKER

Hi that bought up an error when trying to left join the Order_line table to the Order table
What was the error message?


--Adrian
Avatar of cozza13

ASKER

Unexpected token order requires SELECT in Statement [Select.....

it does this when I do the LEFT JOIN Order_Line ON Order.order_id=Order_Line.order_id)
What version of MySQL are you using?


--Adrian
Avatar of cozza13

ASKER

well I was trying to do it in SQL on openoffice database. The I tried it in Cryatl Reports 11 and I get the error message. "join expression not supported" using the ODBC text driver.
Avatar of cozza13

ASKER

I have a feeling that it can't be done with SQL on its own.
ASKER CERTIFIED SOLUTION
Avatar of AdrianSRU
AdrianSRU

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cozza13

ASKER

ok thanks for your help