2013-09-30 121 views
0

我有DB与得到所有可能的组合

|x1|y1|z1|c1| 

我想提取可能像所有的组合:

x1 
x1 y1 
x1 z1 
x1 c1 
x1 y1 z1 
x1 y1 c1 
x1 z1 c1 
x1 y1 z1 c1 
y1 
y1 z1 
y1 c1 
y1 z1 c1 
z1 
z1 c1 
c1 

我如何使用SQL办呢?

+1

我的上帝,没有。不要试图在SQL中实现这一点。使用应用程序 - 而且有许多排列和子集选择的标准实现。 –

+0

试图理解这一点。你的意思是你有一个数据库表,其中一个字段包含指定的4条记录? – neelsg

+0

你真的应该考虑使用存储过程或在应用程序级别解决这个问题。但是,你可以实现这个基本上连接表,一列值的选择'SELECT DISTINCT t1.v,t2.v ... FROM(SELECT DISTINCT COLUMN1 v FROM TABLE)t1,(SELECT DISTINCT COLUMN2 v FROM TABLE)t2 .. .' – Mikhail

回答

0
with combi (old, new) as          
(select 'x1y1z1c1', '    ' from sysibm/sysdummy1 
union all             
select substr(old, 3),          
     strip(new) concat substr(old, 1, 2) from combi  
     where locate(substr(old, 1, 2), new) = 0    
union all             
select substr(old, 1, 2) concat substr(old, 5),    
     strip(new) concat substr(old, 3, 2) from combi  
     where locate(substr(old, 3, 2), new) = 0    
union all             
select substr(old, 1, 4) concat substr(old, 7),    
     strip(new) concat substr(old, 5, 2) from combi  
     where locate(substr(old, 5, 2), new) = 0    
union all             
select substr(old, 1, 6),         
     strip(new) concat substr(old, 7, 2) from combi  
     where locate(substr(old, 7, 2), new) = 0  
)             
select distinct new         
from combi