Adding new column to GROUP BY vs adding MIN or MAX of that column

  • I have a straightforward report, which performs some calculation, e.g.

    SELECT myCustomerId,
    customerProductId,
    SUM(totalSpentByCustomerForProduct) AS totalPerProductPerCustomer
    FROM customersTable
    JOIN ordersTable ON ...
    JOIN productsTable ON ...
    GROUP BY myCustomerId,
    customerProductId

    Now I need to extend it, providing both customer and  product names.

    Normally I would implement it by adding MIN (customer_name), MIN (product_name) to the rest of the columns in SELECT segment or by adding customer_name, product_name to the  GROUP BY.

    I wonder if there is better approach in general and specifically from 2016 onwards, please?

  • Most likely that's still best.

    Btw, do you know for certain that a given product only occurs once on each order?

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply