簡體   English   中英

僅在有多個結果的情況下才返回行

[英]How to return rows only if there is more than one result

在Oracle 10g數據庫中,我想構建一個SQL查詢,該查詢僅在有多個行結果時才返回結果行。

有可能嗎?

您需要計算返回結果的數量,如果您不希望分組,可以使用以下方法:

SELECT *
  FROM (SELECT col1,
               col2,
               COUNT(*) OVER() cnt
          FROM your_table
         WHERE <conditions> )
 WHERE cnt > 1
select * from 
  (select column1,column2,column3,
    (select count(*) from table1 where '*XYZ Condition*' ) as rowCount 
  from table1 where '*XYZ Condition*') 
where rowCount > 1;

您只需要在查詢的兩個位置都放置相同的條件,即,兩個where子句的“ XYZ條件 ”都相同。

您是否需要以下結果?

c1   c2
1    AA
1    BB
2    CC

結果:

c1   c2
1    AA,BB
2    CC

以下內容可以滿足您的要求。

select c1,ltrim(sys_connect_by_path(c2,','),',') from (
  select c1,c2, row_number() over(partition by c1 order by c2)rn, 
                     count(*) over(partition by id ) cnt from XXX  -- XXX: your table
                 ) a  where level=cnt
              start with rn=1  connect by prior c1=c1 and prior rn=rn-1

暫無
暫無

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

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