简体   繁体   English

在 oracle 的 sql 查询中替换“过滤器”

[英]substituting "filter" in a sql query on oracle

We have a table with data that has one date column indicating what day the data is for ("planning_day") and another column for logging when the data was sent ("first_sent_time").我们有一个数据表,其中一个日期列指示数据的日期(“planning_day”)和另一个用于记录数据发送时间的列(“first_sent_time”)。

I'm trying to make a report showing how far in the past/future we've sent data on which day.我正在尝试制作一份报告,显示过去/未来我们在哪一天发送了数据。 So if today we sent 2 data for yesterday, 5 for today and 1 for the day after tomorrow, the result should be something like this:因此,如果今天我们发送 2 个昨天的数据,5 个今天的数据,1 个后天的数据,结果应该是这样的:

sent_day   minus2 minus1 sameDay plus1 plus2
2021-11-24    0      2      5      0     1
...

I know I could do this in postgres with a query using "filter":我知道我可以在 postgres 中使用“过滤器”进行查询:

select
  trunc(t.first_sent_time),
  count(t.id) filter (where e.planning_day - trunc(e.first_sent_time) = -2) as "minus2",
  count(t.id) filter (where e.planning_day - trunc(e.first_sent_time) = -1) as "minus1",
  count(t.id) filter (where e.planning_day - trunc(e.first_sent_time) = 0)  as "sameDay",
  count(t.id) filter (where e.planning_day - trunc(e.first_sent_time) = 1)  as "plus1",
  count(t.id) filter (where e.planning_day - trunc(e.first_sent_time) = 2)  as "plus2"
from
  my_table t
group by
  trunc(t.first_sent_time)
;

Unfortunately, this "filter" doesn't exist in Oracle.不幸的是,这个“过滤器”在 Oracle 中不存在。 I need help here.我在这里需要帮助。 I tried something like following:我尝试了以下方法:

select 
  sent_day,
  sum(minus2),
  sum(minus1),
  sum(sameDay),
  sum(plus1),
  sum(plus2)
from (
  select 
    *
  from (
    select
      b.id,
      trunc(b.first_sent_time) as sent_day,
      b.planning_day,
      b.planning_day - trunc(b.first_sent_time) as day_diff
    from
      my_table b
    where
      b.first_sent_time >= DATE '2021-11-01'
  )
  pivot (
    count(id) for day_diff in (-2 as "minus2",-1 as "minus1",0 as "sameDay", 1 as "plus1",2 as "plus2")
  )
)
group by
  sent_day
order by
  sent_day
;

but it doesn't work and it feels like I'm going too complicated and there must be an easier solution.但它不起作用,感觉我太复杂了,必须有一个更简单的解决方案。

Use a CASE expression within the aggregation function to simulate the filter .在聚合 function 中使用CASE表达式来模拟filter

Here a simplified example这里是一个简化的例子

with dt as (
select 1 id , 1 diff_days from dual union all
select 2 id , 1 diff_days from dual union all
select 3 id , -1 diff_days from dual union all
select 4 id , -1 diff_days from dual union all
select 4 id , -1 diff_days from dual)
/* query */
select 
  count(case when diff_days = 1 then id end) as cnt_1,
  count(case when diff_days = -1 then id end) as cnt_minus_1
from dt;

results in结果是

     CNT_1 CNT_MINUS_1
---------- -----------
         2           3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM