我试图以多种方式重新编写此查询,并且我不断收到不同的错误。基本上,我试图根据"何时"加入选项A或B?案例陈述的一部分是真实的。当我不使用案例stmt时,它工作正常。
案件的原因是我们在星期日从2a-8a停机。所以,我需要考虑那个时间并从delivery_time中减去它。
这是我得到的错误:
ORA-00905:缺少关键字 00905. 00000 - "缺少关键字"
以下是无效的查询:
SELECT distinct minutes, COUNT(ct.lni) OVER (PARTITION BY minutes) lni_count
FROM card_track ct
INNER JOIN (
select '00) -1000000->-6001' minutes, -1000000 rng_start, -6001 rng_stop from dual
union (select '01a) -6000->-5001' minutes, -6000 rng_start, -5001 rng_stop from dual)
union (select '01b) -5000->-4001' minutes, -5000 rng_start, -4001 rng_stop from dual)
union (select '01c) -4000->-3001' minutes, -4000 rng_start, -3001 rng_stop from dual)
union (select '01d) -3000->-2501' minutes, -3000 rng_start, -2501 rng_stop from dual)
union (select '01e) -2500->-2001' minutes, -2500 rng_start, -2001 rng_stop from dual)
union (select '01f) -2000->-1001' minutes, -2000 rng_start, -1001 rng_stop from dual)
union (select '02) -1000->-501' minutes, -1000 rng_start, -501 rng_stop from dual)
union (select '03) -500->-201' minutes, -500 rng_start, -201 rng_stop from dual)
union (select '04) -200->-101' minutes, -200 rng_start, -101 rng_stop from dual)
union (select '05) -100->0' minutes, -100 rng_start, 0 rng_stop from dual)
union (select '06) 1->50' minutes, 1 rng_start, 50 rng_stop from dual)
union (select '07) 51->100' minutes, 51 rng_start, 100 rng_stop from dual)
union (select '08) 101->150' minutes, 101 rng_start, 150 rng_stop from dual)
union (select '09) 151->200' minutes, 151 rng_start, 200 rng_stop from dual)
union (select '10) 201->250' minutes, 201 rng_start, 250 rng_stop from dual)
union (select '11) 251->300' minutes, 251 rng_start, 300 rng_stop from dual)
union (select '12) 301->350' minutes, 301 rng_start, 350 rng_stop from dual)
union (select '13) 351->400' minutes, 351 rng_start, 400 rng_stop from dual)
union (select '14) 400->500' minutes, 401 rng_start, 500 rng_stop from dual)
union (select '15) 500->600' minutes, 501 rng_start, 600 rng_stop from dual)
union (select '601+' minutes, 601 rng_start, 1000000 rng_stop from dual)
) ON
case
when ((to_char(ct.delivery_time,'d')=1)
and (to_char(ct.delivery_time,'HH24')>=8 )
and ((to_char(ct.workflow_time,'d')=1)
and (to_char(ct.workflow_time,'HH24')<2)
or (to_char(ct.workflow_time,'d')=7) ))
then
to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999')
BETWEEN nvl(rng_start, to_char((ct.delivery_time-(6/24)-ct.la_promote_time)*24*60,'999999'))
AND nvl(rng_stop, to_char((ct.delivery_time-(6/24)-ct.la_promote_time)*24*60,'999999'))
else
to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999')
BETWEEN nvl(rng_start, to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999'))
AND nvl(rng_stop, to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999'))
end
where trunc(ct.delivery_time) = trunc(to_date('06-MAR-16'))
and ct.la_promote_time is not null
and ct.delivery_time is not null
and ct.card_status_no in (2,3)
and batch_type in (0,1)
ORDER BY minutes;
答案 0 :(得分:1)
我希望您的问题是不为子查询提供别名。
SELECT distinct minutes, COUNT(ct.lni) OVER (PARTITION BY minutes) lni_count
FROM card_track ct
INNER JOIN (
select '00) -1000000->-6001' minutes, -1000000 rng_start, -6001 rng_stop from dual
union (select '01a) -6000->-5001' minutes, -6000 rng_start, -5001 rng_stop from dual)
union (select '01b) -5000->-4001' minutes, -5000 rng_start, -4001 rng_stop from dual)
union (select '01c) -4000->-3001' minutes, -4000 rng_start, -3001 rng_stop from dual)
union (select '01d) -3000->-2501' minutes, -3000 rng_start, -2501 rng_stop from dual)
union (select '01e) -2500->-2001' minutes, -2500 rng_start, -2001 rng_stop from dual)
union (select '01f) -2000->-1001' minutes, -2000 rng_start, -1001 rng_stop from dual)
union (select '02) -1000->-501' minutes, -1000 rng_start, -501 rng_stop from dual)
union (select '03) -500->-201' minutes, -500 rng_start, -201 rng_stop from dual)
union (select '04) -200->-101' minutes, -200 rng_start, -101 rng_stop from dual)
union (select '05) -100->0' minutes, -100 rng_start, 0 rng_stop from dual)
union (select '06) 1->50' minutes, 1 rng_start, 50 rng_stop from dual)
union (select '07) 51->100' minutes, 51 rng_start, 100 rng_stop from dual)
union (select '08) 101->150' minutes, 101 rng_start, 150 rng_stop from dual)
union (select '09) 151->200' minutes, 151 rng_start, 200 rng_stop from dual)
union (select '10) 201->250' minutes, 201 rng_start, 250 rng_stop from dual)
union (select '11) 251->300' minutes, 251 rng_start, 300 rng_stop from dual)
union (select '12) 301->350' minutes, 301 rng_start, 350 rng_stop from dual)
union (select '13) 351->400' minutes, 351 rng_start, 400 rng_stop from dual)
union (select '14) 400->500' minutes, 401 rng_start, 500 rng_stop from dual)
union (select '15) 500->600' minutes, 501 rng_start, 600 rng_stop from dual)
union (select '601+' minutes, 601 rng_start, 1000000 rng_stop from dual)
) x ON -- Add the alias here before the ON
case
when ((to_char(ct.delivery_time,'d')=1)
and (to_char(ct.delivery_time,'HH24')>=8 )
and ((to_char(ct.workflow_time,'d')=1)
and (to_char(ct.workflow_time,'HH24')<2)
or (to_char(ct.workflow_time,'d')=7) ))
then
to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999')
BETWEEN nvl(rng_start, to_char((ct.delivery_time-(6/24)-ct.la_promote_time)*24*60,'999999'))
AND nvl(rng_stop, to_char((ct.delivery_time-(6/24)-ct.la_promote_time)*24*60,'999999'))
else
to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999')
BETWEEN nvl(rng_start, to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999'))
AND nvl(rng_stop, to_char((ct.delivery_time-ct.la_promote_time)*24*60,'999999'))
end
where trunc(ct.delivery_time) = trunc(to_date('06-MAR-16'))
and ct.la_promote_time is not null
and ct.delivery_time is not null
and ct.card_status_no in (2,3)
and batch_type in (0,1)
ORDER BY minutes;
答案 1 :(得分:0)
就像Kordirko所说,你的ON条款可以更简单。
ON trunc((ct.delivery_time-ct.la_promote_time)* 24 * 60)BETWEEN rng_start和rng_stop / *你比较整数,不需要转换为字符串* /
等等。
但这就是你想要的吗?