2 Replies - 516 Views - Last Post: 05 February 2017 - 09:36 PM

#1 Hady Shaaban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-February 17

SQL Server subtraction

Posted 02 February 2017 - 05:20 AM

Dears,
Good day every body.
I have a problem that I want to get difference between two values from one column based on certain selection criterias as follows :
Product channel Month Year Sales
x retail 1 2016 10
y retail 2 2016 20
x retail 3 2016 60
x wholesale 4 2015 40
x wholesale 5 2016 20
x retail 6 2015 5
the above data is a part of the table. I want to write a query to get difference in sales based on the following selection : (sales of year 2016-sales of year 2015) where product =x & channel=retail. so the result must be 60+10-5=65
Much appreciate your support :)
Regards,

Is This A Good Question/Topic? 0
  • +

Replies To: SQL Server subtraction

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2342
  • View blog
  • Posts: 9,393
  • Joined: 03-December 12

Re: SQL Server subtraction

Posted 02 February 2017 - 10:16 AM

Moving to SQL area.

First thought is why you are separating the month and year? Date column types work well and hold all of that information.


Now, what queries have you tried thus far?
Was This Post Helpful? 0
  • +
  • -

#3 Hady Shaaban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-February 17

Re: SQL Server subtraction

Posted 05 February 2017 - 09:36 PM

astonecipher,
Thank you for your interest in helping me :). Separating months and years is because these data will be used for c# application with input parameters month and year. Users will use this application for reporting may use different date format. Months and years numbers will be more easier. By the way i solved this problem by the following Query:
select (select sum([Sales]) from [table name] as totalsales where [Product]='x' AND [Year]=2016 AND [Month]=8)-(select sum([Sales]) from [table name] as totalsales where [Product]='x' AND [Year]=2015 AND [Month]=8);
Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1