Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49

    Trouble with Sum(iif... statement

    Hello,



    I have the following query:

    SELECT Data.[Item Size], Data.[Color], Sum(Data.[Line Item Qty]) AS [SumOfLine Item Qty], Sum(IIf(Data.[Dec Description] Like “*xyz*”,Data.[Count],0)) AS [Second Total]
    FROM Data INNER JOIN [Sales Orders] ON Data.[SO #] = [Sales Orders].[Sales Orders]
    GROUP BY Data.[Item Size], Data.[Color]
    HAVING (((Data.[Color])<>"NA"));

    I keep getting a parameter prompt for the first asterisk in my like comparison, a second parameter prompt for the xyz, and a third prompt for the second asterisk.

    I haven't used Access in quite some time. Does anyone know what I am doing wrong?

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    your design is wrong,
    1 field has the *xyz*
    2nd field has the sum.

    dont do it all in 1 field.

  3. #3
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    Sorry I'm pretty rusty. What do you mean by one field. The first sum is being summed on one alias name, and the second sum has a different alias name.

    So a basic example of the data:

    Description - Count Color
    abc 4 Green
    xyz 5 Green
    def 2 Green
    xyz 4 Green

    So I want the results to look like this

    Color Total Second Total
    Green 15 9



    So I want it to tell me for each color, how many are there total, and how many of the total have a description (XYZ)

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you attach a sample of the db here? In a brief test, my query ran without prompts:

    Sum(IIf([l_name] Like "*ew*",[zone],0)) AS Whatever
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    dont put the IF in the sum stmt.
    if you must, make 2 querys
    Q1 sum the [ Item Qt] per [Dec Description]
    Q2 on Q1, NOW do the IF,
    select * ,iif(
    [Dec Description] like “*xyz*”),0,SumOfItmCount) from Q1

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by ranman256 View Post
    dont put the IF in the sum stmt.
    I'm curious why not. I also don't think your 2-query solution returns the data as desired, unless I'm missing something.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    Quick Test - Copy.accdb

    Hopefully this clears up what I'm trying to do. Thanks!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your query is:

    SELECT Data.[Item Size], Data.Color, Sum(Data.[Line Item Qty]) AS [SumOfLine Item Qty], Sum(IIf(Data.Description Like [“]*xyz*[”],Data.Count,0)) AS [New Total]
    FROM Data INNER JOIN [Sales Orders] ON Data.[SO #] = [Sales Orders].[Sales Orders]
    GROUP BY Data.[Item Size], Data.Color
    HAVING (((Data.Color)<>"NA"));

    note the bracketing in your IIf(). Try:

    SELECT Data.[Item Size], Data.Color, Sum(Data.[Line Item Qty]) AS [SumOfLine Item Qty], Sum(IIf(Data.Description Like "*xyz*",Data.Count,0)) AS [New Total]
    FROM Data INNER JOIN [Sales Orders] ON Data.[SO #] = [Sales Orders].[Sales Orders]
    GROUP BY Data.[Item Size], Data.Color
    HAVING (((Data.Color)<>"NA"));

    which works without prompt for me.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    Perfect! Thanks so much! That is a huge help. I did not realize the bracketing was such a big deal.

  10. #10
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    Quote Originally Posted by Chad Access View Post
    Perfect! Thanks so much! That is a huge help. I did not realize the bracketing was such a big deal.
    Unfortunately when I make the change in my full scale DB, I get the same parameter prompts. The only difference is the fields in the table have names that contain a space, and thus require the brackets.

    What can I do? I do not think I can change the data file because it comes in from another source.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What is the SQL of the query in the full scale db?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    SELECT Data.[Item Size], Data.[Lid Color], Sum(Data.[Line Item Qty]) AS [SumOfLine Item Qty], Sum(IIf(Data.[Dec Description] Like [“]*xyz*[”],Data.[Cup Count],0)) AS [TotalTwo]
    FROM Data INNER JOIN [Sales Orders] ON Data.[SO #] = [Sales Orders].[Sales Orders]
    GROUP BY Data.[Item Size], Data.[Lid Color]
    HAVING (((Data.[Lid Color])<>"NA"));

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Does this work?

    SELECT Data.[Item Size], Data.[Lid Color], Sum(Data.[Line Item Qty]) AS [SumOfLine Item Qty], Sum(IIf(Data.[Dec Description] Like "*xyz*",Data.[Cup Count],0)) AS [TotalTwo]
    FROM Data INNER JOIN [Sales Orders] ON Data.[SO #] = [Sales Orders].[Sales Orders]
    GROUP BY Data.[Item Size], Data.[Lid Color]
    HAVING (((Data.[Lid Color])<>"NA"));
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    Chad Access is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    49
    YES! Yes, it does! I'm looking for the difference still, what was it???

    Much appreciated! Thanks.

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Basically I just got rid of the brackets, but I also noticed your SQL had angled quotes and mine were straight. It could be that they are treated differently.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  2. Trouble with CASE Statement Syntax
    By hellfire45 in forum Access
    Replies: 5
    Last Post: 03-05-2015, 02:21 PM
  3. Replies: 1
    Last Post: 08-29-2013, 06:14 AM
  4. Replies: 1
    Last Post: 02-22-2013, 12:46 PM
  5. Trouble with a Select Statement
    By mrfixit1170 in forum Programming
    Replies: 3
    Last Post: 09-17-2012, 11:18 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