2012-04-15 104 views
0

,如果我是有,只是返回的記錄:獲得各種可能的組合列

SELECT * FROM USERS

Name | Gender | Company 
    Bob | male | Abc Inc. 
    Bob | male | Xyz Inc. 
    Bob | male | NULL 

而且我想它返回此:

Name | Gender | Company 
    NULL | NULL | NULL 
    NULL | NULL | Abc Inc. 
    NULL | male | NULL 
    NULL | male | Abc Inc. 
    NULL | NULL | Xyz Inc. 
    NULL | male | Xyz Inc. 
    Bob | NULL | NULL 
    Bob | NULL | Abc Inc. 
    Bob | male | NULL 
    Bob | male | Abc Inc. 
    Bob | NULL | Xyz Inc. 
    Bob | male | Xyz Inc. 

如何它完成了嗎?

回答

1
select Name, Gender, Company 
from T 
group by Name, Gender, Company with cube 
+0

你錯過了'distinct'那裏。 – 2012-04-15 18:50:52

+1

沒有廢話,那工作..哈哈謝謝。 :) – 2012-04-15 18:55:09

0
SELECT DISTINCT NAME, GENDER, COMPANY FROM TABLE 
GROUP BY NAME, GENDER, COMPANY WITH CUBE