Results 1 to 9 of 9
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to identify highest sales by department for each store?

    I have a table with about 3200 records at anytime of the month. There are three columns I look at, StoreNum, Dept and Sales. I would like to know which Dept that is in the same store had higher sales. Ex. for StoreNum123, Dept_B would win, so I need a field to return 'highest' or 'lowest' per StoreNum. I can do a Totals/Sum but not sure how I could identify the Dept w/the highest sales that is within the same store.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    Order by dept, sales DESC ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,563
    Your Data table in not normalised.

    You should have tables as shown in the attached screenprint:-
    Attached Thumbnails Attached Thumbnails Relationships.PNG  
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  5. #5
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    I tried:


    Code:
    SELECT Data.StoreNum, Data.Dept, Data.SalesFROM Data
     (
               SELECT StoreNum, Dept, Sum(Sales) as TotSales
               FROM Data As T2
               GROUP BY StoreNum, Dept, Sales) AS Test1
    Inner Join Data.StoreNum = Test1.StoreNum;
    and

    Code:
    SELECT Data.StoreNum, Data.DeptFROM Data
    Join (
               SELECT StoreNum, Dept, Sum(Sales) as TotSales
               FROM Data As T2
               GROUP BY StoreNum, Dept, Sales) AS Test1
    ON Data.StoreNum = Test1.StoreNum;
    but get Syntax Error in FROM clause for both... not sure what I'm missing.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    As posted, you have no space between Dept and FROM. That might just be a posting error.
    4 views on your attached file already, so I didn't download - kinda late to the party.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    Didn't see that - that wasn't causing the error though. I'm getting close except the sum as all of sales, I can't find examples (that I understand at least) that would help me split by store and dept.

    Code:
    SELECT *         FROM (SELECT StoreNum, Dept, Sum([Sales]) as TotSales
             FROM Data
             GROUP BY StoreNum, Dept) AS T2 INNER JOIN
    Data as T1
    ON T1.StoreNum = T2.StoreNum;

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Use your Query1 in another query:

    SELECT Query1.*
    FROM Query1 INNER JOIN (SELECT StoreNum, Max(SumOfSales) AS S FROM Query1 GROUP BY StoreNum) AS D
    ON Query1.StoreNum = D.StoreNum AND Query1.SumOfSales = D.S;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    I wasn't even close - thanks June7.

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

Similar Threads

  1. Replies: 17
    Last Post: 04-02-2020, 09:53 AM
  2. Replies: 1
    Last Post: 03-07-2019, 11:58 AM
  3. Replies: 16
    Last Post: 12-28-2017, 02:04 PM
  4. Replies: 26
    Last Post: 11-02-2016, 10:38 AM
  5. Replies: 0
    Last Post: 12-13-2012, 03: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