Solved

add a grand total column in query

Posted on 2014-09-15
6
184 Views
Last Modified: 2014-09-17
I am trying to add a grand total of the fields I have totaled - how do I do that

SELECT Job.Jobid, 
                Job.company, 
                Job.wellname, 
                Job.rig, 
                Job.rignumber, 
                Job.onlocationdatetime, 
                Job.offlocationdatetime, 
                sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +  
                rigdowncharge + numberoftools * toolredresscharge +  
                sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +  
                servicemandays * servicemanrate - creditdays * creditrate + othercharges  
                + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate  
                AS  
                'Total' , 
SUM(Total) as 'GrandTotal' <---------this is what I tried but it said no "Total" in the field list
                FROM   Job  
                INNER JOIN JobRentalTicket  
                 ON Job.Jobid = JobRentalTicket.Jobid  
                WHERE   Job.Jobcomplete  = 1   
                AND JobRentalTicket.invictussentinvoicetoagent  = 0  
                  AND Job.offlocationdatetime BETWEEN  
                '2012-01-01 00:00:00' AND '2014-12-31 23:59:59'    
                ORDER BY Job.company

Open in new window

0
Comment
Question by:r3nder
[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
6 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40323353
You need to write all operation again, since 'Total' it's only an alias for the column name when it returns the result.
Also miss the GROUP BY clause.
SELECT Job.Jobid, 
                Job.company, 
                Job.wellname, 
                Job.rig, 
                Job.rignumber, 
                Job.onlocationdatetime, 
                Job.offlocationdatetime, 
                sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +  
                rigdowncharge + numberoftools * toolredresscharge +  
                sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +  
                servicemandays * servicemanrate - creditdays * creditrate + othercharges  
                + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate  
                AS  'Total' , SUM(sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +  
                rigdowncharge + numberoftools * toolredresscharge +  
                sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +  
                servicemandays * servicemanrate - creditdays * creditrate + othercharges  
                + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate) as 'GrandTotal' <---------this is what I tried but it said no "Total" in the field list
                FROM   Job  
                INNER JOIN JobRentalTicket  
                 ON Job.Jobid = JobRentalTicket.Jobid  
                WHERE   Job.Jobcomplete  = 1   
                AND JobRentalTicket.invictussentinvoicetoagent  = 0  
                  AND Job.offlocationdatetime BETWEEN  
                '2012-01-01 00:00:00' AND '2014-12-31 23:59:59'    
ORDER BY Job.company
GROUP BY Job.Jobid, 
                Job.company, 
                Job.wellname, 
                Job.rig, 
                Job.rignumber, 
                Job.onlocationdatetime, 
                Job.offlocationdatetime, 
                sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +  
                rigdowncharge + numberoftools * toolredresscharge +  
                sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +  
                servicemandays * servicemanrate - creditdays * creditrate + othercharges  
                + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate 

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40323362
You can't reference a newly named column in the Select clause - it's not allowed.

You should use a CTE - something like:

with mytable as
(SELECT Job.Jobid,
                Job.company,
                Job.wellname,
                Job.rig,
                Job.rignumber,
                Job.onlocationdatetime,
                Job.offlocationdatetime,
                sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge +  
                rigdowncharge + numberoftools * toolredresscharge +  
                sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate +  
                servicemandays * servicemanrate - creditdays * creditrate + othercharges  
                + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate  
                AS  
                'Total' ,
                FROM   Job  
                INNER JOIN JobRentalTicket  
                 ON Job.Jobid = JobRentalTicket.Jobid  
                WHERE   Job.Jobcomplete  = 1  
                AND JobRentalTicket.invictussentinvoicetoagent  = 0  
                  AND Job.offlocationdatetime BETWEEN  
                '2012-01-01 00:00:00' AND '2014-12-31 23:59:59')
select Jobid, company, wellname, rig, rignumber, onlocationdatetime, offlocationdatetime, total, sum(total) over() as [Grand Total]
from mytable
group by Jobid, company, wellname, rig, rignumber, onlocationdatetime, offlocationdatetime, total
order by company
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40323365
Also, what about is the offlocationdatetime is 2014-12-31 23:59:59.7 - it won't be picked up.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40323379
Once you have fixed all the other problems mentioned here, you can use the

GROUP BY WITH ROLLUP

to get a total of the totals.

http://dev.mysql.com/doc/refman/5.6/en/group-by-modifiers.html
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40323596
Thanks Victor
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40327478
AND Job.offlocationdatetime BETWEEN  
                '2012-01-01 00:00:00' AND '2014-12-31 23:59:59'

This really isn't good practice 23:59:59 is NOT the end of the date 2014-12-31

and from MySQL .6.4 does have time precision finer than second

use this instead:
                  AND Job.offlocationdatetime >= '2012-01-01 00:00:00'
                 AND  Job.offlocationdatetime  < '2015-01-01 00:00:00'
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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