将两个独立列的结果合并为一个细长列

时间:2015-06-18 18:50:15

标签: sql oracle select

我的表:

ID       Name     Status1     Status2
-------------------------------------
 1       foo       bar        grain
 2       bar       foo        sball
 3       foo       bar        grain
 4       far       boo        sball

我需要它真的这样出来:

ID      Name      Status    
-------------------------------
 1       foo       bar       
 1       foo       grain
 2       bar       foo       
 2       bar       sball
 3       foo       bar       
 3       foo       bar
 4       far       boo       
 4       far       sball

我将如何做到这一点,你能解释一下原因吗?

我尝试过concat,但这显然是错误的。

3 个答案:

答案 0 :(得分:5)

您可以使用E ====================================================================== ERROR: test_sauce_labs_header (__main__.SauceLabsHeader) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\-kuhu-\git\CS82A_Automation\src\SauceLabs_Glyph_WebDriver.py", line 43, in test_sauce_labs_header driver.find_element_by_xpath(header_link).click() File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 232, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 664, in find_element {'using': by, 'value': value})['value'] File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//nav[@id =\"global\"]//a[contains(text(),\"Sign Up\")]"} Stacktrace: at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/-kuhu-/appdata/local/temp/tmpivizdh/extensions/fxdriver@googlecode.com/components/driver-component.js:10271) at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/-kuhu-/appdata/local/temp/tmpivizdh/extensions/fxdriver@googlecode.com/components/driver-component.js:603) ---------------------------------------------------------------------- Ran 1 test in 94.708s FAILED (errors=1) (或union all,这取决于您希望获得的内容unionStatus1相同):

Status2

SQLFiddle

答案 1 :(得分:0)

这是另一个解决方案:

WITH counter AS
   (SELECT LEVEL lvl FROM dual CONNECT BY LEVEL <= 2)
SELECT t.ID
      ,t.NAME
      ,CASE c.lvl
         WHEN 1 THEN t.status1
         WHEN 2 THEN t.status2
       END status
FROM   test_table t
CROSS JOIN counter c;

分层查询是您在http://asktom.oracle.com/上经常看到的用于生成数字列表的内容。交叉连接为连接表中的每个行组合返回一行。

答案 2 :(得分:0)

Unpivot

    select * from tbl t unpivot (Status for col in(Status1, Status2));