Rollup Query Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Py

    Rollup Query Help


    I found this great rollup query example that uses grouping and sub
    totals. Would it be possible to expand on this and include a group
    within a group and subtotal each group? For example, parent product then
    child product? Help appreciated. Thanks.
    Frank

    SQL:
    SELECT
    CASE
    WHEN (Grouping(Categ oryName)=1) THEN 'MainTotal'
    ELSE CategoryName
    END AS CategoryName,

    CASE
    WHEN (Grouping(Produ ctName)=1) THEN 'SubTotal'
    ELSE Productname
    END AS ProductName,

    Sum(UnitPrice) as UnitPrice,
    Sum(UnitsinStoc k) as UnitsInStock

    FROM Products
    INNER JOIN Categories On
    Products.Catego ryID = Categories.Cate goryID
    GROUP BY CategoryName, ProductName WITH ROLLUP




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • David Portas

    #2
    Re: Rollup Query Help

    CUBE/ROLLUP will subtotal by whatever columns you specify, so presumably you
    need to change the GROUP BY to something like:

    ....
    GROUP BY categoryname, subcategoryname , productname WITH ROLLUP

    If this doesn't answer your question please post DDL for this table, post
    INSERT statements to generate a few rows of sample data and show your
    required result.

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --


    Comment

    Working...