Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

add a grand total column in query

Posted on 2014-09-15
6
Medium Priority
?
186 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
6 Comments
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 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
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!

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

972 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