Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 subtract values from same column

Author  Topic 

eschwartz
Starting Member

2 Posts

Posted - 2015-02-24 : 13:08:29
I have a single table listing products, company, sales and quarter and need to subtract sales values of one product from another product for each company per quarter. For example: company aaaa = product B sales - product A sales in q1. Any suggestions?

product company sales quarter
A aaaa 1234 q1
A cccc 1235 q1
B aaaa 8234 q1
B cccc 8235 q1
A aaaa 4567 q2
A cccc 4568 q2
B aaaa 3567 q2
B cccc 3568 q2

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-02-24 : 14:11:51
I'm not sure understanding what you want. Given your sample data, what is the expected output?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

eschwartz
Starting Member

2 Posts

Posted - 2015-02-24 : 14:36:10
basically, i need a query that checks if a company sold product B in a quarter, and then subtracts the sales number for product b from the sales number for product a in the same quarter

the output would look like..
product company sales quarter
(a - b) aaaa (1234 - 8234) q1

i realize the example data will have a negative number, but thats ok as long as the query works. i can fix any negative numbers later.


Go to Top of Page
   

- Advertisement -