使用 on 条件过滤同一列两次的行
Filter rows using on conditions twice the same column
我有下一个查询:
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 使用最后一个值过滤器,按通道分组。此示例的结果必须如下所示:
只有两行。
我试过:
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
我该怎么办?
此致
在 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;
我有下一个查询:
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 使用最后一个值过滤器,按通道分组。此示例的结果必须如下所示:
只有两行。
我试过:
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
我该怎么办?
此致
在 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;