Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Group Totals Expand / Collapse
Author
Message
Posted Tuesday, June 30, 2009 1:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 29, 2010 1:33 PM
Points: 20, Visits: 49
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.
Post #744803
Posted Tuesday, June 30, 2009 1:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:41 PM
Points: 10,613, Visits: 11,952
Sounds like you could use Rollup or Cube see this article in BOL.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #744810
Posted Tuesday, June 30, 2009 1:30 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 1:45 PM
Points: 15,442, Visits: 9,572
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
Post #744813
Posted Tuesday, June 30, 2009 1:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:41 PM
Points: 10,613, Visits: 11,952
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #744818
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse