2011-03-22 81 views
-2

我有表A和COL1,数据COL2作为Oracle查询

col1 col2 
----------- 
1  x 
2  x 
3  x 
1  y 
2  y 
3  y 
4  y 
1  z 
2  z 

我想输出:

col1 col2 
----------- 
1  x 
2  x 
3  x 
4  x 
1  y 
2  y 
3  y 
4  y 
1  z 
2  z 
3  z 
4  z 

即使值不存在在COL2在最大值COL1即“4”查询应显示多达4

回答

6
SELECT A.col1, B.col2 
FROM (SELECT DISTINCT col1 FROM YourTable) A 
CROSS JOIN (SELECT DISTINCT col2 FROM YourTable) B 
1

如果你想在col1和COL2值的每个可能的组合的笛卡尔积:

Select col1, col2 from 
(select distinct col1 from sourcetable) as t1 
Cross join 
(select distinct col2 from sourcetable) as t2