Solved

Sum of multiple columns

Posted on 2014-03-21
3
314 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 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

21 Experts available now in Live!

Get 1:1 Help Now