Solved

SQL query for multiple records based on one column value

Posted on 2013-12-11
3
535 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

10 Experts available now in Live!

Get 1:1 Help Now