Solved

create grand total row

Posted on 2012-04-02
10
467 Views
Last Modified: 2012-04-05
I have a query that gives me the results I need, there is a column named Scrap Amount

I would like to learn how to create a row that displays the total sum of every row returned.


my query:

SELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount'
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description
ORDER BY Mill_Number, Scrap_Code_Description;

Open in new window

0
Comment
Question by:metropia
  • 5
  • 4
10 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
if sql2005 or lower....

S ELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount'
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description WITH ROLLUP
ORDER BY Mill_Number, Scrap_Code_Description;

if sql2008 or higher

S ELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount'
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY ROLLUP(MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description)
ORDER BY Mill_Number, Scrap_Code_Description;

the rollup will create summations for each column as well as overall total
0
 
LVL 13

Assisted Solution

by:Ashok
Ashok earned 100 total points
Comment Utility
SELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount'
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description
with rollup
ORDER BY Mill_Number, Scrap_Code_Description;

See http://www.sqlhacks.com/index.php/Summary/GrandTotals

HTH
Ashok
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
note,  both the ROLLUP and WITH ROLLUP will create sub-totals for you as well.
(side note,  WITH ROLLUP is supported in sql2008 but is not recommended practice, use ROLLUP instead)

if you want to exclude those then nest your query and filter them out

select mill_type,mill_number,week_ending,scrap_code_description,scrap_amount from
(SELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount',
grouping(mill_type) grpa, grouping(mill_number) grpb, grouping(CONVERT(VARCHAR(10),week_ending,101)) grpc, grouping (scrap_code_description) grpd
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY ROLLUP(MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description)
) x
where grpa+grpb+grpc+grpd in (0,4)
ORDER BY Mill_Number, Scrap_Code_Description
0
 

Author Comment

by:metropia
Comment Utility
how could I have the grand total just show for all the rows and not for each group?

thank you  a bunch.
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
see my previous post  http:#a37797311
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:metropia
Comment Utility
i am getting totals, but it is just hard to read. i am attaching a copy of my sample data returned by your queries.
sampledata.txt
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
which query are you using? and for which version of sql server?
0
 

Author Comment

by:metropia
Comment Utility
first I used:


if sql2005 or lower....

SELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount'
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description WITH ROLLUP
ORDER BY Mill_Number, Scrap_Code_Description;


Then the one on post: 37797311

But I got message: "'ROLLUP' is not a recognized function name."

I am using MS SQL Server 2000
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 400 total points
Comment Utility
37797311  was just an example  you have to do the same logic with the WITH ROLLUP


select mill_type,mill_number,week_ending,scrap_code_description,scrap_amount from
(SELECT Mill_Type, Mill_Number, CONVERT(VARCHAR(10),week_ending,101) 'Week_Ending', Scrap_Code_Description,  REPLACE(CONVERT(varchar(20), (CAST(SUM(Scrap_Amount) AS money)), 1), '.00', '') 'Scrap_Amount',
grouping(mill_type) grpa, grouping(mill_number) grpb, grouping(CONVERT(VARCHAR(10),week_ending,101)) grpc, grouping (scrap_code_description) grpd
 FROM RG_MILL_SCRAP_BY_MONTH_VW
 WHERE Transaction_Date >= {ts '2012-03-19 00:00:00'} AND Transaction_Date < {ts '2012-03-26 00:00:00'}
GROUP BY MILL_TYPE, MILL_NUMBER, CONVERT(VARCHAR(10),WEEK_ENDING,101), Scrap_Code_Description WITH ROLLUP
) x
where grpa+grpb+grpc+grpd in (0,4)
ORDER BY Mill_Number, Scrap_Code_Description
0
 

Author Closing Comment

by:metropia
Comment Utility
Thank you guys for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now