在 Oracle 中,我们如何获得不在表中的结果中的多个列
In Oracle How do we get multiple columns in result which are not in tables
例如:
我下面 table 名为 "T1"
我需要这样的结果:
if "earlist_run_date" , "last_rundate" & "remainng_run_dates" 其中 table T1 可以使用 PIVOT.
但我不知道如何将这 3 列放入结果集中。任何解决方案将不胜感激
我猜你想要这样的东西。可能有更好的方法来消除 listagg
中我没有想到的第一行和最后一行,但这应该相当有效。
with ranked_t1 as (
select t1.*,
rank() over( partition by job_id
order by run_date asc ) asc_rank,
rank() over( partition by job_id
order by run_date desc ) desc_rank
from t1
)
select job_id,
min( run_date ) earliest_run_date,
max( run_date ) last_rundate,
listagg( (case when asc_rank != 1
and desc_rank != 1
then run_date
else null
end), ' ' )
within group( order by run_date ) remaining_run_dates
from ranked_t1
group by job_id;
删除 remaining_run_dates
列,您得到一个简单的查询
select
JOB_ID,
min(RUN_DATE) as earliest_run_date,
max(RUN_DATE) as last_rundate
from T1
group by JOB_ID
例如:
我下面 table 名为 "T1"
我需要这样的结果:
if "earlist_run_date" , "last_rundate" & "remainng_run_dates" 其中 table T1 可以使用 PIVOT.
但我不知道如何将这 3 列放入结果集中。任何解决方案将不胜感激
我猜你想要这样的东西。可能有更好的方法来消除 listagg
中我没有想到的第一行和最后一行,但这应该相当有效。
with ranked_t1 as (
select t1.*,
rank() over( partition by job_id
order by run_date asc ) asc_rank,
rank() over( partition by job_id
order by run_date desc ) desc_rank
from t1
)
select job_id,
min( run_date ) earliest_run_date,
max( run_date ) last_rundate,
listagg( (case when asc_rank != 1
and desc_rank != 1
then run_date
else null
end), ' ' )
within group( order by run_date ) remaining_run_dates
from ranked_t1
group by job_id;
删除 remaining_run_dates
列,您得到一个简单的查询
select
JOB_ID,
min(RUN_DATE) as earliest_run_date,
max(RUN_DATE) as last_rundate
from T1
group by JOB_ID