0

问题:

我需要按某些值汇总表格上的价格,所以我这样做了

select t.r as score_range, count(*) as number_of_occurences
from (
  select case  
    when publicsellingprice between 0 and 5000 then ' 0-5k'
    when publicsellingprice between 5000 and 10000 then '5-10k'
    when publicsellingprice between 10000 and 20000 then '10-20k'
    when publicsellingprice between 20000 and 400000 then '20-40k'
    when publicsellingprice between 40000 and 80000 then '40-80k'   
    else '80-999K' end as r
  from product) t
group by t.r
order by 1

这没问题。如果我仅使用 5-k 的值对此进行测试,则仅显示该行;我想要的是始终拥有完整的 5 行数据,并且当给定条件没有值时,我想要 0 或 null 或其他值。我怎样才能做到这一点?

4

2 回答 2

2

你需要一个左外连接。最简单的方法(IMO)是使用子查询分解,AKA 一个公共表表达式,来提供范围信息,然后将其连接到您的产品表:

with ranges as (
  select 0 as min_price, 5000 as max_price, ' 0-5k' as score_range from dual
  union all select 5000, 10000, '5-10k' from dual
  union all select 10000, 20000, '10-20k' from dual
  union all select 20000, 40000, '20-40k' from dual
  union all select 40000, 80000, '40-80k' from dual
  union all select 80000, 999000, '80-999k' from dual
)
select r.score_range, count(publicsellingprice) as number_of_occurences
from ranges r
left join product p on p.publicsellingprice between r.min_price and r.max_price
group by r.score_range, r.min_price
order by r.min_price;

但是,使用between可能不会给您想要的结果;恰好在边界上的东西,例如在哪里publicsellingprice5000将被计入两个桶中。您可以调整 CTE 中的最低/最高价格以给出一个上限(例如4999.99),或者以不同的方式对它们进行比较:

with ranges as (
  select 0 as min_price, 5000 as max_price, ' 0-5k' as score_range from dual
  union all select 5000, 10000, '5-10k' from dual
  union all select 10000, 20000, '10-20k' from dual
  union all select 20000, 40000, '20-40k' from dual
  union all select 40000, 80000, '40-80k' from dual
  union all select 80000, 999000, '80-999k' from dual
)
select r.score_range, count(publicsellingprice) as number_of_occurences
from ranges r
left join product p on p.publicsellingprice >= r.min_price
  and p.publicsellingprice < r.max_price
group by r.score_range, r.min_price
order by r.min_price;

在这两种情况下,我都将其包含min_price在其中,group by因此您也可以按此订购;否则,您将按字符串比较规则进行排序,这将使您的 5k 括号介于 40k 和 80k 之间。

于 2013-07-09T15:00:58.893 回答
0

试试这个查询,只需添加一些虚构的行来与真实数据求和:

select res.score_range, sum(res.number_of_occurences) number_of_occurences FROM
(select ' 0-5k' score_range, 0 number_of_occurences from dual
union
select '5-10k' score_range, 0 number_of_occurences from dual
union
select '10-20k' score_range, 0 number_of_occurences from dual
union
select '20-40k' score_range, 0 number_of_occurences from dual
union
select '40-80k' score_range, 0 number_of_occurences from dual
union
select t.r as score_range, count(*) as number_of_occurences
from (
  select case  
    when publicsellingprice between 0 and 5000 then ' 0-5k'
    when publicsellingprice between 5000 and 10000 then '5-10k'
    when publicsellingprice between 10000 and 20000 then '10-20k'
    when publicsellingprice between 20000 and 400000 then '20-40k'
    when publicsellingprice between 40000 and 80000 then '40-80k'   
    else '80-999K' end as r
  from product) t
group by t.r
order by 1) res
group by res.score_range
于 2013-07-09T15:00:29.363 回答