473,511 Members | 15,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I exclude certain data from my query?

2 New Member
I have an Access 2016 table with several columns, one of which is a column SerialNumberExclude. I want to compare data from another table and if the serial number in this table matches any serial number in the SerialNumberExclude, I want to exclude those records in my query. hope that's not too confusing. I'm only YouTube University qualified with Access! LOL

**Additional info: Serial numbers are a text format and are entered in the column like this: 123, 1234, 345, 3456, etc. There can be multiple serial numbers associated with an item in my table. So if serial number 345 is in my table that I am comparing, I don't want to see it in my query.
Feb 18 '20 #1
8 5089
twinnyfo
3,653 Recognized Expert Moderator Specialist
Soupy,

Welcome to Bytes!

Example:

Expand|Select|Wrap|Line Numbers
  1. tblSoupySales
  2. SoupyID
  3. SoupyName
  4. SoupyPlace
  5. SerialNumberExclude
Expand|Select|Wrap|Line Numbers
  1. tblSoupySerials
  2. SerialNumber
  3. SoupyBreakfast
  4. SoupyLunch
  5. SoupyDinner
Your Query would look something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM tblSoupySales 
  3. LEFT JOIN tblSoupySerials 
  4. ON tblSoupySale.SerialNumberExclude = tblSoupySerials.SerialNumber 
  5. WHERE tblSoupySerials.SerialNumber Is Null;
This should display all records in tblSoupySales that do NOT have a matching record in tblSoupySerials.

Hope this hepps!
Feb 18 '20 #2
Soupy
2 New Member
Thanks for the reply. I guess I'm not understanding as this is new to me. Can this be done with a Criteria statement in the Access query? Where the query compares the two tables and returns all line items except those where the serial number is a certain number? Sorry for my ignorance on this.
Feb 18 '20 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
Soupy,

Yes and yes. You can build this query in the Query Designer or simply type it in in the SQL view.

If you are designing the query in the Query Designer, just add your two tables and create a relationship between the two tables. Double click the relationship line to edit it and select "Include all records from tblSoupySales ...." Then drag tblSoupySerials.SerialNumber to the fields list, and add "Is Null" in the criteria section.

This is a very common design. Often, you only want the records that have a matching record in the second table, but it is often helpful to know which records do not have corresponding records.

Again, hope this hepps!
Feb 18 '20 #4
NeoPa
32,557 Recognized Expert Moderator MVP
I hate to rain on anyone's parade here, but if you really mean you are entering multiple serials to exclude into a string value then trying to exclude any and all of those items then this is far more complicated than shown so far.

The short response is to avoid such a design like the plague, but that's another story.
Feb 25 '20 #5
SioSio
272 Contributor
twinnyfo wrote
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM tblSoupySales 
  3. LEFT JOIN tblSoupySerials 
  4. ON tblSoupySale.SerialNumberExclude = tblSoupySerials.SerialNumber 
  5. WHERE tblSoupySerials.SerialNumber Is Null;
Let's see this Query in action.
Expand|Select|Wrap|Line Numbers
  1. tblSoupySales
  2. +-------+---------+----------+-------------------+
  3. |SoupyID|SoupyName|SoupyPlace|SerialNumberExclude|
  4. +-------+---------+----------+-------------------+
  5. |     1 |      A |        s |                 1 |
  6. |     2 |       B |        t |                 2 |
  7. |     3 |       C |        u |                 1 |
  8. |     4 |       D |        v |                 1 |
  9. |     5 |       E |        w |                 1 |
  10. |     6 |       F |        x |                 1 |
  11. |     7 |       G |        y |                 2 |
  12. |     8 |       H |        z |                 2 |
  13. +-------+---------+----------+-------------------+
  14.  
  15. tblSoupySerials
  16. +------------+--------------+----------+-----------+
  17. |SerialNumber|SoupyBreakfast|SoupyLunch|SoupyDinner|
  18. +------------+--------------+----------+-----------+
  19. |         1  |            y |        y |         y | 
  20. |         2  |            n |        y |         y |
  21. |         3  |            y |        y |         n |
  22. |         4  |            n |        n |         n |
  23. |         5  |            y |        n |         y |
  24. |         6  |            n |        n |         y |
  25. +------------+--------------+----------+-----------+
  26.  
  27. Join result(Without "Where" statement)
  28. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  29. |SoupyID|SoupyName|SoupyPlace|SerialNumberExclude|SerialNumber|SoupyBreakfast|SoupyLunch|SoupyDinner|
  30. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  31. |     1 |      A |        s |                 1 |         1  |            y |        y |         y |
  32. |     2 |       B |        t |                 2 |         2  |            n |        y |         y |
  33. |     3 |       C |        u |                 1 |         1  |            y |        y |         y |
  34. |     4 |       D |        v |                 1 |         1  |            y |        y |         y |
  35. |     5 |       E |        w |                 1 |         1  |            y |        y |         y |
  36. |     6 |       F |        x |                 1 |         1  |            y |        y |         y |
  37. |     7 |       G |        y |                 2 |         2  |            n |        y |         y |
  38. |     8 |       H |        z |                 2 |         2  |            n |        y |         y |
  39. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  40.  
  41. Query execute result
  42. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  43. |SoupyID|SoupyName|SoupyPlace|SerialNumberExclude|SerialNumber|SoupyBreakfast|SoupyLunch|SoupyDinner|
  44. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  45. +-------+---------+----------+-------------------+------------+--------------+----------+-----------+
  46.  
Result hit is 0 record.
The result it actually want is
Expand|Select|Wrap|Line Numbers
  1. +-------+---------+----------+-------------------+
  2. |SoupyID|SoupyName|SoupyPlace|SerialNumberExclude|
  3. +-------+---------+----------+-------------------+
  4. |     3 |       C |        u |                 1 |
  5. |     4 |       D |        v |                 1 |
  6. |     5 |       E |        w |                 1 |
  7. |     6 |       F |        x |                 1 |
  8. |     7 |       G |        y |                 2 |
  9. |     8 |       H |        z |                 2 |
  10. +-------+---------+----------+-------------------+
  11.  
Fixed the query by modified the table structure.
Expand|Select|Wrap|Line Numbers
  1. tblSoupySales
  2. +-------+---------+----------+------------+
  3. |SoupyID|SoupyName|SoupyPlace|SerialNumber|
  4. +-------+---------+----------+------------+
  5. |     1 |      A |        s |         10 |
  6. |     2 |       B |        t |         20 |
  7. |     3 |       C |        u |         30 |
  8. |     4 |       D |        v |         40 |
  9. |     5 |       E |        w |         50 |
  10. |     6 |       F |        x |         60 |
  11. |     7 |       G |        y |         70 |
  12. |     8 |       H |        z |         80 |
  13. +-------+---------+----------+------------+
  14.  
  15. tblSoupySerials
  16. +-------------------+--------------+----------+-----------+
  17. |SerialNumberExclude|SoupyBreakfast|SoupyLunch|SoupyDinner|
  18. +-------------------+--------------+----------+-----------+
  19. |                10 |            y |        y |         y |
  20. |                20 |            n |        y |         y |
  21. +-------------------+--------------+----------+-----------+
  22.  
New Query is
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM tblSoupySales as a 
  3. LEFT JOIN tblSoupySerials as b 
  4. ON (a.SerialNumber = b.SerialNumberExclude) 
  5. WHERE b.SerialNumberExclude IS NULL;
  6.  
Expand|Select|Wrap|Line Numbers
  1. Join result(Without "Where" statement)
  2. +-------+---------+----------+------------+-------------------+--------------+----------+-----------+
  3. |SoupyID|SoupyName|SoupyPlace|SerialNumber|SerialNumberExclude|SoupyBreakfast|SoupyLunch|SoupyDinner|
  4. +-------+---------+----------+------------+-------------------+--------------+----------+-----------+
  5. |     1 |      A |        s |         10 |                10 |            y |        y |         y |
  6. |     2 |       B |        t |         20 |                20 |            n |        y |         y |
  7. |     3 |       C |        u |         30 |              NULL |         NULL |     NULL |      NULL |
  8. |     4 |       D |        v |         40 |              NULL |         NULL |     NULL |      NULL |
  9. |     5 |       E |        w |         50 |              NULL |         NULL |     NULL |      NULL |
  10. |     6 |       F |        x |         60 |              NULL |         NULL |     NULL |      NULL |
  11. |     7 |       G |        y |         70 |              NULL |         NULL |     NULL |      NULL |
  12. |     8 |       H |        z |         80 |              NULL |         NULL |     NULL |      NULL |
  13. +-------+---------+----------+------------+-------------------+--------------+----------+-----------+
  14.  
  15. Query execute result
  16. +-------+---------+----------+------------+
  17. |SoupyID|SoupyName|SoupyPlace|SerialNumber|
  18. |     3 |       C |        u |         30 |
  19. |     4 |       D |        v |         40 |
  20. |     5 |       E |        w |         50 |
  21. |     6 |       F |        x |         60 |
  22. |     7 |       G |        y |         70 |
  23. |     8 |       H |        z |         80 |
  24. +-------+---------+----------+------------+
  25.  
Feb 27 '20 #6
twinnyfo
3,653 Recognized Expert Moderator Specialist
Sio,

Thanks for the discussion. However, OP states:
I want to compare data from another table and if the serial number in this table matches any serial number in the SerialNumberExclude, I want to exclude those records in my query.
Based upon this definition, and based upon the data in your tables above, my query would, indeed, return no records. This is because every record in tblSoupySales has a corresponding record in tblSoupySerial. However, when you change any of the values in the Field SerialNumberExclude to a value that does NOT exist in tblSoupySerials, that record will show up.

I believe this this the result the OP wants, but outside of OP confirming or denying, we must go on mere interpretation of their post.
Feb 27 '20 #7
NeoPa
32,557 Recognized Expert Moderator MVP
I'd just like to repeat here that the best advice we can give to this OP is that they should be approaching this completely differently.

DO NOT use lists within a string and expect a database system to work well with it.
DO NOT use variable-length strings in your PK if it's at all possible to avoid.

They have explained they're new and mistakes are easily made. Unless we, as experts, explain this to them clearly they may go off without that most important understanding.
Apr 25 '20 #8
lewish95
37 New Member
The EXCEPT operator is used to exclude like rows that are found in one query but not another. It returns rows that are unique to one result. To use the EXCEPT operator, both queries must return the same number of columns and those columns must be of compatible data types.
Apr 29 '20 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4396
by: 'bonehead | last post by:
Greetings, I'd like to figure out some syntax for retrieving the data from a table when I don't know all the of field names. What I do know are, the name of the table, the names of the primary...
3
20820
by: Oliver Stratmann | last post by:
Hello All! I'm looking for a description of the needs of space for a certain data-type. How much space does e.g. a NUMERIC(1,0)- field take? (??? Bytes?) Is there a listing in the...
5
2320
by: MX1 | last post by:
Here's an interesting one. I have a database that has user level security invoked. The design is normalized and I have one field in particular called Rep ID. Any thoughts on how I can allow all...
2
4407
by: Peter | last post by:
I have a site which uses forms authentication but I would like to exclude certain pages on the site from authentication. In other words I would like the user to be able to get to these pages...
2
1917
by: enambo | last post by:
In MS Access, i would to give the locking system to certain user, by which they can enable or disable to work on certain data entry or reports. How can i work on that? Kartik
1
1221
by: bmdlfc | last post by:
Morning all, long time reader, first time poster. I need to manipulate an existing CSV file and create another one from it but excluding some fields. For example, the fields I have in the...
4
1352
by: Prodian | last post by:
Im trying to parse a recv from a telnet session then only grab certain data. Heres an example of the recv that Im storing into a string: Internet 204.189.124.205 0 001a.a01f.4e5a ...
1
12221
by: bips2005 | last post by:
i have got two tables,one customer with fields cust_id,email and another with field cust_id,domname,expirydate. i have this query to combine these two tables to give me email and the expirydate as ...
9
1815
by: Jonathan Austin | last post by:
My question is basically how would I be able to assign certain data automatically in a pattern (i.e. If employee1 is Alpha, employee2 is Bravo, employee3 is Charlie so on and so forth). How would I...
18
3264
by: trixxnixon | last post by:
say i want to query a few fields from a table for the purposes of a report. how might i have the query only return records that have data in at least one of the fields to prevent the repot from...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.