PLSQL:子查询之外的条件

时间:2018-11-23 12:33:53

标签: sql oracle subquery

我有这样的查询(原始查询要复杂得多)

select 0, 999, count(case when cond = 'aa' then 1 end) val1, sum(case when cond = 'aa' then amount else 0 end) amount1,
   count(case when cond = 'bb' then 1 end) val2, sum(case when cond = 'bb' then amount else 0 end) amount2
from myTable
where amount between 0 and 999
union
select 1000, 2999, count(case when cond = 'aa' then 1 end) val1, sum(case when cond = 'aa' then amount else 0 end) amount1,
   count(case when cond = 'bb' then 1 end) val2, sum(case when cond = 'bb' then amount else 0 end) amount2
from myTable
where amount between 1000 and 2999
union
...

有多个并集,选择的方式更加复杂。

我想简化一下。为此,我使用了with子句

with q1 as(
    select case when cond = 'aa' then 1 end val1, case when cond = 'aa' then amount else 0 end amount1,
           case when cond = 'bb' then 1 end val2, case when cond = 'bb' then amount else 0 end amount2
)
select 0, 999, sum(val1) val1, sum(amount1) amount1, sum(val2) val2, sum(amount2) amount2
from q1 where amount between 0 and 999
union
select 1000, 2999, sum(val1) val1, sum(amount1) amount1, sum(val2) val2, sum(amount2) amount2
from q1 where amount between 1000 and 2999
union
....

但就我而言,这仍然很长。 在plsql中有可能这样写吗

with q1 as (
   select minVal, maxVal, count(case when cond = 'aa' then 1 end) val1, sum(case when cond = 'aa' then amount else 0 end) amount1,
      count(case when cond = 'bb' then 1 end) val2, sum(case when cond = 'bb' then amount else 0 end) amount2
   from myTable
   where amount between minVal and maxVal
)
      select * from q1 insideWhere minVal=0 and maxVal=999
union select * from q1 insideWhere minVal=1000 and maxVal=2999
union ...

或其他解决方案?

2 个答案:

答案 0 :(得分:1)

听起来应该是一个汇总查询,例如:

SELECT min_val,
       max_val,
       COUNT(CASE WHEN cond = 'aa' THEN 1 END) val1,
       SUM(CASE WHEN cond = 'aa' THEN amount ELSE 0 END) amount1,
       COUNT(CASE WHEN cond = 'bb' THEN 1 END) val2,
       SUM(CASE WHEN cond = 'bb' THEN amount ELSE 0 END) amount2,
FROM   (SELECT cond,
               amount,
               CASE WHEN amount BETWEEN 0 AND 999 THEN 0
                    WHEN amount BETWEEN 1000 AND 2999 THEN 1000
               END min_val,
               CASE WHEN amount BETWEEN 0 AND 999 THEN 999
                    WHEN amount BETWEEN 1000 AND 2999 THEN 2999
               END max_val
        FROM   mytable) -- using a subquery here to avoid repeating the case statement in both the select column list and the group by column list
GROUP BY min_val,
         max_val;

答案 1 :(得分:0)

要执行您要尝试的操作,需要将最小/最大组显示为类似表格的结构。您已经了解了大部分方法:执行此操作的简单方法是使用WITH子句。

WITH value_groups AS
         (SELECT 0 AS min_val, 999 AS max_val FROM DUAL
          UNION ALL
          SELECT 1000, 2999 FROM DUAL),
     q1 AS
         (SELECT *
          FROM   my_table
                 PIVOT
                     (COUNT (*) FOR cond IN ('aa' AS aa, 'bb' AS bb)))
SELECT   min_val,
         max_val,
         COUNT (aa) as val1,
         SUM (aa * amount) as amount1,
         COUNT (bb) as val2,
         SUM (bb * amount) as amount2 
FROM     q1 JOIN value_groups ON amount BETWEEN min_val AND max_val
GROUP BY min_val, max_val

我还将您的CASE语句转换为枢轴。这将对匹配的计数/金额集进行分组(即,如果有两个数量为100的“ aa”行,将以数量= 100,计数= 2返回),这就是为什么我将两者相乘得到正确的金额。