Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Group Totals
Group Totals
Rate Topic
Display Mode
Topic Options
Author
Message
darryl-715744
darryl-715744
Posted Tuesday, June 30, 2009 1:14 PM
Grasshopper
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
Jack Corbett
Jack Corbett
Posted Tuesday, June 30, 2009 1:28 PM
SSChampion
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
GSquared
GSquared
Posted Tuesday, June 30, 2009 1:30 PM
SSCoach
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
Jack Corbett
Jack Corbett
Posted Tuesday, June 30, 2009 1:33 PM
SSChampion
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.