tags:

views:

64

answers:

2

I have this query which works perfectly:

SELECT *
FROM Customer
WHERE SacCode IN
(
    SELECT SacCode
    FROM SacCode
    WHERE ResellerCorporateID = 392
    ORDER BY SacCode
)
AND CustomerID IN
(
    SELECT CxID
    FROM CustAppointments
    WHERE AppRoomID IN
    (
        SELECT AppRoomID
        FROM ClinicRooms
        WHERE ClinID IN
        (
                SELECT ClinID
                FROM AppClinics
                WHERE ClinDate >='20090101'
                AND ClinDate <='20091119'
        )
    )
)

However, I need to see the value of ClinDate (inside the last nested query) so I've been told I need to rework the query using JOINS.

I have no idea how, can someone help please?

Thanks.

+6  A: 

Here's a start:

SELECT     *
FROM       Customer c
INNER JOIN CustAppointments ca ON ca.CxId = c.CustomerID
INNER JOIN ClinicRooms cr ON cr.AppRoomID = ca.AppRoomID
INNER JOIN AppClinics ac ON ac.ClinID = cr.ClinID
WHERE      ap.ClinDate BETWEEN '20090101' AND '20091119'
AND        SacCode IN (SELECT sc.SacCode 
                       FROM SacCode sc 
                       WHERE sc.ResellerCorporateID = 392)

This will allow you to select columns from AppClinics.

Andomar
it would return multiple records for customer where there is more that one match in the join. But that is probably what we want here if there many appointments meeting the data criteria. It is just that the original query returns only one record per customer
kristof
Wonderful solution. Nice to see (not) they went for `CustomerID` in a table and `CxId` in another one.
Alex Bagnolini
You either have to submit a grouping rule, or handle it in business level if you want it to return a single customer value.
Alex Bagnolini
+1  A: 

Read this http://www.w3schools.com/Sql/sql%5Fjoin.asp

Hugo Assanti