Solved

Grand total on 2 columns

Posted on 2015-02-09
20
227 Views
Last Modified: 2015-02-11
I want to get a grand total below each column when the results return - is that possible?
                SELECT Year(offlocationdatetime)                                               AS Yr, 
                Month(offlocationdatetime)                                              AS Mo,                 
                Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2014-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
                GROUP  BY Year(offlocationdatetime), 
                Month(offlocationdatetime),  
                Job.company 
                HAVING (( ( Job.company ) = 'JB Hunt' )) 
                ORDER  BY Year(offlocationdatetime) DESC, 
                Month(offlocationdatetime) DESC; 

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
  • 8
  • 8
  • 4
20 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 40599471
Usually putting summary row logic in your application is a better idea.  But ... sometimes you're constrained.

So you UNION in a summary row.  Except that we end up losing your ORDER BY in the process.

                SELECT Year(offlocationdatetime)                                               AS Yr, 
                Month(offlocationdatetime)                                              AS Mo,                 
                Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2014-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
                GROUP  BY Year(offlocationdatetime), 
                Month(offlocationdatetime),  
                Job.company 
                HAVING (( ( Job.company ) = 'JB Hunt' )) 
/* your query above */
UNION
 Select '' as Yr, '' as Mo, 
Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2014-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
               and Job.company = 'JB Hunt'

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40599652
Awesome!
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40599842
:( not awesome from a performance perspective (no offence to Daniel, as he states it could be a constraint)

2 tips.

1. DO NOT use UNION

instead use UNION ALL. When used by itself UNION attempts to verify that every row is unique, and this is just redundant effort when you need a grand total (i.e. union ALL is faster than union).

2. Use a Common Table Expression (CTE)

Here this can help avoid running the full query twice.
;WITH CTE
AS (

      SELECT
            YEAR(offlocationdatetime) AS YR
          , MONTH(offlocationdatetime) AS MO
          , SUM(sfrentaldays + wfrentaldays) AS RENTALDAYS
          , COUNT(JDE.RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) AS 'Failure'
          , CASE
                  WHEN (SUM(sfrentaldays + wfrentaldays) / COUNT(RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) * 24) = NULL THEN 0
                  ELSE (SUM(sfrentaldays + wfrentaldays) / COUNT(RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) * 24)
            END AS 'Hours'
      FROM Job
            JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid
            JOIN JobDownholeEquipment AS JDE ON Job.jobid = JDE.jobid
            JOIN JobUpholeEquipment AS JUE ON Job.jobid = JUE.jobid
      WHERE (((Job.offlocationdatetime) > '2014-01-01 12:00:00 AM'
                        AND (Job.offlocationdatetime) < '2014-12-31 11:59:59 PM'))
      GROUP BY
            YEAR(offlocationdatetime)
          , MONTH(offlocationdatetime)
          , Job.company
      HAVING (((Job.company) = 'JB Hunt'))
)
SELECT
      YR, MO, RENTALDAYS, 'Failure', 'Hours'
FROM CTE

UNION ALL

      SELECT
            '' AS YR
          , '' AS MO
          , SUM(sfrentaldays + wfrentaldays) AS RENTALDAYS
          , COUNT(JDE.RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) AS 'Failure'
          , CASE
                  WHEN (SUM(sfrentaldays + wfrentaldays) / COUNT(RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) * 24) = NULL THEN 0
                  ELSE (SUM(sfrentaldays + wfrentaldays) / COUNT(RigFailureTime + JDE.ToolFailureTime + JUE.FailureTime) * 24)
            END AS 'Hours'
      FROM CTE
;

Open in new window


You should also acquaint yourself with "rollup"
e.g. https://msdn.microsoft.com/en-AU/library/ms177673.aspx
It is possible these days to have SQL produce the grand totals without resorting to a second query
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 49

Expert Comment

by:PortletPaul
ID: 40599851
apologies, your topics include MS SQL Server, and my comments about the CTE and rollup do not apply to MySQL
(not yet anyway, maybe one day)

for MySQL you should still use UNION ALL
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40599903
Thanks for the tip on UNION ALL, Paul. I didn't realize the performance difference.

I hate using a UNION of any sort ... but find that certain reporting situations go a lot better if the SQL does all the work.  Therefore ... a UNION.  But I'll remember the UNION ALL for the future.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40599906
Yes, do keep UNION ALL in mind, it can make a huge difference (not saying it does here, but it can)
0
 
LVL 6

Author Comment

by:r3nder
ID: 40600705
I made the change to UNION ALL  and Changed "Select '' as Yr, '' as Mo," to "Select CAST(' AS CHAR) as Yr, CAST('' AS CHAR) as Mo, " so I could export it to CSV the only issue is the hours column doesn't sum correct

Mean Time Between Failure By Customer: JB Hunt				
Created On: 	2/10/2015 8:26			
From 	1/1/2012 0:00			
To 	12/31/2015 23:59			
Yr	Mo	RentalDays	Failure	Hours
2014	5	17	2	204
2014	8	37	5	177.6
2014	9	5	1	120
	        59	 8	177

Open in new window

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40600752
I don't know why the hours wouldn't sum correctly.

Would you post the query as you currently have it?
0
 
LVL 6

Author Comment

by:r3nder
ID: 40600757
                SELECT Year(offlocationdatetime)                                               AS Yr, 
                Month(offlocationdatetime)                                              AS Mo,                 
                Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
                GROUP  BY Year(offlocationdatetime), 
                Month(offlocationdatetime),  
                Job.company 
                HAVING (( ( Job.company ) = 'JB Hunt' )) 

UNION ALL
 Select CAST('' AS CHAR) as Yr, CAST('' AS CHAR) as Mo, 
Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
               and Job.company = 'JB Hunt'

Open in new window

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40600840
Ok, let's try a SUM() around that CASE statement.

                SELECT Year(offlocationdatetime)                                               AS Yr, 
                Month(offlocationdatetime)                                              AS Mo,                 
                Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
                GROUP  BY Year(offlocationdatetime), 
                Month(offlocationdatetime),  
                Job.company 
                HAVING (( ( Job.company ) = 'JB Hunt' )) 

UNION ALL
 Select CAST('' AS CHAR) as Yr, CAST('' AS CHAR) as Mo, 
Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                SUM(CASE WHEN (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) = null THEN 0 ELSE (Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24) END) AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
               and Job.company = 'JB Hunt'

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 40600908
Invalid use of group function
0
 
LVL 6

Author Comment

by:r3nder
ID: 40600935
Changed the query from case to IFNULL - do you think that is better?
                SELECT Year(offlocationdatetime)                                               AS Yr, 
                Month(offlocationdatetime)                                              AS Mo,                 
                Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                IFNULL((Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24),0) AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
                GROUP  BY Year(offlocationdatetime), 
                Month(offlocationdatetime),  
                Job.company 
                HAVING (( ( Job.company ) = 'JB Hunt' )) 

UNION ALL
 Select CAST('' AS CHAR) as Yr, CAST('' AS CHAR) as Mo, 
Sum(sfrentaldays + wfrentaldays)                                       AS RentalDays,
                Count(jde.RigFailureTime + jde.ToolFailureTime + jue.FailureTime)      as 'Failure',  
                IFNULL((Sum(sfrentaldays + wfrentaldays)/Count(RigFailureTime + jde.ToolFailureTime + jue.FailureTime)  * 24),0) AS 'Hours'
                FROM   Job 
                JOIN JobRentalTicket ON Job.Jobid = JobRentalTicket.Jobid 
                JOIN JobDownholeEquipment AS jde ON Job.jobid = jde.jobid
                JOIN JobUpholeEquipment AS jue ON Job.jobid = jue.jobid
                WHERE  (( ( Job.offlocationdatetime ) > '2012-01-01 12:00:00 AM' 
                AND ( Job.offlocationdatetime ) < '2014-12-31 11:59:59 PM' ))  
               and Job.company = 'JB Hunt'
               

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40602085
It looks OK to me, but I have no way of testing this
0
 
LVL 6

Author Comment

by:r3nder
ID: 40602409
still doesn't sum correctly
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40602433
please look back to ID: 40600705 (I placed output into code block)

are you getting a total of 177 hours? and not the (wanted) sum of the 3 figures above it?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40602455
177 is the correct "total" because it is a row calculation and NOT a column summation.
that column is "Hours_per_Failure" (or something similar to that)  so it can go up and down

Mean Time Between Failure By Customer: JB Hunt                
Created On:     2/10/2015 8:26            
From     1/1/2012 0:00            
To     12/31/2015 23:59    
        
Yr     Mo    RentalDays    Failure  Hours_per_Failure
-----------------------------------------------------
2014    5    17            2        204
2014    8    37            5        177.6
2014    9    5             1        120
----------------------------------------------------------------
             59            8        177 --<< (59 / 8) * 24 = 177 

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 40603337
What I wanted was a sum of the 3 you see in that column 204 + 177.6 + 120 = 501.6
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40604685
but what you wanted/expected would not be correct information

177 is the correct answer

-- OR --

the other rows are incorrect and you should not be dividing
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40604693
btw this has become a second question....

you wanted to know HOW to get the grand total

I supplied a tiny tip

now we have a debate on accuracy of calculation....
0
 
LVL 6

Author Comment

by:r3nder
ID: 40604897
your right it is a second question - I have decided I want to average that column - Thank you PorterPaul and Daniel Wilson
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

724 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