日期:2014-05-19  浏览次数:20430 次

动态sql 如何写啊
我希望实现如下效果,  

和上一行比较,   10101               10101       ,   999,   999是相同的,   就显示y,   否则   显示   n,   如何做啊?

===========

得到数据是


id                 -------name   ----   -----------   ------------   ------

10101                   li

10101                   li

234                         li

999                         li

999                         li


经过sql   语句更改为:

 

id                 -------name   ----   -CONT----------   ------------   ------

10101                   li                                         n

10101                   li                                   y

234                         li                                         n

999                         li                                         n

999                         li                                         y


------解决方案--------------------
declare @t table(id int,name varchar(10))
insert @t select 10101, 'li '
union all select 10101, 'li '
union all select 234, 'li '
union all select 999, 'li '
union all select 999, 'li '
union all select 10101, 'li '

select identity(int,1,1) pk,* into # from @t
select id,name, case when (select count(*) from # where id=a.id and name=a.name and pk=a.pk-1)> 0 then 'Y ' else 'N ' end cont from # a
drop table #

--结果
id name cont
----------- ---------- ----
10101 li N
10101 li Y
234 li N
999 li N
999 li Y

(所影响的行数为 5 行)
------解决方案--------------------
create table T(id varchar(10) , name varchar(10))
insert T select '10101 ', 'li '
union all select '10101 ', 'li '
union all select '234 ', 'li '
union all select '999 ', 'li '
union all select '999 ', 'li '

select PID=identity(int, 1, 1),id, name
into #T
from T

select tmp.*,
[count]=case when tmp.