Solved

create grand total row

Posted on 2012-04-02
10
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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