I have a table with dates from first of previous year till last month i.e., Jan 2015 to Sep 2016.
I have to perform a two months comparison in the same table as below i. e., select all rows in Jan month from table1
except rows in Feb month from table 1 ; select all rows in Feb month except rows in march month from table1
.....
Till it reaches last month.
Here I need do conditional union for each two month comparison till it reaches last month.
Sample:
select col1,col2 from table1 where month=jan 2015
except
select col1,col2 from table1 where month=feb 2015
union
select col1,col2 from table1 where month=feb 2015
except
select col1,col2 from table1 where month=mar 2015
.
.
.
union
select col1,col2 from table1 where month=aug 2016
except
select col1,col2 from table1 where month=sep 2016
Sample data:
Customer Product date amount
-------------------------------------------
a p1 1/31/2015 $12
a p2 1/31/2015 $13
a p2 2/28/2015 $1
So here product p1 exist in jan and no longer in feb 2015. So I need all products in Jan which are not there in Feb 2015 ....
Can anyone suggest a solution?