I'm trying to do calculation on two columns within the table.. (DDL for
creating and inserting test data below)
I need to get the difference between the last entry and first entry on the
columns "E" and "M" and it must be grouped by each month. I also need to
ignore the rows where the Total is equal or less than 0 (zero).
How can I achieve this?
create table [tblWeek](
[E] [int] null,
[M] [int] null,
[Total] [int] null,
[Date] [date] null,
[LastTotal] [int] null,
) on [PRIMARY]
go
insert into tblWeek (E,M,Total, Date, LastTotal)
values (967652,1086346,2053998,'2010-09-20', 10765)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (962932,1080301,2043233,'2010-09-13',0)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (962932,080301,2043233,'2010-09-06',5490)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (960808,1076935,2037743,'2010-08-30',7692)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (958864,1071187,2030051,'2010-08-23',15336)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (954219,1060496,2014715,'2010-08-09',6715)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (952196,1055804,2008000,'2010-08-02',6457)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (949797,1051746,2001543,'2010-07-26',-31294)
creating and inserting test data below)
I need to get the difference between the last entry and first entry on the
columns "E" and "M" and it must be grouped by each month. I also need to
ignore the rows where the Total is equal or less than 0 (zero).
How can I achieve this?
create table [tblWeek](
[E] [int] null,
[M] [int] null,
[Total] [int] null,
[Date] [date] null,
[LastTotal] [int] null,
) on [PRIMARY]
go
insert into tblWeek (E,M,Total, Date, LastTotal)
values (967652,1086346,2053998,'2010-09-20', 10765)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (962932,1080301,2043233,'2010-09-13',0)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (962932,080301,2043233,'2010-09-06',5490)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (960808,1076935,2037743,'2010-08-30',7692)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (958864,1071187,2030051,'2010-08-23',15336)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (954219,1060496,2014715,'2010-08-09',6715)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (952196,1055804,2008000,'2010-08-02',6457)
insert into tblWeek (E,M,Total, Date, LastTotal)
values (949797,1051746,2001543,'2010-07-26',-31294)