1

我想为字符串选择不同的列组合。我不知道如何在查询中做到这一点。场景如下

c1   c2   c3
a     1    x
b     2    x
b     2    y

我想要一个像

a:1:x
a:1:y
a:2:x
a:2:y
b:1:x
b:1:y
b:2:x
b:2:y

关于如何做的任何建议?

4

3 回答 3

3

示例 CREATE TABLE 语句

create table #Test( c1 char(1), c2 char(1), c3 char(1) )
insert INTO #Test
SELECT
    'a', '1', 'x'
UNION ALL SELECT
    'b', '2', 'x'
UNION ALL SELECT
    'b', '2', 'y'

所有不同列值的组合

select 
    c1List.c1, c2List.c2, c3List.c3
from ( 
    select DISTINCT c1 from #Test ) c1List
CROSS JOIN (
    select DISTINCT c2 from #Test ) c2List
CROSS JOIN (
    select DISTINCT c3 from #Test ) c3List

字符串连接

select 
    c1List.c1 + ':' + c2List.c2 + ':' + c3List.c3
from ( 
    select DISTINCT c1 from #Test ) c1List
CROSS JOIN (
    select DISTINCT c2 from #Test ) c2List
CROSS JOIN (
    select DISTINCT c3 from #Test ) c3List
于 2012-12-03T12:32:09.537 回答
1
select concat(c1,':',c2,':',c3) from 
(select distinct c3 from t) as t3, 
(select distinct c2 from t) as t2, 
(select distinct c1 from t) as t1
于 2012-12-03T12:34:35.513 回答
-1

select concat(c1,':',c2,':',c3) from ...

于 2012-12-03T12:28:50.317 回答