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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

SQL Query - SUM Two Columns in One Row

Hello,

I have a question with MS SQL Query I am trying to write.

The table fields I am using are (there will be a few more e.g. matching CompanyID to another table for the CompanyName and EXTMins will also be used so I would add mins and hours together but hopefully this request can be written or some guidance I should be able to write the rest)
CostCode (nchar)
ExtHours (Int)
CompanyID (Int)

The table is called Timesheets.

The user can only enter RTI or NRT for Cost Code, ExtHour can be any integer and the CompanyID is matched to another table which for the moment is not important.

I want a query to
Total up the total of ExtHours for NRT and RTI and group by company. I want these answers in just one row for each company.

e.g. Company ID, Total for NRT, Total for RTI.

Now not all companies will have RTI but if there is no data I want it to be 0.

I have tried something very simple which is below.

SELECT        costcode, SUM(exthours) AS Expr1, CompanyId
FROM           Timesheets
GROUP BY costcode, CompanyId

Open in new window


This works to a limit but it does not group it how I want it.
I was thinking of a query which is like this

SELECT       costcode,( SELECT SUM(exthours) from Timesheets where costcode = 'RTI') as RTITotal, (SELECT SUM(extHours) from Timesheets where costCode = 'NRT') AS NRTTOtal, CompanyId
FROM            Timesheets
GROUP CompanyId

Open in new window


But that doesn't work, so any suggestions would be very useful.

Thanks,
Alex
0
BoltonWanderer
Asked:
BoltonWanderer
1 Solution
 
Simone BCommented:
Try this:

SELECT       CompanyId,
CASE WHEN costcode = 'RTI' THEN SUM(exthours) END AS RTITotal,
CASE WHEN costcode = 'NRT' THEN SUM(exthours) END AS NRTTotal
 
FROM            Timesheets
GROUP BY CompanyId

Open in new window

0
 
Guru JiCommented:
Try this

SELECT        costcode, CASE WHEN costcode = 'RTI' OR costcode = 'NRT' THEN SUM(exthours) ELSE 0 END as TotalSum , CompanyId
FROM           Timesheets
GROUP BY CompanyId, costcode

Open in new window


If you don't want costcode then

SELECT         CompanyId, CASE WHEN costcode = 'RTI' OR costcode = 'NRT' THEN SUM(exthours) ELSE 0 END as TotalSum 
FROM           Timesheets
GROUP BY CompanyId

Open in new window

0
 
Simone BCommented:
Sorry, I forgot about this - "Now not all companies will have RTI but if there is no data I want it to be 0."

SELECT       CompanyId,
CASE WHEN costcode = 'RTI' THEN SUM(ISNULL(exthours,0)) END AS RTITotal,
CASE WHEN costcode = 'NRT' THEN SUM(ISNULL(exthours,0)) END AS NRTTotal
 
FROM            Timesheets
GROUP BY CompanyId 

Open in new window

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!

 
BoltonWandererAuthor Commented:
@Buttercup1 that is what I want I believe however I get a message saying

Executed SQL statement: SELECT CompanyId, CASE WHEN costcode = 'RTI' THEN SUM(exthours) END AS RTITotal, CASE WHEN costcode = 'NRT' THEN SUM(exthours) END AS NRTTotal FROM dbo.Timesheets GROUP BY CompanyId
Error Source: .Net SqlClient Data Provider
Error Message: Column 'dbo.Timesheets.costcode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Sorry @write2mohit that just groups the total and on different rows or it puts it on one row but its either this or that. what I would like to archieve is to have CompanyID, TotalExtHours which is NRT, TotalExtHours which is RTI on a per row basis.

Thanks,
0
 
Simone BCommented:
Oops, sorry. The problem with adding costcode to the GROUP BY clause is that you're back to 2 lines for each CompanyID again. You can use CTE, or use a subquery as below. This seems repetitive, but should give you what you need:

SELECT X.CompanyID, SUM(X.RTITotal) RTITotal, SUM(X.NRTTotal) NRTTotal
FROM (

SELECT       CompanyId,
CASE WHEN costcode = 'RTI' THEN SUM(ISNULL(exthours,0)) END AS RTITotal,
CASE WHEN costcode = 'NRT' THEN SUM(ISNULL(exthours,0)) END AS NRTTotal
 
FROM            Timesheets
GROUP BY CompanyId, costcode
) X
GROUP BY CompanyID

Open in new window

0
 
BoltonWandererAuthor Commented:
@Buttercup1 Thank You. Just what I needed :)
0
 
Simone BCommented:
You're welcome!
0
 
Simone BCommented:
Can you assign points?
tx
0
 
BoltonWandererAuthor Commented:
Just what I wanted. Thanks.
0
 
PortletPaulCommented:
btw:

SELECT
      CompanyId
    , SUM(CASE WHEN costcode = 'RTI' THEN exthours ELSE 0 END) AS RTITotal
    , SUM(CASE WHEN costcode = 'NRT' THEN exthours ELSE 0 END) AS NRTTotal
FROM Timesheets
GROUP BY CompanyId

-- no need for an inner subquery, it's where you put the SUM( ... ) that matters
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now