Podsixia
2013-03-14 11:03:45 UTC
This works:
SELECT cust_name, cust_state,
(SELECT COUNT(*) FROM orders
WHERE orders.cust_id = Customers.cust_id) AS innerquery
FROM customers
ORDER BY cust_name;
But this doesn't:
SELECT COUNT(*) FROM orders
WHERE orders.cust_id = Customers.cust_id;
It gives the error: "The multi-part identifier "Customers.cust_id" could not be bound."
I get that this is basically using a JOIN where no JOIN is needed, but if that's the case, why does it work in the subquery?
Thanks for your help. I'm using MS SQL Server Express 2008R2. The tables are from the Sams DB from the Sam's 10 minute SQL book.