Solved

add a grand total column in query

Posted on 2014-09-15
6
177 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 45

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 27

Expert Comment

by:yodercm
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now