Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group Totals


Group Totals

Author
Message
darryl-715744
darryl-715744
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
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.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12784 Visits: 14863
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
GSquared
GSquared
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16683 Visits: 9729
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
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12784 Visits: 14863
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search