Solved

add a grand total column in query

Posted on 2014-09-15
6
180 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 46

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 51
Amazon Redshift 2 23
myqsl update statement on phpMyAdmin 8 21
MS SQL Inner Join - Multiple Join Parameters 2 17
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

1 Experts available now in Live!

Get 1:1 Help Now