有这么一个表:
id name content date
---------------------------------
1 zhang abc 2007-01-05
2 yang ab 2007-01-12
3 zhang abc 2007-01-25
4 zhang abc 2007-02-01
5 zhang abc 2007-02-21
6 yang ac 2007-03-01
7 hu abc 2007-04-01
。。。
现在要做统计,(id是递增1的)
每个人每个月最新的内容,如果为abc的返回1,如果不是abc返回0
结果应该返回如下格式:
name contentIsABC date
---------------------------
zhang 1 2007-01-25 ---> 1月份中间最后的内容
zhang 1 2007-02-21 ---> 2月份最后的一次内容也是abc
yang 0 2007-01-12
yang 0 2007-03-01
hu 1 2007-04-01
发了100分,希望高手愿意帮忙,很紧急,我死都想不出来。在线等,,,拜托了
------解决方案--------------------
SELECT
name,
contentIsABC = case content when 'abc ' then 1 else 0 end,
date
FROM 表 A
WHERE NOT EXISTS(
SELECT * FROM 表
WHRE name = A.name
AND datediff(month, date, A.date) = 0
AND id > A.id)
------解决方案--------------------
declare @tab table(id int,name varchar(10),content varchar(10),date datetime)
insert into @tab select 1, 'zhang ', 'abc ', '2007-01-05 '
insert into @tab select 2, 'yang ', 'ab ', '2007-01-12 '
insert into @tab select 3, 'zhang ', 'abc ', '2007-01-25 '
insert into @tab select 4, 'zhang ', 'abc ', '2007-02-01 '
insert into @tab select 5, 'zhang ', 'abc ', '2007-02-21 '
insert into @tab select 6, 'yang ', 'ac ', '2007-03-01 '
insert into @tab select 7, 'hu ', 'abc ', '2007-04-01 '
select
t.name,(case t.content when 'abc ' then 1 else 0 end) as contentIsABC,t.date
from
@tab t
where