我有一个简化的表myowner.mydable,至少列为order_nr,the_days,我是一个多级选择查询
select * from (
select
sum(..)函数在内部选择查询中不起作用。如何在内部选择中使用函数???
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",--this is a line 4
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
union all
然后我得到另一个order_nr
select
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
) as t2
选择(.....)作为。但选择((.....)为t union all(.....)为t),因为t2给出错误:
Could not execute statement.
Feature, scalar value subquery (at line 4) outside of a top level SELECT
list, is not supported.
-- (opt_Select.cxx 2238)
SQLCODE=-1001030, ODBC 3 State="HY000"
Line 1, column 1
如何摆脱错误?
答案 0 :(得分:1)
如果您经常需要执行此查询,可以尝试根据此查询创建视图并使用它。
此外,您的子查询看起来很奇怪。这样:
select
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
等于:
select sum(the_days), order_nr from myowner.mydable where order_nr = '5276751'
group by order_nr
UPD 完整查询您可以这样转换:
select t.category, t.def_or_indef, t.book_date, t.book_code, t.revenue,
t2.conf_nr,
t2.mydays,
t.revenue / t2.mydays
from (select category, def_or_indef, book_date, book_code,
revenue,
confnr as conf_nr
from (select CASE WHEN status = 'status1' OR status = 'status2' THEN 'category1' ELSE 'category2' END AS category,
CASE WHEN status = 'status1' OR status = 'status2' THEN 'definite' ELSE 'indefinite' END AS def_or_indef,
book_date,
book_code,
revenue,
conf_nr as confnr
from myowner.mydable
WHERE days > 0
AND book_date IS NOT NULL
AND DATE(dateadd(year,-1,dateadd(day,-day(today()),today()))) < book_date
AND book_date <= date(dateadd(day,-day(today()),today()))) as t
union all
select .. from ( select .. WHERE .. AND DATE(dateadd(day,-day(today()),today())) < resv_date and resv_date < today()
) as t,
(select conf_nr, sum(days) as mydays
from myowner.mydable
group by conf_nr) t2
答案 1 :(得分:0)
我提出了一个真实的问题:
select * from (
select
category, def_or_indef, book_date, book_code,
(select sum(days) from myowner.mydable where conf_nr=confnr) as mydays,
revenue, confnr as conf_nr,
revenue / (select sum(days) from myowner.mydable where conf_nr=confnr) as adr
from (
select
CASE WHEN status = 'status1' OR status = 'status2' THEN 'category1' ELSE 'category2' END AS category,
CASE WHEN status = 'status1' OR status = 'status2' THEN 'definite' ELSE 'indefinite' END AS def_or_indef,
book_date, book_code, revenue, conf_nr as confnr
from myowner.mydable
WHERE days > 0 AND book_date IS NOT NULL AND
DATE(dateadd(year,-1,dateadd(day,-day(today()),today()))) < book_date AND book_date <= date(dateadd(day,-day(today()),today()))
) as t
union all
select .. from ( select .. WHERE .. AND
DATE(dateadd(day,-day(today()),today())) < resv_date and resv_date < today()
) as t
) as t2
你能给予同等的工作吗?