Results 1 to 7 of 7
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    concatenate fields plus grouping and counting results

    Hi all,


    I have a table Called tblproduct
    I am trying to create a query to first; concatenate fields then use the said field to group on in the same table...

    Code:
    Select City + '_' + State as Demograph, Sum(Product_Cost) as Total, SaleDate from tblProduct  Groupby Demograph having count (Demograph) >5
    I need the result as follow: concatenate the City and State, Count the number of products for the same saledate five of more and give me the total Cost.
    It gives me an error stating that I can't concatenate a field because its not an aggregate field? Can anyone give me some guidance?

    Maybe I'm missing a step

    Thanks Slim

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

    GROUP BY City + '_' + State

    Edit, as a rule you should use & to concatenate, not +.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Thanks for responding paul...I'm kinda of lost. I'm still getting the error. Should I be using a subquery being that I need to concatenate a field I want to use in the result. I'm not strong in subqueries though. It doesn't seem that I can group on and total on an alias that's not in the table. Can you assist please? I need to concatenate the fields for City and State as Demograph, get the sum of the product cost for the products with the saledate greater than 5..need to return results as Demograph, saledate, Sumof Product Cost and count the number of product per demograph. Hope I'm making sense.

  4. #4
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Is there anyone that can assist pls.

  5. #5
    Minty is online now VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You cant refer to the Alias in the grouping section sometimes. Try something like;

    Select City & '_' & State as Demograph, Sum(Product_Cost) as Total, SaleDate from tblProduct Group by City & '_' & State , SaleDate Having count (City & '_' & State) >5
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Quote Originally Posted by Minty View Post
    You cant refer to the Alias in the grouping section sometimes. Try something like;

    Select City & '_' & State as Demograph, Sum(Product_Cost) as Total, SaleDate from tblProduct Group by City & '_' & State , SaleDate Having count (City & '_' & State) >5
    Thanks for the response...It's throwing an error that the data types varchar and varchar are incompatiable in the '&' operator

  7. #7
    Minty is online now VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Can you post up your database (zipped) with some sample data - enough to get a query result ?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. concatenate text column by grouping?
    By aero in forum Queries
    Replies: 2
    Last Post: 07-27-2016, 03:11 PM
  2. Grouping, then counting groups.
    By RedWolf3x in forum Queries
    Replies: 6
    Last Post: 05-31-2012, 09:52 AM
  3. Replies: 3
    Last Post: 09-01-2011, 11:07 PM
  4. grouping and counting
    By boutwater in forum Queries
    Replies: 3
    Last Post: 06-23-2011, 03:41 PM
  5. Replies: 4
    Last Post: 04-09-2011, 10:39 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