Solved

query question to add value of two rows together

Posted on 2008-06-13
3
1,814 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
  • 2
3 Comments
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 400 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 400 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 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 100 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email the result out from a T-SQL queries 29 62
SSRS Get Last Day of Year 2016 2 44
How does this SELECT query work 11 99
SQL Exceptions 3 35
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
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.
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.

948 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

21 Experts available now in Live!

Get 1:1 Help Now