+ Reply to Thread
Results 1 to 5 of 5

How to group by and SUM in each line

  1. #1
    Registered User
    Join Date
    07-27-2021
    Location
    NL
    MS-Off Ver
    2013
    Posts
    8

    How to group by and SUM in each line

    Hello Sirs!

    It will be a bit difficult to explain. Please have a look at attached file for better understanding.

    What I want to achieve is to have results from Table1 summarized in each row. Summary should be between given dates From Table2. Please note that db has dates in YYYYMM format.

    I imagine results like this:

    1month |2country |SumOf3sales
    202101 |CZ |3 (no change as 202012 is not in db)
    202101 |PL |2 (no change as 202012 is not in db)
    202102 |CZ |8 (5+3)
    202102 |PL |6 (4+2)
    202103 |CZ |12 (7+5)
    202103 |PL |10 (6+4)
    202104 |CZ |16 (9 from 202104CZ+7 from 202103CZ)
    202104 |PL |14 (8 from 202104PL+6 from 202103PL)

    Hopefully you will be able to understand what I'm trying to make
    Attached Files Attached Files

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How to group by and SUM in each line

    The main problem I see here is that your categories overlap in Table2 the start month and end month are the same between rows, so odds are this will go wrong since the same 1month applies to 2 rows of Table1, thus double-counting that value. The current query is causing issues too because you are joining the tables, which results in limiting results to where start month actually equals 1month, thus not looking at the whole range of dates.
    I would:
    1. Remove the join
    2. make the 1month criteria: "Between [Table2].[start month] And [Table2].[end month]"

    But I don't know if you have the luxury of adjusting your Table2 contents. As it stands now, I am not certain that it is possible to get the output you want

    I'd also like to understand the expected results... 202102 CZ to me wouldn't show 5+3 but rather just 5
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    07-27-2021
    Location
    NL
    MS-Off Ver
    2013
    Posts
    8

    Re: How to group by and SUM in each line

    Thanks for the advice! I also think I won't be able to do this in one table.
    Currently I'm getting expected result for single line only, using "max" on 1month criteria. I guess I will have to make a loop macro and apend results in another table.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2501 (Windows 11 24H2 64-bit)
    Posts
    88,918

    Re: How to group by and SUM in each line

    Is this an Access question?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    03-19-2022
    Location
    TBD
    MS-Off Ver
    O365
    Posts
    46

    Re: How to group by and SUM in each line

    Hi Marcinrd,

    You are almost there.
    From Table1 Query, please edit it as below steps:

    1. Remove the join (still keep Table1 and Table2 but no need to link between them).
    2. Criteria of [1month] (last field) , please change to -> Between Table2.[start month] And Table2.[end month] .
    3. First field , 1month|Table1|Group By, please change to start month|Table2|Group By .

    Then you should get the result as expected, except for 202104 because in data in Table2 is currently 202104 and 202104 (not 202103).

    Note that using 'Between' will slow down the query if data are very huge.
    Last edited by chocolate_kitten; 03-19-2022 at 10:02 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 05-29-2021, 03:04 AM
  2. [SOLVED] Group cells on the same line
    By AlexFern in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-16-2020, 02:55 PM
  3. [SOLVED] need the first line of each cell color line group
    By Diogo2 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-29-2018, 10:21 AM
  4. Button to Insert new Line with Group Box.
    By jp16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2017, 01:37 AM
  5. Adding line numbers within a group
    By OcoBri in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-05-2013, 12:03 PM
  6. Isolating last line from a group. VBA?
    By jartzh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2008, 08:24 AM
  7. Converting a group of lines into one line without merging
    By THEFALLGUY in forum Excel General
    Replies: 2
    Last Post: 08-24-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1