Getting the right criteria (1 Viewer)

Dumferling

Member
Local time
Today, 17:34
Joined
Apr 28, 2020
Messages
102
Maybe it is just because it is the end of a pretty hectic week but I just can't think how to do this. I have a list of contract counterparties (the one side). On the many side we have contracts. Contracts can have a status of "Current" and "Terminated". A counterparty may, for example, have three contracts - two with a "Terminated" status and one with a "Current" status.

I want to show those Counterparties where all their related contracts are "Terminated". Any Counterparty with even one related contract with a "Current" status would be excluded from the list. I just can't think how to set up the query to handle this. I am pretty sure this is simple but I can't see it. Any suggestions most welcome!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:34
Joined
Feb 19, 2002
Messages
43,275
Join the counterparties to the contracts. Do not select any columns from the contract tables. Add a where clause
Where Status = "Current"
Then change it to a totals queries and count the number of rows returned for each counterparty.
Add additional query
Where Count("*") = 0
 
Last edited:

GPGeorge

Grover Park George
Local time
Today, 08:34
Joined
Nov 25, 2004
Messages
1,873
Off the top of my head:

SELECT ContractPartyID, ContractParty
FROM tblContractPary WHERE
ContractPartyID NOT EXISTS
(SELECT ContractPartyID FROM tblContracts WHERE ContractStatus ="Current")

Or use Pat's approach.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:34
Joined
Feb 19, 2002
Messages
43,275
I use joins rather than subselects except where subselects are the only option because Access does not optimize subselects well. Obviously small recordsets are irrelevant. You can do whatever you want.
 

ebs17

Well-known member
Local time
Today, 17:34
Joined
Feb 7, 2020
Messages
1,946
I would write @GPGeorge's query like this
SQL:
SELECT
   C.ContractPartyID,
   C.ContractParty
FROM
   tblContractParty AS C
WHERE
   NOT EXISTS
      (
         SELECT
            NULL
         FROM
            tblContracts AS X
         WHERE
            X.ContractPartyID = C.ContractPartyID
               AND
            X.ContractStatus = "Current"
      )
because Access does not optimize subselects well
This doesn't necessarily have to be the case if the developer has already optimized the query.
If there is only one key in the correlation, the existing index on the key is used.

Or
SQL:
SELECT DISTINCT
   C.ContractPartyID,
   C.ContractParty
FROM
   tblContractParty AS C
      LEFT JOIN
         (
            SELECT
               ContractPartyID
            FROM
               tblContracts
            WHERE
               ContractStatus = "Current"
         ) AS X
         ON C.ContractPartyID = X.ContractPartyID
WHERE
   X.ContractPartyID IS NULL
 
Last edited:

plog

Banishment Pending
Local time
Today, 10:34
Joined
May 11, 2011
Messages
11,646
1 SELECT---

Code:
SELECT CounterPartyName
FROM CounterParties
INNER JOIN Contracts on Contracts.CounterPartyID = CounterParties.CounterPartyID
GROUP BY CounterPartyName
HAVING MIN(ContractStatus)="Terminated")

If any of their contracts are "Current", then that would be the minimum value for all of their contract statuses and they would not be included in the above query. But if the minimum value is "Terminated" then that means all their contract statuses are "Terminated".
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:34
Joined
Feb 19, 2002
Messages
43,275
I'm sure there's at least one more way to do this;)
 

Users who are viewing this thread

Top Bottom