Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

query question to add value of two rows together

Posted on 2008-06-13
3
Medium Priority
?
1,856 Views
Last Modified: 2012-06-22
I like to have a query to add all the type A+B salary together for each yr_mon.  Help me on the query please.  Thanks.

Here is the table data.
yr_mon      type  salary      
2008_01      A           100      
2008_01      B            200    
2008_02      A            300      
2008_02      B            400      
2008_02      C            500      

Here is my result data.  
yr_mon        salary      
2008_01                 300      
2008_02               700      
0
Comment
Question by:ewang1205
[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
  • 2
3 Comments
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 1600 total points
ID: 21782716
select a.yr_mon, salsuma + salsumb
from
(
select yr_mon, salsuma = sum(salary)
from yourtable
where type = 'A'
group by yr_mon
) a
INNER JOIN
(
select yr_mon, salsumb = sum(salary)
from yourtable
where type = 'B'
group by yr_mon
) b ON a.yr_mon = b.yr_mon
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 1600 total points
ID: 21782722
if there is a mismatch, you'll want to use this...

select a.yr_mon, isnull(salsuma,0) + isnull(salsumb,0)
from
(
select yr_mon, salsuma = sum(salary)
from yourtable
where type = 'A'
group by yr_mon
) a
full outer JOIN
(
select yr_mon, salsumb = sum(salary)
from yourtable
where type = 'B'
group by yr_mon
) b ON a.yr_mon = b.yr_mon
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 21783141
Could either A or B be mssing?  

If not:

SELECT a.yr_mon, a.salary + b.salary AS salary
FROM tableData a
INNER JOIN tableData b ON a.yr_mon = b.yr_mon AND b.type = 'B'
WHERE a.type = 'A'

If B, only, could be missing:

SELECT a.yr_mon, a.salary + ISNULL(b.salary, 0) AS salary
FROM tableData a
LEFT OUTER JOIN tableData b ON a.yr_mon = b.yr_mon AND b.type = 'B'
WHERE a.type = 'A'


If A, only, could be missing, let me know, that's somewhat more coding, don't want to do it for no reason :-) .
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

610 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