22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([relation] int,[code] varchar(5))
insert [tb]
select 1,'0101' union all
select 1,'0102' union all
select 2,'0101' union all
select 3,'0202' union all
select 4,'0001S' union all
select 4,'0101' union all
select 5,'0101' union all
select 5,'0102' union all
select 5,'0103E' union all
select 6,'0002' union all
select 6,'0102'
declare @str varchar(100)
set @str='0001S,0101'
select top 1 relation from tb
group by relation
order by sum(case when charindex(','+rtrim(code)+',',','+@str+',')>0
then 1 else 0 end) desc,count(*)
/*
relation
-----------
4
create table tb(relation int,code varchar(10))
insert into tb select 1,'0101'
insert into tb select 1,'0102'
insert into tb select 2,'0101'
insert into tb select 3,'0202'
insert into tb select 4,'0001S'
insert into tb select 4,'0101'
insert into tb select 5,'0101'
insert into tb select 5,'0102'
insert into tb select 5,'0103E'
insert into tb select 6,'0002'
insert into tb select 6,'0102'
go
declare @col varchar(1000)
set @col='0101,0102'
--set @col='0001,0101'
;with cte as(
select row_number()over(order by (select 1))rn, substring(@col,number,charindex(',',@col+',',number+1)-number)col from master..spt_values
where type='p' and number<=len(@col) and substring(@col,number,1)<>',' and substring(','+@col,number,1)=','
)select top 1 relation from (
select distinct b.relation,a.col from cte a inner join tb b on b.code like a.col+'%'
)t group by relation having count(*)=(select max(rn) from cte)
/*
relation
-----------
1
(1 行受影响)
*/
go
drop table tb
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-10-24 10:11:22
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([relation] int,[code] varchar(5))
insert [tb]
select 1,'0101' union all
select 1,'0102' union all
select 2,'0101' union all
select 3,'0202' union all
select 4,'0001S' union all
select 4,'0101' union all
select 5,'0101' union all
select 5,'0102' union all
select 5,'0103E' union all
select 6,'0002' union all
select 6,'0102'
--------------开始查询--------------------------
select relation from tb t where code='0101' group by relation having COUNT(1)=(select COUNT(code) from tb where relation=t.relation)
----------------结果----------------------------
/* relation
-----------
2
(1 行受影响)
*/
declare @param varchar(max) --你要传入的code
exec ('select relation from tb where code in ('''+@param+''')')
[Quote=引用楼主 celeryzeng 的回复:]关注
select min(relation) from tb where code='0101'
select distinct min(relation) from tb where code='0101'
select distinct relation
from tab a
where code = '0101'
and exists (
select 1 from tab
where relation = a.relation
and code = '0102'
)
and not exists
select 1 from tab
where relation = a.relation
and code not in ( '0101','0102')
)
select distinct relation
from tab a
where code = '0101'
and not exists
select 1 from tab
where relation = a.relation
and code not in ( '0101')
)