简体   繁体   中英

missing field in nested SQL statement in ms access 2010

The following MS Access 2010 query only outputs values for FirstName, HomePhone, and ClientNumber. It is not outputting any values for LastName.

Can anyone show me how to change it so that it outputs values for LastName also?

SELECT
  ActiveCustomers.FirstName
, ActiveCustomers.LastName
, tblClientAddress.HomePhone
, ActiveCustomers.ClientNumber
FROM (
  SELECT
    Clients.ClientNumber
  , Clients.FirstName
  , Clients.LastName (
      SELECT COUNT(ReferralDate) FROM IntakeTable 
      WHERE Clients.ClientNumber = IntakeTable.ClientNumber
      AND Len(ReferralDate & '') > 0
    ) AS IntakeCount
  , (
    SELECT COUNT(ExitDate)
    FROM ExitTable
    WHERE Clients.ClientNumber = ExitTable.ClientNumber
    AND Len(ExitDate & '') > 0
  ) AS ExitCount
  FROM Clients
) AS ActiveCustomers
INNER JOIN tblClientAddress
ON ActiveCustomers.ClientNumber = tblClientAddress.ClientNumber
WHERE ActiveCustomers.IntakeCount > [ExitCount]
AND tblClientAddress.CurrentResidence = True;

You appear to be missing a comma after Clients.LastName . Try:

SELECT ActiveCustomers.FirstName, ActiveCustomers.LastName, tblClientAddress.HomePhone, ActiveCustomers.ClientNumber
FROM 
       (SELECT Clients.ClientNumber, 
               Clients.FirstName, 
               Clients.LastName,
               (SELECT COUNT(ReferralDate) FROM IntakeTable WHERE Clients.ClientNumber = IntakeTable.ClientNumber AND Len(ReferralDate & '') > 0) AS IntakeCount,
               (SELECT COUNT(ExitDate) FROM ExitTable WHERE Clients.ClientNumber = ExitTable.ClientNumber AND Len(ExitDate & '') > 0) AS ExitCount 
        FROM Clients) AS ActiveCustomers
INNER JOIN tblClientAddress ON ActiveCustomers.ClientNumber = tblClientAddress.ClientNumber 
WHERE (((ActiveCustomers.IntakeCount)>[ExitCount]) AND 
       ((tblClientAddress.CurrentResidence)=True));

EDIT:

It seems likely that:

  • You have a many-to-one (or one-to-many) relationship between Clients and tblClientAddress , and you have two records on the "many" side. If you are getting duplicate records, you could add a DISTINCT or a GROUP BY ,

And/Or:

  • That the (SELECT COUNT( subqueries are somehow messing up their parent query, and need to be modified so that they can go into their parent query's FROM clause with an Inner Join , eg:

    (SELECT ClientNumber, COUNT(ReferralDate) as IntakeCount FROM IntakeTable WHERE Len(ReferralDate & '') > 0 GROUP BY ClientNumber) AS qryIntakeCount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM