简体   繁体   中英

SELECT Query in ms access

I have an ms access table like the following:

 
 
 
UserName | PC_Type
Rob | Desktop
Frank | Laptop
Rob | Laptop
Lindesy | ThinCient
Mark | Desktop
Paul | Desktop
Paul | ThinClient

How can I create a SELECT query that returns only users who doesn't have neither ThinClient nor Laptop?
I tried:

SELECT DISTINCT UserName, PC_Type 
From Table 
GROUP BY UserName, PC_Type
HAVING PC_Type <> "ThinClient" AND PC_Type <> "Laptop"  
[or where PC_Type Not In ("ThinClient", "Laptop")]

but this doesn't filter users excluding who has already a Laptop (or ThinClient) because it returns for instance "ROB | Desktop" who has already a laptop (or "Paul | Desktop" who has already a ThinClient). I tried queries to find duplicates, difference queries, crossqueries, but I'm starting to get confused. I was trying to create a subquery or a partition query but I don't know if I'm doing something right.

Anyone can help me with this, please ? I'd expect from the query to return only Mark.

You can use aggregation and HAVING :

SELECT UserName
From Table 
GROUP BY UserName
HAVING SUM(IIF(PC_Type IN ("ThinClient", "Laptop"), 1, 0) = 0 ;

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