Solved

create grand total row

Posted on 2012-04-02
10
471 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

791 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