这是代码
DECLARE @List TABLE (n nvarchar(50));
INSERT INTO @list (n) VALUES ('A'),('B'),('C'),('D'),('E');
SELECT
(t1.n )
+ (t2.n)
+ (t3.n)
+ (t4.n)
+(t5.n)
FROM @list AS t1
JOIN @list AS t2
ON 1=1
JOIN @list AS t3
ON 1=1
JOIN @list AS t4
ON 1=1
JOIN @list AS t5
ON 1=1
WHERE
not t1.n = t2.n
and not t1.n = t3.n
and not t1.n = t4.n
and not t1.n = t5.n
and not t2.n = t3.n
and not t2.n = t4.n
and not t2.n = t5.n
and not t3.n = t4.n
and not t3.n = t5.n
and not t4.n = t5.n
我需要简化where子句,如(其中T1.n,T2.n,T3.n,.T4.n,T5.n彼此不相同)
提前致谢
答案 0 :(得分:4)
您可以查看您拥有的不同值:
where (
select count(distinct n)
from (values (t1.n), (t2.n), (t3.n), (t4.n), (t5.n)) as ns (n)
) = 5
如果您的值不足5个,则某些值相等。
答案 1 :(得分:3)
你可以使用,
NOT IN (....)
条款
像,
where t1.n NOT IN (t2.n, t3.n, t4.n, t5.n)
and t2.n NOT IN (t3.n , t4.n,t5.n)
and t3.n NOT IN (t4.n,t5.n)
答案 2 :(得分:1)
要创建组合(或者是排列吗?),您可以使用CTE。
例如:
;with cte as
(
select n from @list
union all
select convert(nvarchar(50),L.n+ cte.n) from @List L
inner join cte on charindex(l.n,cte.n)=0
where LEN(l.n)<5
)
select * from cte where LEN(n)=5