簡體   English   中英

根據Oracle SQL中Select語句中的條件排除和包含列標題

[英]Exclude and Include column header based on condition in Select statement in Oracle Sql

我有一個表,如下所示

Documentno  created      docstatus
498461      08-OCT-14    IN
501681      27-OCT-14    IN
544491      19-AUG-15    IP
550041      28-SEP-15    IP

輸出應采用以下格式

select Documentno, created from c_order where docstatus in ('IP')
Documentno  created      
544491      19-AUG-15    
550041      28-SEP-15    

select Documentno, created,docstatus from c_order where docstatus in ('IN')
Documentno  created      docstatus
498461      08-OCT-14    IN
501681      27-OCT-14    IN

預期產出,如工會

  Documentno     created      
    544491      19-AUG-15    
    550041      28-SEP-15   
  Documentno   created       docStatus
    498461      08-OCT-14    IN
    501681      27-OCT-14    IN

sql select語句中可能嗎? 我已經嘗試過Case了,但是它僅適用於Value而不適用於列名。請問有人指導我如何在oracle中實現此select語句嗎?

您必須給列名至少輸入一個字符

測試數據

with t(Documentno,
created,
docstatus) as
 (select 498461, sysdate, 'IN'
    from dual
  union all
  select 501681, sysdate + 1, 'IN'
    from dual
  union all
  select 544491, sysdate + 2, 'IP'
    from dual
  union all
  select 550041, sysdate + 3, 'IP'
    from dual)

詢問

  select Documentno, created, null as " "
      from t
     where docstatus in ('IP')
    union all
    select null, null, 'STATUS'
      from dual
    union all
    select Documentno, created, docstatus from t where docstatus in ('IN')

輸出量

DOCUMENTNO CREATED
---------- -------- ------
    544491 10/07/16
    550041 11/07/16
                    STATUS
    498461 08/07/16 IN
    501681 09/07/16 IN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM