1

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)...

How do I do it?

Thanks.

1
  • 2
    Is this not possible with JOINS? You could get all the data you needed then. Commented Nov 19, 2009 at 12:21

4 Answers 4

1

I'd rewrite the query using joins. Then, you can access any data from any of the joined tables.

For example, you could rewrite your query like this:

SELECT c.*, ac.ClinDate
FROM Customer c
  JOIN SacCode sc ON sc.SacCode = c.SacCode
  JOIN CustAppointments ca ON ca.CustomerID = c.CustomerID
  JOIN ClinicRooms cr ON cr.AppRoomID = ca.AppRoomID
  JOIN AppClinic ac ON ac.ClinID = cr.ClinID
WHERE ac.ClinDate >='20090101'
  AND ac.ClinDate <='20091119'
  AND sc.ResellerCorporateID = 392
Sign up to request clarification or add additional context in comments.

Comments

0

Think I'd use derived table in the FROM statement rather than 3 deep nested query, will allow you to access values and will look a LOT better.

Comments

0

You'll need to copy the subselects to the FROM clause or rewrite the query using JOINs.

Comments

0

it should look something like this:

SELECT c.*, a.ClinDate

FROM Customer c

inner join CustAppointments ca
inner join ClinicRooms cr
inner join AppClinics a

where c.SacCode IN
(
    SELECT SacCode
    FROM SacCode
    WHERE ResellerCorporateID = 392
    ORDER BY SacCode
)

and c.CustomerID = ca.CxID
and ca.AppRoomID = cr.AppRoomID
and cr.ClinID = a.ClinID

and a.ClinDate >='20090101' 
and a.ClinDate <='20091119'

3 Comments

Thanks Dave, but I can get it to work... Where does s.SacCode come from?
that should be c.SacCode as you are selecting from the inline query i.e. there is not join to SacCode
...although you could do (see answer from Jim)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.