Results 1 to 7 of 7
  1. #1
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83

    NZ and Sum by Group

    Here is my SQL. i am trying to Sum the Stock and Used data by DateEnter and if its nothing I want it to show up as a 0.



    But I get the following error
    You tried to execute a query that does not include the specified expression 'Item' as part of an aggregate function

    INSERT INTO AllDataTogether ( DateEnter )
    SELECT RawInfoUNION.*, [enter date] AS DateEnter
    FROM (SELECT Information.Item, Location, Supplier, Used, Sum(Nz(Raw Data]![Stock],0)) AS Stock FROM Information LEFT JOIN [Raw Data] ON Information.Item = [Raw Data].Item
    UNION SELECT [Information 2].Item, Location, Supplier, Sum(Nz(Raw Data 2]![Used],0)) AS Used, Sum(Nz(Raw Data 2]![Stock],0)) AS Stock FROM [Information 2] LEFT JOIN [Raw Data 2] ON [Information 2].Item = [Raw Data 2].Item) AS RawInfoUNION
    WHERE (((RawInfoUNION.Location)="NJ")) OR (((RawInfoUNION.Location)="CA"));

  2. #2
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Since your using a sum aggregate function you need to include the Group By
    Code:
    SELECT Information.Item, Location, Supplier, Used, Sum(Nz(Raw Data]![Stock],0)) AS Stock FROM Information LEFT JOIN [Raw Data] ON Information.Item = [Raw Data].Item Group By Item, Location, Supplier
    UNION SELECT [Information 2].Item, Location, Supplier, Sum(Nz(Raw Data 2]![Used],0)) AS Used, Sum(Nz(Raw Data 2]![Stock],0)) AS Stock FROM [Information 2] LEFT JOIN [Raw Data 2] ON [Information 2].Item = [Raw Data 2].Item Group By Item, Location,Supplier) AS RawInfoUNION
    Also in the first part of the union you have the field Used In the Second Part it's a calculation. If it's a field in the first part of the union include it in the group by. If not you need to put in a calculation such as Sum(0) As Used. instead of just used.

  3. #3
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    I get the following error and I can't figure where to remove/add the ")".

    If I keep the ")" to complete he FROM, I get the error Extra ) in query expression 'Supplier) AS RawInfoUnion WHERE (((RawInfo UNION.Location)="NJ")) OR (((RawInUnion.Location)="CA"))'

    and if I remove it I get missing operator

    Code:
    INSERT INTO AllDataTogether ( DateEnter )
    SELECT RawInfoUNION.*, [enter date] AS DateEnter
    FROM (SELECT Information.Item, Location, Supplier, Used, Sum(Nz([Raw Data]![Stock],0)) AS Stock FROM Information LEFT JOIN [Raw Data] ON Information.Item = [Raw Data].Item Group By Item, Location, Supplier
    UNION SELECT [Information 2].Item, Location, Supplier, Sum(Nz([Raw Data 2]![Used],0)) AS Used, Sum(Nz([Raw Data 2]![Stock],0)) AS Stock FROM [Information 2] LEFT JOIN [Raw Data 2] ON [Information 2].Item = [Raw Data 2].Item) Group By Item, Location,Supplier) AS RawInfoUNION
    WHERE (((RawInfoUNION.Location)="NJ") OR (((RawInfoUNION.Location)="CA"));

  4. #4
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    In the second part of the Union in the On part of the left join you have a ) prior to the Group by Item, Location,supplier) Take that one out.

  5. #5
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    Now I'm getting a error sayng Syntax error in query expression '((RawInfoUnion.Locattion)="NJ") OR (((RawInfoUNION.Location)="CA")):'

    I called the query RayTest and attached the dbm Make sure you copy the code into raytest query since its missing the last WHERE statement

    Code:
    INSERT INTO AllDataTogether ( DateEnter )SELECT RawInfoUNION.*, [enter date] AS DateEnter
    FROM (SELECT Information.Item, Location, Supplier, Used, Sum(Nz([Raw Data]![Stock],0)) AS Stock FROM Information LEFT JOIN [Raw Data] ON Information.Item = [Raw Data].Item Group By Item, Location, Supplier
    UNION SELECT [Information 2].Item, Location, Supplier, Sum(Nz([Raw Data 2]![Used],0)) AS Used, Sum(Nz([Raw Data 2]![Stock],0)) AS Stock FROM [Information 2] LEFT JOIN [Raw Data 2] ON [Information 2].Item = [Raw Data 2].Item Group By Item, Location,Supplier) AS RawInfoUNION
    WHERE (((RawInfoUNION.Location)="NJ") OR (((RawInfoUNION.Location)="CA"));
    If I remove the WHERE statement but I wish to include this I get the following error message
    The specified field 'Item' could refer to more than one table listed in the FROM clause of your SQL statement
    Attached Files Attached Files

  6. #6
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    Thank you for all your help RayMilhon

    I was able to figure it out. The table was not being properly called

  7. #7
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Glad you got it figured out.

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

Similar Threads

  1. query group by over another group by?
    By gap in forum Queries
    Replies: 2
    Last Post: 07-04-2011, 12:59 AM
  2. Replies: 5
    Last Post: 11-29-2010, 08:16 AM
  3. how to group?
    By wconan in forum Access
    Replies: 2
    Last Post: 03-08-2010, 09:44 PM
  4. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 AM
  5. Group By
    By SMAlvarez in forum Access
    Replies: 0
    Last Post: 10-28-2008, 04:40 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