簡體   English   中英

根據 oracle sql 中的條件獲取數據

[英]Get data based on condition in oracle sql

我的桌子

loads(Unique)       Value
 T123                11
 T234               9.5
 T456               15
 T678               35
 T345               3.7    

想要我想要

 count(values<=10)    count(values>10 &<=20)   count(values>20) 
            2                       2                    1

我嘗試使用 CASE 但不知道用法

CASE是的; 不是COUNT而是SUM

SQL> with test (loads, value) as
  2    (select 't123', 11   from dual union all
  3     select 't234',  9.5 from dual union all
  4     select 't456', 15   from dual union all
  5     select 't678', 35   from dual union all
  6     select 't345',  3.7 from dual
  7    )
  8  select
  9    sum(case when value <= 10 then 1 end) cnt_1,
 10    sum(case when value > 10  and value <= 20 then 1 end) cnt_2,
 11    sum(case when value > 20 then 1 end) cnt_3
 12  from test;

     CNT_1      CNT_2      CNT_3
---------- ---------- ----------
         2          2          1

SQL>

使用條件聚合作為

select coalesce(sum(case when value<=10 then 1 end),0) as "values<=10",
       coalesce(sum(case when value>10 and value<=20 then 1 end),0) as "values>10value<20",
       coalesce(sum(case when value>20 then 1 end),0) as "values>20"
from your_table;

暫無
暫無

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

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