【问题标题】:Filter rows using on conditions twice the same column使用同一列的两次条件过滤行
【发布时间】:2020-10-08 15:28:19
【问题描述】:

我有下一个问题:

        select CHANNEL , my_date 
        from table_1 d
        where source_data = 'test_5'
          and my_date < to_date('27/09/2020','DD/MM/YYYY')
          and customer_ID = :param_customer_ID
        order by d.my_date asc;

这将显示下一个结果:

我的需要是。有最后一个 my_date 的最后一个 vale 过滤器,按频道分组。我对这个示例的结果必须如下所示:

只有两行。

我试过了:

select CHANNEL , my_date 
from table_1 d
where source_data = 'test_5'
  and (my_date < to_date('27/09/2020','DD/MM/YYYY') and my_date = max(my_date))
  and customer_ID = :param_customer_ID
group by CHANNEL, my_date 
order by d.my_date asc;

但是没有,它不起作用,并给我错误

ORA-00934: función de grupo no permitida aquí
00934. 00000 -  "group function is not allowed here"
*Cause:    
*Action:
Error en la línea: 138, columna: 30

我该怎么办?

问候

【问题讨论】:

    标签: sql plsql oracle-sqldeveloper oracle19c


    【解决方案1】:

    在 Oracle 中,您可以使用聚合:

    select channel, max(my_date)
    from table_1 d
    where source_data = 'test_5' and
          my_date < date '2020-09-27' and
          customer_ID = :param_customer_ID
    group by channel;
    

    如果您需要更多列,请使用row_number()

    select channel, my_date
    from (select d.*,
                 row_number() over (partition by channel order by my_date desc) as seqnum
          from table_1 d
          where source_data = 'test_5' and
                my_date < date '2020-09-27' and
                customer_ID = :param_customer_ID
         ) d
    where seqnum = 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2018-10-05
      相关资源
      最近更新 更多