select a.order_quantity, a.deptname, a.status
from (
select count(mv.id) order_quantity, hr.deptname, 'exception' status
from hr_dept hr
left join mrp_wo_v mv
on mv.Manufact_Hr_dept_id = hr.id
and mv.plan_end_date > sysdate
and substr(mv.wo_date_str, 0, 4) = '2017'
and substr(mv.wo_date_str, 5, 2) = '12'
group by hr.deptname
union all
select count(mv.id) order_quantity, hr.deptname, 'normal' status
from hr_dept hr
left join mrp_wo_v mv
on mv.Manufact_Hr_dept_id = hr.id
and mv.plan_end_date < sysdate
and substr(mv.wo_date_str, 0, 4) = '2017'
and substr(mv.wo_date_str, 5, 2) = '12'
group by hr.deptname
) a
order by a.deptname, a.status;

問題:當 mv.plan_end_date 全部小于sysdate時,查不出excepiton的資料了,改怎么改造????要求資料必須成對存在,一個exception匹配一個normal
uj5u.com熱心網友回復:
樓主給下測驗資料,和你的預期結果。方便大家回復你uj5u.com熱心網友回復:
1、當 mv.plan_end_date 全部小于sysdate時,不滿足mv.plan_end_date > sysdate,所以第一個Select肯定取不到資料啊。2、把 and substr(mv.wo_date_str, 0, 4) = '2017' and substr(mv.wo_date_str, 5, 2) = '12' 改為 and mv.wo_date_str like '201712%'會提高效率。
uj5u.com熱心網友回復:
就是想在不滿足大于sysdate的情況下,拼出exception的資料,該怎么改造陳述句能達到一個normal匹配一個exception的陳述句,求指教!
uj5u.com熱心網友回復:
當 mv.plan_end_date 全部小于sysdate時,不滿足mv.plan_end_date > sysdate ,第一個Select肯定取不到資料。
預期的結果如下:
數量 部門 狀態
0 21車間 exception
260 21車間 normal
0 23車間 exception
176 23車間 normal
.....
......
uj5u.com熱心網友回復:
and mv.plan_end_date > sysdateand substr(mv.wo_date_str, 0, 4) = '2017'
and substr(mv.wo_date_str, 5, 2) = '12'
沒有同時符合這三個條件的資料吧、還有
and substr(mv.wo_date_str, 0, 4) = '2017'
and substr(mv.wo_date_str, 5, 2) = '12' 這個條件可以換成to_char(mv.wo_date_str,'yyyy-mm')='2017-12'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/70807.html
標籤:基礎和管理
上一篇:各位大佬,望閑暇時間指點一下
下一篇:求助oracle大神
