Results 1 to 5 of 5
  1. #1
    ricksil is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    10

    Problem with a MS Access self-join

    I'm having some trouble with this MS Access query:

    Code:
    select  t.code1,
            sum(nz(iw.qty,0)) as iwcnt,
            sum(nz(t.qty,0)) as totcnt
    from    quantities iw,
            quantities t
    where   t.code1 = iw.code1
    and     iw.code2 = 'iw'
    group by t.code1;
    In the quantities table there is a column called code2. I am joining the quantities table to itself because I am trying to see what percentage has a code2 of "iw". For example, our data looks like this:

    Code:
    code1   code2   qty
    -----   -----   ---
      56       iw     3
      56       xx    11
    So with my query, I was hoping to return:



    Code:
    code1   iwcnt   totcnt
    -----   -----   ------ 
       56       3       14
    This means that 3 out of 14 were for code2 of "iw". The t table holds the total data and the iw table holds the data just where code2 = "iw".

    But instead of returning 3 out of 14, it is returning 22 out of 14!:

    Code:
    code1   iwcnt   totcnt
    -----   -----   ------ 
       56      22       14
    Does anybody have any idea why?

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    try

    from quantities iw inner join quantities t on t.code1 = iw.code1
    where iw.code2 = 'iw'

  3. #3
    ricksil is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    10
    No joy. I tried:
    Code:
    select  t.code1,        sum(nz(iw.qty,0)) as iwcnt,
            sum(nz(t.qty,0)) as totcnt
    from    quantities iw inner join quantities t on (t.code1 = iw.code1)
    and     iw.code2 = 'iw'
    group by t.code1;
    and I got the same results.

  4. #4
    DrGUI is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    Try this:

    Code:
    SELECT code1, sum(qty) AS iwcnt, (SELECT sum(qty) FROM tbl_Codes) AS totcnt FROM tbl_Codes
    WHERE code2="iw" group by code1

  5. #5
    ricksil is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    10
    That worked! Thanks so much, Doc.

    Any idea why my original approach doesn't work?

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

Similar Threads

  1. problem in self join query
    By royalhishighness in forum Queries
    Replies: 2
    Last Post: 12-30-2013, 02:27 PM
  2. Problem with Natural Join
    By Kaisa in forum Queries
    Replies: 11
    Last Post: 07-19-2011, 07:07 AM
  3. Access Update with Join Problem
    By saascuba in forum Access
    Replies: 3
    Last Post: 11-03-2010, 01:20 PM
  4. Problem with Join
    By sujitshukla in forum Queries
    Replies: 1
    Last Post: 08-26-2010, 07:25 AM
  5. Many to many self-join problem
    By dbdbdo in forum Database Design
    Replies: 1
    Last Post: 07-18-2010, 09:31 AM

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