Solved

SQL query for multiple records based on one column value

Posted on 2013-12-11
3
545 Views
Last Modified: 2013-12-28
Hi,
Please see attached screen shot of current database table [employeeHours]
I need to write a query, or set of queries that will do the following.

Select a particular office using officeid, in the case of the screen shot it's "Smithtown".

The take each empid and add up the values in the columns f,a,s,v,o for each empid.

So I need to take all rows for empid [employee] 107 and add up f,a,s,v,o
Then all rows for employee 111 and add up f,a,s,v,o
And so on for each empid.

I'm not sure exactly how to do that. I am using a scripting language (perl) to pull these results so I can do some manipulation or multiple queries if need be. it doesn't HAVE To be all in one query but it's fine if it is.

ideas?

Thanks!
Nacht
Screen-shot-2013-12-11-at-8.58.1.png
0
Comment
Question by:nachtmsk
3 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39711338
Not familiar with MySQL, but in MS SQL, your requirements are a standard GROUP By query:

SELECT empID, sum(f) TotalF, sum(a) TotalA, sum(s) TotalS, sum(v) TotalV, sum(o) TotalO
FROM employeeHours
WHERE office = 'Smithtown'
GROUP BY empID

If you need to pull other columns that vary between rows, such as the date columns, this will not work unless you also use an aggregate function (i.e. max/min) on those columns as well.
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39711418
SELECT empID, office , sum(f) TotalF, sum(a) TotalA, sum(s) TotalS, sum(v) TotalV, sum(o) TotalO
FROM employeeHours
GROUP BY empID, office
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39713319
i assume you wanted all the columns added together

SELECT empID, office , f+a+s+v+o) as TotalFASVO
FROM [employeeHours]

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

820 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