Oracle-用于创建间隔的理论sql查询

Oracle-用于创建间隔的理论sql查询,sql,oracle,connect-by,Sql,Oracle,Connect By,通过ORACLE中的sql查询可以解决这种情况吗 我有一张这样的桌子: TYPE UNIT A 230 B 225 C 60 D 45 E 5 F 2 0 - 77 -> 4 78 - 154 -> 0 155 - 230 -> 2 我需要将单位分割成三个“相同”大小的变量间隔,然后计算出每个变量的计数?意思是这样的: TYPE UNIT A 230 B

通过ORACLE中的sql查询可以解决这种情况吗

我有一张这样的桌子:

TYPE    UNIT
A       230
B       225
C       60
D       45
E       5
F       2
0   - 77    -> 4
78  - 154   -> 0
155 - 230   -> 2
我需要将单位分割成三个“相同”大小的变量间隔,然后计算出每个变量的计数?意思是这样的:

TYPE    UNIT
A       230
B       225
C       60
D       45
E       5
F       2
0   - 77    -> 4
78  - 154   -> 0
155 - 230   -> 2

是的,这可以在Oracle中完成。困难的部分是边界的定义。可以对值为1、2和3的序列使用最大值和某些算术运算

之后,剩下的只是交叉连接和聚合:

with bounds as (
      select (case when n = 1 then 0
                   when n = 2 then trunc(maxu / 3)
                   else trunc(2 * maxu / 3)
              end) as lowerbound,
             (case when n = 1 then trunc(maxu / 3) 
                   when n = 2 then trunc(2*maxu / 3)
                   else maxu
              end) as upperbound
      from (select 1 as n from dual union all select 2 from dual union all select 3 from dual
           ) n cross join
           (select max(unit) as maxu from atable t)
     )
select b.lowerbound || '-' || b.upperbound,
       sum(case when units between b.lowerbound and b.upperbound then 1 else 0 end)
from atable t cross join
     bounds b
group by b.lowerbound || '-' || b.upperbound;

是的,这可以在Oracle中完成。困难的部分是边界的定义。可以对值为1、2和3的序列使用最大值和某些算术运算

之后,剩下的只是交叉连接和聚合:

with bounds as (
      select (case when n = 1 then 0
                   when n = 2 then trunc(maxu / 3)
                   else trunc(2 * maxu / 3)
              end) as lowerbound,
             (case when n = 1 then trunc(maxu / 3) 
                   when n = 2 then trunc(2*maxu / 3)
                   else maxu
              end) as upperbound
      from (select 1 as n from dual union all select 2 from dual union all select 3 from dual
           ) n cross join
           (select max(unit) as maxu from atable t)
     )
select b.lowerbound || '-' || b.upperbound,
       sum(case when units between b.lowerbound and b.upperbound then 1 else 0 end)
from atable t cross join
     bounds b
group by b.lowerbound || '-' || b.upperbound;

您可以使用“最大值”和“连接方式”查询为每个范围生成上限值和下限值:

select ceil((level - 1) * int) as int_from,
  floor(level * int) - 1 as int_to
from (select round(max(unit) / 3) as int from t42)
connect by level <= 3;

  INT_FROM     INT_TO
---------- ----------
         0         76 
        77        153 
       154        230 
然后对原始表进行左外联接,对每个范围进行计数,这样中间范围的值为零值:

with intervals as (
  select ceil((level - 1) * int) as int_from,
    floor(level * int) - 1 as int_to
  from (select round(max(unit) / 3) as int from t42)
  connect by level <= 3
)
select i.int_from || '-' || i.int_to as range,
  count(t.unit)
from intervals i
left join t42 t
on t.unit between i.int_from and i.int_to
group by i.int_from, i.int_to
order by i.int_from;

RANGE      COUNT(T.UNIT)
---------- -------------
0-76                   4 
77-153                 0 
154-230                2 

您可以使用“最大值”和“连接方式”查询为每个范围生成上限值和下限值:

select ceil((level - 1) * int) as int_from,
  floor(level * int) - 1 as int_to
from (select round(max(unit) / 3) as int from t42)
connect by level <= 3;

  INT_FROM     INT_TO
---------- ----------
         0         76 
        77        153 
       154        230 
然后对原始表进行左外联接,对每个范围进行计数,这样中间范围的值为零值:

with intervals as (
  select ceil((level - 1) * int) as int_from,
    floor(level * int) - 1 as int_to
  from (select round(max(unit) / 3) as int from t42)
  connect by level <= 3
)
select i.int_from || '-' || i.int_to as range,
  count(t.unit)
from intervals i
left join t42 t
on t.unit between i.int_from and i.int_to
group by i.int_from, i.int_to
order by i.int_from;

RANGE      COUNT(T.UNIT)
---------- -------------
0-76                   4 
77-153                 0 
154-230                2 

想再试一次吗?我不知道您是如何从表数据获得所需输出的。对你要做的事情进行更详细的解释会很有帮助……你写的东西对我来说毫无意义。想再试一次吗?我不知道您是如何从表数据获得所需输出的。更详细地解释一下你要做的事情会有帮助的……你写的东西对我来说毫无意义。只花了10分钟。。!?!天才小问题,你缺少作为上限,你指的是单位,而不是单位;稍微大一点的问题,你们的范围目前重叠,所以76或153将被计算两次?我真的不是跟着你到处寻找缺点,8-泰坦之战;只花了10分钟。。!?!天才小问题,你缺少作为上限,你指的是单位,而不是单位;稍微大一点的问题,你们的范围目前重叠,所以76或153将被计算两次?我真的不是跟着你到处寻找缺点,8-泰坦之战;