Solved

Sum of multiple columns

Posted on 2014-03-21
3
318 Views
Last Modified: 2014-03-22
I have an old database that looks like this:

Job  Tech1  Hours1   Tech2  Hours2   Tech3  Hours3
A     Bob         1           Sam       2          Joe          3
B     Sam        2           Joe         3          Bob         1
C     Joe          3          Bob        1          Sam

The people in the database can fulfill different rolls on a job so they are put in different columns.

I need a query that figures out the total for each person.  In the above case, this would be:

Tech      Total Hours
Bob              3
Sam             6
Joe               9

I have this in an Access database with multiple queries that perform the calculation, but am looking for a way to simplify into 1 query.

Thanks!
0
Comment
Question by:MrBeedge
  • 2
3 Comments
 
LVL 12

Expert Comment

by:Tony303
ID: 39946798
How about getting the 3 Techs and Hours separated out then brought together in a UNION?

Something like this....

SELECT Tech, SUM(Hours)  as TotalHours
FROM
(
SELECT Tech1 as Tech, Hours1 as Hours
FROM Table
UNION
SELECT Tech2 as Tech, Hours2 as Hours
FROM Table
UNION
SELECT Tech3 as Tech, Hours3 as Hours
FROM Table
) a
GROUP BY Tech

Open in new window

0
 

Author Comment

by:MrBeedge
ID: 39947066
Tony -

Thanks for your reply.  I think some nulls in the hours (Hours1, Hours2, Hours3) are causing some issues.  If I only include the first Hours1 in the query, things total up fine.  When I add the Hours2 and Hours 3 using UNION, the Total Hours actually go down.  There will be some times in this database where the Tech Name is filled in, but no hours will be entered.

So, this is definitely simplified to what I have in my access database, but doesn't quite work,
0
 
LVL 12

Accepted Solution

by:
Tony303 earned 500 total points
ID: 39947070
Ok,

Let's try this..

SELECT Tech, SUM(Hours)  as TotalHours
FROM
(
SELECT Tech1 as Tech, ISNULL(Hours1,0) as Hours
FROM Table
UNION ALL
SELECT Tech2 as Tech, ISNULL(Hours2,0)  as Hours
FROM Table
UNION ALL
SELECT Tech3 as Tech, ISNULL(Hours3,0) as Hours
FROM Table
) a
GROUP BY Tech 

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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