Solved

add a grand total column in query

Posted on 2014-09-15
6
183 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 50

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 Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
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 48

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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