--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([姓名] varchar(1),[日期] varchar(7),[数据] int)
insert [TB]
select 'A','2012-10',90 union all
select 'B','2012-10',85 union all
select 'A','2012-11',120 union all
select 'C','2012-11',100 union all
select 'C','2012-10',60
SELECT
A.[姓名]
,A.[数据]-B.[数据]
from [TB] A
INNER JOIN TB B ON A.[姓名]=B.[姓名] and A.[日期]>B.[日期]
WHERE a.[日期]='2012-11' AND B.[日期]='2012-10'
/*
姓名
---- -----------
A 30
C 40
(2 行受影响)
*/
DROP TABLE TB
------其他解决方案-------------------- select 姓名,SUM(case when 日期='2012-10' then 数据*-1 else 数据 end) as 数据和
from tablename
where 日期 in ('2012-10','2012-11')
group by 姓名
having COUNT(1)=2 ------其他解决方案-------------------- select A.姓名, 数据 = B.数据 - A.数据
from 表 as A,表 as B
where A.姓名= B.姓名
and A.日期 ='2012-10'
and B.日期 ='2012-11'