Solved

create grand total row

Posted on 2012-04-02
10
472 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 74

Expert Comment

by:sdstuber
ID: 37797243
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
ID: 37797253
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 74

Expert Comment

by:sdstuber
ID: 37797311
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

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

thank you  a bunch.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37797327
see my previous post  http:#a37797311
0
 

Author Comment

by:metropia
ID: 37797332
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 74

Expert Comment

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

Author Comment

by:metropia
ID: 37797371
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 74

Accepted Solution

by:
sdstuber earned 400 total points
ID: 37797378
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
ID: 37811883
Thank you guys for your help.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 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