Solved

SQL query for multiple records based on one column value

Posted on 2013-12-11
3
551 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

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…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

691 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