?
Solved

create grand total row

Posted on 2012-04-02
10
Medium Priority
?
475 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

650 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