Results 1 to 13 of 13
  1. #1
    EricRex is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    May 2024
    Location
    Colorado
    Posts
    85

    Query to Get Percentage of subcategory by Weekday

    Hey there,

    This is complex, so I'll get there in steps:

    This query:
    Code:
    SELECT St1.cWeekday, St1.EODAction, Count(St1.EODAction) AS NumActions
    FROM StockTrades as St1
    WHERE St1.EODAction<>""
    GROUP BY St1.cWeekday, St1.EODAction
    ORDER BY SWITCH(St1.cWeekday = 'Mon', 1,
                    St1.cWeekday = 'Tue', 2,
                    St1.cWeekday = 'Wed', 3,
                    St1.cWeekday = 'Thu', 4,
                    St1.cWeekday = 'Fri', 5), Count(St1.EODAction) DESC, St1.EODAction;
    Generates a table of results with Weekday, category name, and count as you can see in attached screenshot.
    Click image for larger version. 

Name:	EODActionByWeekday.jpg 
Views:	30 
Size:	30.3 KB 
ID:	52075

    My goal is to get the percentage of each category by weekday. So, if the Drop category has a count of 8 and Monday has a total of 32 EOD action counts, I should see an output of 25%. And each category would have its corresponding percentage.

    In trying to get to the percentage, I created a query that does not work to simply output the total for each day in a separate column:
    Code:
    SELECT St1.cWeekday, St1.EODAction, Count(St1.EODAction) AS NumActions, (SELECT Count(St2.EODAction) FROM StockTrades WHERE St1.cWeekday = St2.cWeekday GROUP BY St2.cWeekday) as Total
    FROM StockTrades as St1, StockTrades as St2
    WHERE St1.EODAction<>"" AND St2.EODAction <> ""
    GROUP BY St1.cWeekday, St1.EODAction
    ORDER BY SWITCH(St1.cWeekday = 'Mon', 1,
                    St1.cWeekday = 'Tue', 2,
                    St1.cWeekday = 'Wed', 3,
                    St1.cWeekday = 'Thu', 4,
                    St1.cWeekday = 'Fri', 5), Count(St1.EODAction) DESC, St1.EODAction;
    This query generates the error, Your query does not include the specified expression cWeekday as part of an aggregate function.

    Appreciate any help on this.
    Cheers,


    Eric

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Can you provide a test database with data values matching your sample?
    We really need the table and any queries you are dealing with.

  3. #3
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    If all you want is Percent of Parent, this is stupid easy in Excel/DAX.

    Total Actions:=SUM(Table1[NumActions])

    Share of Day:=DIVIDE ( [Total Actions],
    CALCULATE( [Total Actions], ALL('Table1'[EODAction]))
    )

    Or you could create a query that groups by Category
    SELECT Category, SUM([Actions]) FROM <table> GROUP BY Category;

    and then you would join that to the total (and leave out one or more grouping columns) then you'd join those two and divide.
    Last edited by madpiet; 08-03-2024 at 09:14 AM.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I see only 25 for Mon ?
    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

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    Just do another aggregate query based on your first query but excluding categories, then join it to your first query on date.

  6. #6
    EricRex is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    May 2024
    Location
    Colorado
    Posts
    85
    Quote Originally Posted by CJ_London View Post
    Just do another aggregate query based on your first query but excluding categories, then join it to your first query on date.
    As a SQL n00B, I don't know how to do that.

  7. #7
    EricRex is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    May 2024
    Location
    Colorado
    Posts
    85
    Quote Originally Posted by orange View Post
    Can you provide a test database with data values matching your sample?
    We really need the table and any queries you are dealing with.
    I tried to upload a sample dB, but it doesn't appear in the attachments window. Verified that the extension is .accdb, but no go.

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    See how to attach files at the top of the page.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Quote Originally Posted by EricRex View Post
    As a SQL n00B, I don't know how to do that.
    There are fancier ways of doing it, but since you're new, I'd go the easy route. So you break the question into two steps.
    1. Get the total number of actions per Weekday, like this:

    Code:
    SELECT xlsData.cWeekday, Sum(xlsData.NumActions) AS [Total Actions]
    FROM xlsData
    GROUP BY xlsData.cWeekday;
    2. Save that query ... (mine's called qtotWeekdayTotals)

    3. Create a new query, add both the original data table and the qtotWeekdayTotals to the grid. Join on cWeekday column. Then just divide [NumActions] by [Total Actions], and finally, go into the properties of the new column and set the format to percentage

    Code:
    SELECT qtotWeekDayTotals.cWeekday, xlsData.EODAction, xlsData.NumActions, [NumActions]/[Total Actions] AS Share
    FROM qtotWeekDayTotals INNER JOIN xlsData ON qtotWeekDayTotals.cWeekday = xlsData.cWeekday;

  10. #10
    EricRex is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    May 2024
    Location
    Colorado
    Posts
    85
    Quote Originally Posted by madpiet View Post
    There are fancier ways of doing it, but since you're new, I'd go the easy route. So you break the question into two steps.
    1. Get the total number of actions per Weekday, like this:

    Code:
    SELECT xlsData.cWeekday, Sum(xlsData.NumActions) AS [Total Actions]
    FROM xlsData
    GROUP BY xlsData.cWeekday;
    2. Save that query ... (mine's called qtotWeekdayTotals)

    3. Create a new query, add both the original data table and the qtotWeekdayTotals to the grid. Join on cWeekday column. Then just divide [NumActions] by [Total Actions], and finally, go into the properties of the new column and set the format to percentage

    Code:
    SELECT qtotWeekDayTotals.cWeekday, xlsData.EODAction, xlsData.NumActions, [NumActions]/[Total Actions] AS Share
    FROM qtotWeekDayTotals INNER JOIN xlsData ON qtotWeekDayTotals.cWeekday = xlsData.cWeekday;
    Thanks for the starter code! For completeness, here are the working versions:

    qryEODActionTotalsByWeekday:
    Code:
    SELECT cWeekday, Count(EODAction) AS TotalActions
    FROM StockTrades
    GROUP BY cWeekday
    ORDER BY SWITCH(cWeekday = 'Mon', 1,
                    cWeekday = 'Tue', 2,
                    cWeekday = 'Wed', 3,
                    cWeekday = 'Thu', 4,
                    cWeekday = 'Fri', 5);
    And qryEODActionTypesByWeekday:
    Code:
    SELECT [qryEODActionTotalsByWeekday].cWeekday, StockTrades.EODAction, Count(StockTrades.EODAction) AS ActionCount, Round(Count(StockTrades.EODAction)/[qryEODActionTotalsByWeekday].TotalActions*100,1) AS PctofTotal
    FROM qryEODActionTotalsByWeekday INNER JOIN StockTrades ON [qryEODActionTotalsByWeekday].cWeekday=StockTrades.cWeekday
    WHERE StockTrades.EODAction <>""
    GROUP BY [qryEODActionTotalsByWeekday].cWeekday, StockTrades.EODAction, [qryEODActionTotalsByWeekday].TotalActions
    ORDER BY SWITCH([qryEODActionTotalsByWeekday].cWeekday = 'Mon', 1,
                    [qryEODActionTotalsByWeekday].cWeekday = 'Tue', 2,
                    [qryEODActionTotalsByWeekday].cWeekday = 'Wed', 3,
                    [qryEODActionTotalsByWeekday].cWeekday = 'Thu', 4,
                    [qryEODActionTotalsByWeekday].cWeekday = 'Fri', 5), Count(StockTrades.EODAction) DESC , StockTrades.EODAction;
    I'd love to know the fancier version of doing this. Have to admit, while I vaguely understand the JOIN concept, the details escape me.

    Cheers,
    Eric

  11. #11
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  12. #12
    EricRex is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    May 2024
    Location
    Colorado
    Posts
    85
    Quote Originally Posted by orange View Post
    Please provide sample database.
    Attached here:
    SampleDB.accdb

    Note for the Forum Admin: When I first tried to upload this 41MB sample DB, the upload dialog box spun as if processing but didn't show the file in the lower pane as uploaded. No error message, nothing. I then zipped it into a 5MB file and tried to upload which generated the "Exceeds max size" error. I was able to upload by compacting the DB.
    There appears to be a issue with the upload error message when an ACCDB file exceeds the allowable size.

    Cheers,
    Eric

  13. #13
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Eric,

    Well done!

    Re uploading :
    -always do compact/repair to remove garbage/ (deleted tables/queries/forms etc)
    -zip to reduce overall size
    Last edited by orange; 08-04-2024 at 07:24 PM.

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

Similar Threads

  1. Category and SubCategory
    By dgp0099 in forum Access
    Replies: 5
    Last Post: 05-07-2014, 06:30 PM
  2. Business Weekday?
    By HMEpartsmanager in forum Queries
    Replies: 2
    Last Post: 10-01-2012, 01:28 PM
  3. Help with Weekday Function
    By Juan4412 in forum Queries
    Replies: 3
    Last Post: 10-18-2011, 07:14 AM
  4. Weekday Name from a Date
    By Coors in forum Queries
    Replies: 9
    Last Post: 09-26-2011, 03:08 PM
  5. Replies: 0
    Last Post: 08-19-2008, 11:12 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