Results 1 to 4 of 4
  1. #1
    jpunja is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2016
    Posts
    3

    Left join doesn't work correctly

    I have a database (table1) with:

    ID Name Status
    1 John Accepted
    2 John Removed
    3 John Transferred
    4 William Transferred



    I want a table where I count the number of times someone is in the database, but don't count the status status accepted. The results should be:
    ID Name Status # rows without status accepted
    1 John Accepted 0
    2 John Removed 2
    3 John Transferred 2
    4 William Transferred 1


    I have the following query:

    Code:
    xxx
    
    
    LEFT JOIN 
    		(SELECT [table1].[id], [table1].[name], [table1].[status], COUNT(*) as [Number rejected]
    			FROM [table1]
    			WHERE [table1].[status] <> "Accepted"
    			GROUP BY [table1].[id], [table1].[name], [table1].[status]
    		) as SubQuery1
    		ON ([table1].[id] = SubQuery1.[id] AND [table1].[name] = SubQuery1.[name] AND [table1].[status] = SubQuery1.[status]))
    However, the problem is that with this query all rows with status "accepted" are removed. What do I wrong?

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    I believe I got what you need with 2 queries. Create Query1 and save, then use it in Query2 and link it on Name back to Table1.

    Query1:
    SELECT Table1.Name, Count(Table1.Name) AS CountOfName
    FROM Table1
    WHERE (((Table1.Status)<>"Accepted"))
    GROUP BY Table1.Name;

    Query2:
    SELECT Table1.ID, Table1.Name, Table1.Status, IIf([Status]="Accepted",0,[CountOfName]) AS vCountOfName
    FROM Table1 INNER JOIN Query1 ON Table1.Name = Query1.Name;

  3. #3
    jpunja is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2016
    Posts
    3
    Thanks Bulzie. Works perfect!

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Be aware that "Name" is a reserved word in Access and shouldn't be used as an object name.
    Plus it is not very descriptive: "Name" of what?? Company Name, First Name? Component Name? The dog's name??


    List of reserved words in Access/Jet/ACE/SQL: http://allenbrowne.com/AppIssueBadWord.html

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Case statement doesn't work correctly
    By Cecil in forum Access
    Replies: 3
    Last Post: 05-16-2016, 03:21 PM
  2. Replies: 8
    Last Post: 07-03-2015, 05:03 PM
  3. Replies: 3
    Last Post: 06-30-2014, 12:53 AM
  4. Inner Join works but not Left Join
    By kwooten in forum Queries
    Replies: 1
    Last Post: 12-11-2012, 05:09 PM
  5. Replies: 3
    Last Post: 02-02-2011, 01:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums