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
Medium Priority
1,856 Views
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
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
• 2

LVL 60

Assisted Solution

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

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

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

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.
###### Suggested Courses
Course of the Month9 days, 20 hours left to enroll