selecting fields from a subquery to be included in the main query (1 Viewer)

Salwa

New member
Local time
Today, 22:52
Joined
Oct 10, 2023
Messages
12
Hi

How do I get the subquery T1.CODE value to be displayed as part of the main query?

Thanks heaps

SELECT CopyTIMEENTRY.TCT, tblPayrollOfficers.TCTNAME, CopyTIMEENTRY.EMP_ID, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE, CopyTIMEENTRY.CODE
FROM tblPayrollOfficers INNER JOIN CopyTIMEENTRY ON tblPayrollOfficers.PO = CopyTIMEENTRY.TCT
WHERE (((CopyTIMEENTRY.CODE)="a654" or CopyTIMEENTRY.CODE ="a655") AND ((Exists (SELECT T1.CODE AS CODE1 FROM CopyTIMEENTRY as T1 WHERE CopyTIMEENTRY.EMP_ID = T1.EMP_ID AND CopyTIMEENTRY.TRANSDATE = T1.TRANSDATE AND (T1.CODE = "A630" or T1.CODE = "A636" )))=True))
ORDER BY CopyTIMEENTRY.TCT, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE;
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
Isn't it already included as CopyTIMEENTRY.CODE?
 

Salwa

New member
Local time
Today, 22:52
Joined
Oct 10, 2023
Messages
12
no, it is a different value to that of the one in the main query. the Code value in the main query is either A654 or A655 and the code value in the subquery is either A630 or A636.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
I see. What happens if you add the subquery as calculated column in the main query? Just thinking out loud...
 

Salwa

New member
Local time
Today, 22:52
Joined
Oct 10, 2023
Messages
12
I am BA not a coder and I have limited experience with MS Access. So unsure how to do what you are saying.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
I am BA not a coder and I have limited experience with MS Access. So unsure how to do what you are saying.
How many records do you get when running that query?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
Just as a test, what do you get with this query?
SQL:
SELECT CopyTIMEENTRY.TCT, tblPayrollOfficers.TCTNAME, CopyTIMEENTRY.EMP_ID, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE, CopyTIMEENTRY.CODE, (SELECT A.CODE FROM CopyTIMEENTRY AS A WHERE A.EMP_ID=CopyTIMEENTRY.EMP_ID AND A.TRANSDATE=CopyTIMEENTRY.TRANSDATE) AS CODE1
FROM tblPayrollOfficers INNER JOIN CopyTIMEENTRY ON tblPayrollOfficers.PO = CopyTIMEENTRY.TCT
WHERE (((CopyTIMEENTRY.CODE)="a654" or CopyTIMEENTRY.CODE ="a655") AND ((Exists (SELECT T1.CODE AS CODE1 FROM CopyTIMEENTRY as T1 WHERE CopyTIMEENTRY.EMP_ID = T1.EMP_ID AND CopyTIMEENTRY.TRANSDATE = T1.TRANSDATE AND (T1.CODE = "A630" or T1.CODE = "A636" )))=True))
ORDER BY CopyTIMEENTRY.TCT, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE;
 

Salwa

New member
Local time
Today, 22:52
Joined
Oct 10, 2023
Messages
12
at the moment 1, as I have doctored the data
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
at the moment 1, as I have doctored the data
Thanks. I am hoping you will still get one with the above modified query, but you might get more, if you don't get an error instead. :)
 

Salwa

New member
Local time
Today, 22:52
Joined
Oct 10, 2023
Messages
12
i get the following message with your modified query.

At most one record can be returned by this subquery.​

 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,473
i get the following message with your modified query.

At most one record can be returned by this subquery.​

Right. That tells me you sort of have a many-to-many matches between records with code of a654 or a655 and those with code a630 or a636.

Try adding DISTINCT in the calculated query I gave you.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:52
Joined
Feb 19, 2002
Messages
43,275
Have you tried a left join without the subquery?
 

ebs17

Well-known member
Local time
Today, 14:52
Joined
Feb 7, 2020
Messages
1,946
From my point of view, the subquery of the EXISTS condition can be moved to the FROM part. But that also depends somewhat on the specific data situation. This means you can also use their fields in the main query.
It then looks something like this:
SQL:
SELECT
   CT.TCT,
   tPO.TCTNAME,
   CT.EMP_ID,
   CT.NAME,
   CT.TRANSDATE,
   CT.CODE,
   T1.CODE AS XCODE
FROM
   tblPayrollOfficers AS tPO
      INNER JOIN
         (CopyTIMEENTRY AS CT
            INNER JOIN
               (
                  SELECT
                     CODE,
                     EMP_ID,
                     TRANSDATE
                  FROM
                     CopyTIMEENTRY
                  WHERE
                     CODE IN("A630", "A636")
               ) AS T1
               ON CT.EMP_ID = T1.EMP_ID
                  AND
               CT.TRANSDATE = T1.TRANSDATE
         )
      ON tPOs.PO = CT.TCT
WHERE
   CT.CODE IN("a654", "a655")
ORDER BY
   CT.TCT,
   CT.NAME,
   CT.TRANSDATE
 
Last edited:

spaLOGICng

Member
Local time
Today, 05:52
Joined
Jul 27, 2012
Messages
127
Hi

How do I get the subquery T1.CODE value to be displayed as part of the main query?

Thanks heaps

SELECT CopyTIMEENTRY.TCT, tblPayrollOfficers.TCTNAME, CopyTIMEENTRY.EMP_ID, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE, CopyTIMEENTRY.CODE
FROM tblPayrollOfficers INNER JOIN CopyTIMEENTRY ON tblPayrollOfficers.PO = CopyTIMEENTRY.TCT
WHERE (((CopyTIMEENTRY.CODE)="a654" or CopyTIMEENTRY.CODE ="a655") AND ((Exists (SELECT T1.CODE AS CODE1 FROM CopyTIMEENTRY as T1 WHERE CopyTIMEENTRY.EMP_ID = T1.EMP_ID AND CopyTIMEENTRY.TRANSDATE = T1.TRANSDATE AND (T1.CODE = "A630" or T1.CODE = "A636" )))=True))
ORDER BY CopyTIMEENTRY.TCT, CopyTIMEENTRY.NAME, CopyTIMEENTRY.TRANSDATE;
Do not use a Sub Query in the Where Clause. Add the [CopyTimeEntry] into your query with the proper joins and then you can refer to any Column within that table. Joins work very similar to where clauses for the purpose of limiting recordsets.
 

Users who are viewing this thread

Top Bottom