2013-09-30 117 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