SQL Server : compare data in same table -


my db sql server 2012 sp1.

here table:

  date            f1          f2        f3 2015-07-1         32           5        78 2015-08-20        18          45        67 2015-09-1         64           9        80 2015-09-30        12          78        45 

now count sum , select difference value between last , current month, in example selected data should between august , september, example field f1 take unique value august 18 2 values september 64+12=76, count difference 76-18, value of f1 should 58.

thanks in advance.

select       sum(case when month(date) = month(curdatde()) ,  year(date) = year(curdatde()) f1 else null end)- sum(case when month(date) = (month(curdatde())-1) ,  year(date) = year(curdatde()) f1 else null end) as'f1',      sum(case when month(date) = month(curdatde()) ,  year(date) = year(curdatde()) f2 else null end)- sum(case when month(date) = (month(curdatde())-1) ,  year(date) = year(curdatde()) f2 else null end) as'f2',      sum(case when month(date) = month(curdatde()) ,  year(date) = year(curdatde()) f3 else null end)- sum(case when month(date) = (month(curdatde())-1) ,  year(date) = year(curdatde()) f3 else null end) as'f3' table 

this works mysql


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -