I’ve done plenty of joins in SQL before but this one does not want to work – I’m getting a cartesian product and I have no idea why. I have two tables (in this context) – one is for appointments and another for customers. CustomerId is the primary key of the customer table and is the foreign key of the appointment table.
What I’m trying to achieve is to run a query that shows all of the appointments along with the customer’s NAME (stored in customer table).
This is my query:
SELECT a.AppointmentId
,a.Subject
,a.StartDateTime
,c.CustomerId
,c.FirstName
,c.lastname
FROM shared.Appointment a
INNER JOIN shared.Customer c ON a.CustomerId = c.customerid
WHERE a.BusinessCode = 'bp'
AND StartDateTime > '2013-02-11'
AND a.CustomerId > 0
GROUP BY c.customerid
,a.AppointmentId
,a.Subject
,a.CustomerId
,a.StartDateTime
,c.FirstName
,c.lastname
ORDER BY a.AppointmentId;
The result is a horrific cartesian product, which only occurs once I start adding in the first/last name into the select part.
Can anyone see what I’m doing wrong?
I have just attempted a similar but much simpler query with other tables on the database and am still getting a cartesian product. I believe there is a problem with the database, not with the query. I’m not sure anyone on stack overflow could possibly solve this issue without seeing the database.
Thanks anyway guys.