Group Totals

  • Greetings,

    I am still trying to master SQL. I am now at the point of trying to do totals ie aggregate.

    I am trying to get the subtotals by B.loc and A.whse. Then have a grand total of all records returned.

    So, I would have subtotal by location, sub total by whse and grand total of everything.

    The table whse is:

    whse varchar(5)

    description varchar(20)

    data looks like:

    (KS,KANSAS)

    Table item looks like:

    item varchar(32)

    description varchar(40)

    product_code varchar(5)

    p_m_t_code varcar(1)

    u_m varchar(10)

    unit_cost decimal

    data looks like:

    (FV-WDK01, Winddiverter,FV,M,EA,$45.10)

    Table itemloc looks like:

    whse varchar(5)

    loc varchar(10)

    qty_on_hand decimal

    data looks like:

    (KS,STOCK,523)

    (KS,TRUCK,132)

    here is my code:

    use My_App

    declare

    @StartWarehouse WhseType,

    @EndWarehouse WhseType,

    @StartItem ItemType,

    @EndItem ItemType

    SET @StartWarehouse = IsNull(dbo.ExpandKyByType('WhseType', @StartWarehouse), dbo.lowstring('WhseType'))

    SET @EndWarehouse = IsNull(dbo.ExpandKyByType('WhseType', @EndWarehouse), dbo.highstring('WhseType'))

    SET @StartItem = IsNull(dbo.ExpandKyByType('ItemType', @StartItem), dbo.lowstring('ItemType'))

    SET @EndItem = IsNull(dbo.ExpandKyByType('ItemType', @EndItem), dbo.highstring('ItemType'))

    SET @StartWarehouse = 'AK'

    SET @EndWarehouse = 'VMT'

    Select A.whse,

    B.loc,

    C.item,

    C.description,

    C.product_code,

    C.p_m_t_code,

    C.u_m,

    B.qty_on_hand,

    C.unit_cost,

    sum(B.qty_on_hand * C.unit_cost) tcost

    From whse A

    Inner Join itemloc B on (A.whse = B.whse)

    inner Join item C on (B.item = C.item)

    where B.qty_on_hand <> 0 and A.whse between @StartWarehouse and @EndWarehouse

    GROUP BY

    A.whse,

    B.loc,

    C.item,

    C.description,

    C.product_code,

    C.p_m_t_code,

    C.u_m,

    B.Qty_on_hand,

    C.unit_cost

    I need to total the extended cost by location, by whse and then get a grand total of extended cost.

    Not sure how to do the totals.

    Any help greatly appreciated.

  • Sounds like you could use Rollup or Cube see this article in BOL.

  • That kind of calculation can usually be done much more easily in the front end application or report.

    Aggregates in SQL have to be grouped and broken down by all the columns you are selecting. If you need something else, what you need to do is a sub-query that does the aggregate, and join that to the outer query. Are you familiar with writing sub-queries (either CTEs or "derived tables")?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • As a follow up to my other post, I do agree with GSquared that this stuff is usually easier to do in the application/report. For example SSRS (SQL Reporting Services) would deal with this very easily.

Viewing 4 posts - 1 through 3 (of 3 total)

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