日期:2014-05-16 浏览次数:20472 次
declare @tb table(mlszh varchar(12),yjm varchar(40))
insert into @tb(mlszh,yjm)
select '201400912532','81172180058771689485' union all
select '201400912532','81346930080349091223' union all
select '201400912532','81346930080349091224' union all
select '201400912532','81346930080349091223' union all
select '201400912534','81327590023827322966' union all
select '201400912534','81327590023826922558'
SELECT
*,
CASE WHEN EXISTS(SELECT mlszh FROM (SELECT mlszh,yjm,COUNT(1) AS num FROM @tb t GROUP BY mlszh,yjm HAVING COUNT(1)>1)a WHERE mlszh=t.mlszh) THEN 'n' ELSE 'y' END AS clbz,
CASE WHEN EXISTS(SELECT mlszh FROM (SELECT mlszh,yjm,COUNT(1) AS num FROM @tb t GROUP BY mlszh,yjm HAVING COUNT(1)>1)a WHERE mlszh=t.mlszh AND yjm=t.yjm) THEN 'y' ELSE '' END AS cfbz
FROM
@tb t
/*mlszh yjm clbz cfbz
------------ ---------------------------------------- ---- ----
201400912532 81172180058771689485 n
201400912532 81346930080349091223 n y
201400912532 81346930080349091224 n
201400912532 81346930080349091223 n y
201400912534 81327590023827322966 y
201400912534 81327590023826922558 y
(6 行受影响)
*/