+ Reply to Thread
Results 1 to 21 of 21

Query to show what is NOT there

  1. #1
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Query to show what is NOT there

    I have a table and I want to run a query that shows the following:-

    I have a Department field and a Subject Group Field.

    Each Department has a number of Subject Groups.

    So you may have Dept "Teaching", which has 4 Subject Groups, SG T1, SG T2, SG T3 and SG T4

    What I want to find out from the data is if there are any instances of mismatch so where there are records that are Dept "Teaching" but there is a Subject Group that is not one of the above listed e.g. SG X1.

    How do I write this in the query?

    At the moment I have this

    DEPT
    "Teaching"

    SUB GRP
    Not "SG T1"
    Not "SG T2"
    Not "SG T3"
    Not "SG T4"

    This just returns everything.....is this the wrong syntax??

    Thanks!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    Not really enough info to give a definitive answer, but you could use a SQL statement containing
    Please Login or Register  to view this content.
    Between the lines reading suggests you need "Where [DEPT] = 'Teaching' ... AND " in the Where clause.

    You may also have a table listing the valid choices in which case you could use a 'Select * From ...' statement instead of the hard coded list.
    Last edited by cytop; 10-04-2016 at 07:22 AM.

  3. #3
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    I thought I need OR........ because there are multiple possibilities ......so in my query design would want

    Dept Subject Group
    "Teaching" Not In "SG T1"
    "Teaching" Not In "SG T2"

    etc

    Is the Not In the right wording?

  4. #4
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    I tried Not In "SG T1" but it wouldn't accept it.....

    I want the query to show me records where it might have "Teaching" and "SG X1" (i.e. a subject group that is not one of the correct ones, the correct ones being SG T1 etc

  5. #5
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    I could list all the possible combinations I don't want the Dept and subject groups to be but that would take me a lot longer.......

  6. #6
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Let me try to explain more.

    I have 6 Departments. Each Department has a different number of Subject Groups. A subject group only belongs to one Department.
    In some cases the incorrect Department/Subject Group has been attached resulting in a mismatch.
    I need to be able to identify these in my table.
    How do I design a query that will show me where there is a mismatch?
    So where a record has the Department "Teaching" but the subject group is NOT "SG T1, SG T2, SG T3 or SG T4"?
    I could list all the SGs that are NOT T1, T2 etc but that would take me forever and I've to do that for each Department.
    If I could just find the syntax in my query design that shows records where Department = Teaching AND Subject Group = NOT "SG T1" etc that would help....unless this is not possible.....

  7. #7
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    I want the query to show me records where it might have "Teaching" and "SG X1"
    I understand that - ever think it can be difficult to get a concept across to someone who is learning? Now try without the benefit of seeing what that person is seeing/working with the same information...

    If you are using the Access query designer then start a new query definition, add the table you want to query. Drag the '*' at the top of the field list to the 1st column of the output grid.

    Next drag the Department (DEPT?) field to the 2nd column and add 'Teaching' to the Criteria column.

    Drag the Subject Group field to the 3rd and add:
    NOT IN ("SG T1", "SG T2" ...)
    on the same row as the 'Teaching' entry for the DEPT column - This is treated as an implicit AND in Access

    Make sure the 'Show' checkboxes for Cols 2 & 3 are unchecked or you will get duplicate columns in the output.

  8. #8
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Sorry if I have come across the wrong way I was just trying to be me more clear about what I was trying to do.
    I have done everything you have said in your last post but I cannot get it to work.

  9. #9
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    Copy a representative sample out of the table you want to query and paste to an excel sheet, Anonymise any personal data, if any, and upload to the forum. (Only suggesting Excel as it's easier/smaller and there's bound to be shed load of other unrelated stuff in the DB).

    I'll build the query and post the SQL text you can just paste into a new query.

  10. #10
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Thank you! :-)
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    Just to confirm...

    You want all records where DEPT is 'Education, Childhood and Inclusion - D&S' And SUBJECT GROUP not equal to any of the following:

    D&S CHILDHOOD
    D&S ED, PS&CNL
    D&S EDU
    D&S ERLY CHILD
    D&S PRIM PG
    D&S TSOL ENG LN
    D&S TSOL TCH ED

    It's never quite that simple though. Is that list likely to change or will you need to run the same report for other departments?

    EDIT - My mistake. That lists all the current entries for 'Education, Childhood and Inclusion - D&S'. Do I assume the red highlighted item (D&S PRIM PG) should not be included? Any others?
    Last edited by cytop; 10-04-2016 at 08:48 AM.

  12. #12
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Sorry just needed to create a table showing you the correct Dept and Sub Group relationships
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    These will not change....

  14. #14
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    Ta for that. Other things have caught up with me. If no one else comes into this I'll get back to it tomorrow morning

  15. #15
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Great, thank you so much for your help! :-)
    If I mange to get my "Access Amateur" brain around it in the meantime I will post my solution!

  16. #16
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    This, pasted into a new query SQL window, returns the 4 highlighted rows from your sample yesterday.

    PHP Code: 
    SELECT MainList.*
    FROM MainList INNER JOIN Sheet1 ON MainList.SUBJECT_GROUP Sheet1.[Short name]
    WHERE (((MainList.DEPT)="Education, Childhood and Inclusion - D&S") AND ((Sheet1.[Short name]) Not In (SELECT DISTINCT Sheet1.[Short name]
    FROM Sheet1
    WHERE Sheet1
    .Dept="ECI"))); 
    'MainList' is the first excel sheet from yesterday & 'Sheet1' the DEP & SG list uploaded later. You need to replace with your actual table names.

    If the actual table names have spaces then you should use '[' & ']' to delimit them - actually, use the square brackets in any case. For example:
    PHP Code: 
    ...DISTINCT [Sheet1].[Short name]... 
    Last edited by cytop; 10-05-2016 at 04:18 AM. Reason: Additional comment [ & ]

  17. #17
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    Hi,

    Thank you for this. Unfortunately being very new to SQL I don't really understand how to write all the code and create the necessary tables with inner joins.

    Is there any way to build the query in the wizard rather than using SQL?

    Thanks

  18. #18
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    No longer needed it seems.
    Last edited by cytop; 10-05-2016 at 06:06 AM.

  19. #19
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    It is still needed, I am just struggling to understand the SQL side as my knowledge is very limited. I had to find an alternative solution as I was under pressure to get this done but I want to know how to do it right.

  20. #20
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Query to show what is NOT there

    It seemed you had reposted it... Never mind.

    There is no code needed, you simply paste the following into the SQL window of a new query definition.

    You need to replace 'MainList' with the actual table name used to create your first extract in post #10. I'm assuming the 'DEP S&G' is the table used as the source for the 2nd extract in post #12. If not, that needs to be replaced also.



    SELECT [MainList].*
    FROM [MainList]

    INNER JOIN [DEP & SG] ON
    [MainList].[SUBJECT_GROUP] = [DEP & SG].[Short name]

    WHERE ((([MainList].[DEPT])="Education, Childhood and Inclusion - D&S")
    AND (([DEP & SG].[Short name])

    Not In (SELECT DISTINCT [DEP & SG].[Short name]
    FROM [DEP & SG]
    WHERE [DEP & SG].[Dept]="ECI")));
    Last edited by cytop; 10-06-2016 at 09:24 AM.

  21. #21
    Forum Contributor
    Join Date
    03-10-2014
    Location
    Sheffield
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Query to show what is NOT there

    I have posted a separate question about something else I need to do in my database.
    Thank you for clarifying.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Query to show 1, 11, 111
    By vjharry in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2016, 10:04 AM
  2. MS QUERY - Filter To Show Only Dates Within This Month
    By Rosco88 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-18-2015, 06:20 AM
  3. Replies: 1
    Last Post: 05-27-2015, 08:13 AM
  4. Replies: 6
    Last Post: 09-06-2014, 10:50 AM
  5. Date-bound query - how to show values from different timestamps
    By learning_vba in forum Access Tables & Databases
    Replies: 0
    Last Post: 07-02-2014, 01:07 PM
  6. PIVOT TABLE - Show Details query
    By rich534714 in forum Excel General
    Replies: 2
    Last Post: 02-17-2011, 06:30 AM
  7. Query to Show 0 if no match found
    By pr4t3ek in forum Access Tables & Databases
    Replies: 2
    Last Post: 07-01-2009, 12:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1