Skip to Content
author's profile photo Former Member
Former Member


I dont seem to have the correct syntax for getting subtotals in SQL. Does anyone have a sample?


Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on Nov 30, 2005 at 09:05 PM

    Hi Jacquie,

    If you want to return subtotals when executing a select statement then you need to use either the WITH CUBE or WITH ROLLUP clauses. The following example will give subtotals for sales invoices grouped by customer code and item group code:

    select case

    when (grouping(a.CardCode) = 1) then'ALL'

    else isnull(a.CardCode, 'UNKNOWN')

    end as CardCode,


    sum(b.LineTotal) as SalesTotal

    from OINV a inner join INV1 b on a.DocEntry = b.DocEntry inner join OITM c on b.ItemCode = c.ItemCode

    group by a.CardCode, c.ItmsGrpCod with CUBE

    Hope this helps,


    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.