Results 1 to 12 of 12
  1. #1
    Ira is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21

    Cool help with Top 20 SKUS

    Hi Guys,



    Working on a query to give me the Top 20 SKUs.

    I got this to work: ProductCode is SKU

    SELECT TOP 20 Count(OrderDetails.ProductCode) AS CountOfProductCode, OrderDetails.ProductCode, OrderDetails.Packaging
    FROM Orders LEFT JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
    WHERE (((Orders.ShipDate) Between [Forms]![dlgDate]![StartDate] And [Forms]![dlgDate]![EndDate]) AND ((Orders.Rep)="Amazon"))
    GROUP BY OrderDetails.ProductCode, OrderDetails.Packaging
    ORDER BY Count(OrderDetails.ProductCode) DESC;

    But I need to add one more field to get the correct count. That is Quantity. I need apply the quality to Number of ProductCode (SKUs). Not sure how to account for customers buying 2 and 3 or more of the same SKU.

    Thank you,

    Ira

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    make Q1 to count the SKU's,
    then Q2 will use Q1 to get the top 20

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Is that quantity or quality?

    assuming the former why can’t you just sum it?

  4. #4
    Ira is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21
    Sorry, Quantity. I tired this, but getting errors.

    SELECT TOP 20 (Count(OrderDetails.ProductCode)*OrderDetails.QTY) AS CountOfProductCode, OrderDetails.ProductCode, OrderDetails.Packaging, OrderDetails.QTY
    FROM Orders LEFT JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
    WHERE (((Orders.ShipDate) Between [Forms]![dlgDate]![StartDate] And [Forms]![dlgDate]![EndDate]) AND ((Orders.Rep)="Amazon"))
    GROUP BY OrderDetails.ProductCode, OrderDetails.Packaging
    ORDER BY (Count(OrderDetails.ProductCode)*OrderDetails.QTY) DESC;

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

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    what sort of errors?

    I can see your query doesn't make sense - why try to count the product (one value) and multiply by qty (multiple values)?

    e,g, say your data was

    Product Qty
    1.........10
    1..........5
    1..........20
    2..........7
    2..........9

    so count of product would be

    1....3
    2....2

    which you then want to multiply by qty so you get

    1.....30
    1......15
    1......60
    2......14
    2......18

    your query also needs to group by qty which you are not doing
    why not just

    SELECT TOP 20 Count(OrderDetails.ProductCode) as codeCount, sum(OrderDetails.QTY) as ttlqty, ........

  7. #7
    Ira is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21
    Okay, so maybe I am not making myself clear

    I have orders with details like:


    ProductCode QTY

    AAA 10
    BBB 2
    CCC 1
    CCC 1
    CCC 1
    CCC 1
    BBB 2
    BBB 2

    Write now BBB and CCC comes up as the TOP 2, but In reality AAA is the TOP1. How do I take into account the QTY?

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    so you want top based on sum orders, not count of orders? in which case you need to order by the sum, before the count

  9. #9
    Ira is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    21
    Still can't get it.

    Tried this, but I keep getting prompted for SumOfProductCode

    \SELECT TOP 20 Sum(OrderDetails.QTY) AS SumOfProductCode, OrderDetails.ProductCode, OrderDetails.PackagingFROM Orders LEFT JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
    WHERE (((Orders.ShipDate) Between #8/29/2022# And #9/1/2022#) AND ((Orders.Rep)="Amazon"))
    GROUP BY SumOfProductCode, OrderDetails.ProductCode, OrderDetails.Packaging
    ORDER BY SumOfProductCode DESC;

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    suggest use the query builder to build your query so you can see how it is constructed

  11. #11
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Normally T-SQL first executes the FROM clause, then the WHERE, then the GROUP BY, then the SELECT and last the ORDR BY (could be slightly different in the Access dialect of SQL). The named field SumOfProductCode is created in the SELECT statement, so doesn't exist yet when the GROUP BY is executed. Hence the error message.
    So leave the
    SumOfProductCode field out of the GROUP by expression. You can use it in the ORDER BY clause.

  12. #12
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    A google search didn't provide me with specific info about the order of ms access query operations but I did find this vid which also contains a link to this article either of which might provide a tool that answers that puzzle if no one chimes in with the details. In any event, I imagine the feature could be quite useful to people who don't know about it. I decided not to make use of it as I'm no longer involved in mining large db's.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 9
    Last Post: 01-06-2022, 08:28 AM
  2. Update query to not include skus NOT in target
    By OldenMcdonald in forum Import/Export Data
    Replies: 6
    Last Post: 05-25-2017, 10:41 PM
  3. Replies: 4
    Last Post: 09-20-2013, 03:20 PM

Tags for this Thread

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