2017-04-24 25 views
1

我已經運行下面的查詢,而不問題:ORA-00932 - 預期數量有 -

with Nums (NN) as 
(
select 0 as NN 
from dual 
union all 
select NN+1 -- (1) 
from Nums 
where NN < 30 
) 

select null as errormsg, trunc(sysdate)-NN as the_date, count(id) as the_count 
from Nums 
left join 
(
    SELECT c1.id, trunc(c1.c_date) as c_date 
    FROM table1 c1 
    where c1.c_date > trunc(sysdate) - 30 
    UNION 
    SELECT c2.id, trunc(c2.c_date) 
    FROM table2 c2 
    where c2.c_date > trunc(sysdate) -30 
) x1 
on x1.c_date = trunc(sysdate)-Nums.NN 
group by trunc(sysdate)-Nums.NN 

然而,當我嘗試在SSRS一個進程彈出這個使用方法:

procedure pr_do_the_thing (RefCur out sys_refcursor) 

is 
    oops varchar2(100); 
begin 

open RefCur for 
-- see above query -- 
; 
end pr_do_the_thing; 

我得到

錯誤():PL/SQL:ORA-00932:不一致的數據類型:預期數量有 -

有什麼想法?就像我上面所說的那樣,作爲查詢,沒有問題。作爲一個proc,錯誤出現在note(1)int eh查詢中。

回答

1

這似乎是錯誤18139621(見MOS Doc ID 2003626.1)。有可用的補丁,但如果這是你遇到這種唯一的地方,它可能是簡單的切換到一個分層查詢:

with Nums (NN) as 
(
    select level - 1 
    from dual 
    connect by level <= 31 
) 
... 

你也可以計算出CTE內的日期(也失敗,遞歸CTE):

with Dates (DD) as 
(
    select trunc(sysdate) - level + 1 
    from dual 
    connect by level <= 31 
) 
select null as errormsg, DD as the_date, count(id) as the_count 
from Dates 
left join 
(
    SELECT c1.id, trunc(c1.c_date) as c_date 
    FROM table1 c1 
    where c1.c_date > trunc(sysdate) - 30 
    UNION 
    SELECT c2.id, trunc(c2.c_date) 
    FROM table2 c2 
    where c2.c_date > trunc(sysdate) -30 
) x1 
on x1.c_date = DD 
group by DD; 

我可能會組織它略有不同,所以子查詢不直接限制日期範圍:

with dates (dd) as 
(
    select trunc(sysdate) - level + 1 
    from dual 
    connect by level <= 31 
) 
select errormsg, the_date, count(id) as the_count 
from (
    select null as errormsg, d.dd as the_date, c1.id 
    from dates d 
    left join table1 c1 on c1.c_date >= d.dd and c1.c_date < d.dd + 1 
    union all 
    select null as errormsg, d.dd as the_date, c2.id 
    from dates d 
    left join table2 c2 on c2.c_date >= d.dd and c2.c_date < d.dd + 1 
) 
group by errormsg, the_date; 

但總是與這些東西,檢查每個方法的性能...

還要注意,我已經關掉從unionunion all。如果一個ID可能在同一天,同一個表中或兩個表中出現多次,那麼計數將會不同 - 您需要決定是否要計數它們的次數或出現次數。這也適用於您的原始查詢。