2017-08-08 86 views
-2

我已經無休止地搜索過,但還沒有找到這個問題的簡明答案。ORACLE SQL在同一語句中選擇列兩次

如何在SELECT語句中從同一個表中選擇同一列兩次,並在WHERE子句中應用不同的條件?

這樣我可以生成結果,將查詢列過濾爲兩列輸出。

謝謝你的時間。

我迄今,修訂了匿名:

SELECT C_OPERATOR AS "ID", C_OPERATORLOCALE AS "LOCALE", SUM(C_PRODUCTIONCOUNT) AS "PRODUCTION", C_OPERATOR AS "ID 2" 

FROM TABLE1 

WHERE C_STARTTIME BETWEEN TRUNC(SYSDATE - 13) + 1380/1440 AND TRUNC(SYSDATE -6) +1379/1440 
AND C_OPERATOR < '200000' 

GROUP BY C_OPERATOR, C_OPERATORLOCALE 

ORDER BY C_OPERATORLOCALE 

SELECT發言中,我指定的兩倍c_operator這顯然返回兩列的結果完全一樣......

+4

你能提供一個數據的例子,你已經嘗試過了嗎?請參閱https://stackoverflow.com/help/how-to-ask。 – BriteSponge

+2

我不知道你是否在類似於'select col1 ='某些val 1'時,然後'一些結果1',當col1 ='某些val 2',然後'一些結果2'結束時,選擇大小寫.... ....?沒有更多的信息,很難說。 – Boneist

+0

請參閱[本示例](http://sqlfiddle.com/#!4/b643e/5)。 – Phylogenesis

回答

1

如果你有像

create table Test (id number, color varchar2(20)); 

1, 'red' 
2, 'green' 
3, 'yellow' 
4, 'red' 

一個表,你可以簡單地選擇列兩次:

select t1.id, 
     case when t1.color = 'red' then 'yes' else 'no' end as red, 
     case when t1.color = 'green' then 'yes' else 'no' end as green 
    from Test t1 

或者您可以在同一張表上使用連接,如果您需要更復雜的where子句。

select t1.id, 
     case when t1.color = 'red' then 'yes' else 'no' end as red, 
     case when t2.color = 'green' then 'yes' else 'no' end as green 
    from Test t1 
    join Test t2 on t1.id = t2.id 

都將給你相同的結果:

id red green 
1 yes no 
2 no yes 
3 no no 
4 yes no 

在這種情況下加入將需要更多的資源。當oracle可以使用多個索引時,使用複合where子句可以更快地連接表兩次。

+0

在這種情況下,我有一個在C_OPERATOR的每一行中都有不同ID的表。 – Grib

-1

像OP說:

理想我想辦法利用 'c_operator < '200000'' 上c_operator 爲 「ID」,並添加 'c_operator>'199999' 的c_operator爲 「ID2」

所以,你需要像這樣:

SELECT C_OPERATOR AS "ID", 
     C_OPERATORLOCALE AS "LOCALE", 
     SUM(C_PRODUCTIONCOUNT) AS "PRODUCTION", 
     case when C_OPERATOR > '199999' then C_OPERATOR else NULL end AS "ID 2" 
FROM TABLE1 
WHERE C_STARTTIME BETWEEN TRUNC(SYSDATE - 13) + 1380/1440 AND TRUNC(SYSDATE -6) +1379/1440 
AND C_OPERATOR < '200000' 
GROUP BY C_OPERATOR, C_OPERATORLOCALE 
ORDER BY C_OPERATORLOCALE 
+0

「C_OPERATOR>'199999'」沒有任何情況,因爲「...其中C_OPERATOR <'200000'」 –

+0

@mehmet sahin 199999.5 – I3rutt

1

理想我想喜歡的方式使用「c_operator <‘200000’」上c_operator爲「ID」,並添加「c_operator>'199999」的c_operator爲「ID2」

由於@boneist在評論說,這聽起來像你正在尋找a case expression

如果你有這樣的樣本數據:

C_OPERATOR C_OPERATORLOCALE C_PRODUCTIONCOUNT C_STARTTIM 
---------- ---------------- ----------------- ---------- 
    199998 Europe/London     1 2017-07-29 
    199998 Europe/London     2 2017-07-29 
    199998 America/New York     3 2017-07-29 
    199999 Europe/London     4 2017-07-29 
    200000 Europe/London     5 2017-07-29 
    200001 Europe/London     6 2017-07-29 

則是這樣的:

select case when c_operator < 200000 then c_operator end as id, 
    c_operatorlocale as locale, 
    sum(c_productioncount) as production, 
    case when c_operator > 199999 then c_operator end as id_2 
from table1 
where c_starttime between trunc(sysdate - 13) + 1380/1440 and trunc(sysdate -6) +1379/1440 
group by c_operator, c_operatorlocale 
order by c_operatorlocale; 

會產生:

 ID LOCALE   PRODUCTION  ID_2 
---------- ---------------- ---------- ---------- 
    199998 America/New York   3   
    199998 Europe/London    3   
    199999 Europe/London    4   
      Europe/London    5  200000 
      Europe/London    6  200001 

注意,沒有對c_operator值沒有限制在where子句中 - 日期範圍中的所有行都被聚合,用case表達式將操作符值放入適當的ID列中。