Solved

SQL query for multiple records based on one column value

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

732 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