原帖的解决方案创意不错哟, 出错主要是c.部门+'%', oracle用||连接字符根据他的创意,我也写了个类似的。SQL> select * from dept_t; ID NAME ---------- ------------- 0 分公司1 1 分公司1财务部 2 分公司1管理处 3 分公司2 4 分公司2财务部 5 分公司2管理处SQL> select * from dept_income_t; 部门 收入 ---------- ---------- 1 300 1 500 2 400 4 300 5 100 5 500用这个试试 SQL>select a.name, sum(收入) from dept_t a left join (select a.*, b.收入 from dept_t a, dept_income_t b where a.id=b.部门) b on instr(b.name, a.name)=1 group by a.name, a.id order by a.id;NAME SUM(收入) ------------- ---------- 分公司1 1200 分公司1财务部 800 分公司1管理处 400 分公司2 900 分公司2财务部 300 分公司2管理处 600已选择6行。
为什么在我的plsql中还是出错 ORA-00904: "A"."ID": 标识符无效
你执行的哪个sql呀。 把整个错误信息贴出来看看。
你的dept表和dept_income的结构也贴出来你单独 select a.id from dept_t a看看有没有问题。估计是不是表名和字段名带了引号。
Inthirties老大的sql语句完全正确:select a.name, sum(收入) from dept_t a left join (select a.*, b.收入 from dept_t a, dept_income_t b where a.id=b.部门) b on instr(b.name, a.name)=1 group by a.name, a.id order by a.id; 但是,我的数据量不小,用instr运行起来比较慢(两种表的数据量分别是300和5000,执行语句需要15秒左右),还有更好的方法吗?
看了你的原贴, 如果是我,我会用子查询就完了select 部门, Nvl((select sum(收入) from table2 where 部门 = id),0)as 收入 from table1这样不知道有什么问题???
看错,原来要包括子部门的~~~select 部门, NVl((select sum(收入) from table1, table2 where table1.id = table2.部门 and table1.部门 like temp.部门||'%'),0)as 收入 from table1 temp
出错主要是c.部门+'%', oracle用||连接字符根据他的创意,我也写了个类似的。SQL> select * from dept_t;
ID NAME
---------- -------------
0 分公司1
1 分公司1财务部
2 分公司1管理处
3 分公司2
4 分公司2财务部
5 分公司2管理处SQL> select * from dept_income_t;
部门 收入
---------- ----------
1 300
1 500
2 400
4 300
5 100
5 500用这个试试
SQL>select a.name, sum(收入) from dept_t a left join (select a.*, b.收入 from dept_t a, dept_income_t b where a.id=b.部门) b on instr(b.name, a.name)=1 group by a.name, a.id order by a.id;NAME SUM(收入)
------------- ----------
分公司1 1200
分公司1财务部 800
分公司1管理处 400
分公司2 900
分公司2财务部 300
分公司2管理处 600已选择6行。
ORA-00904: "A"."ID": 标识符无效
select a.id from dept_t a看看有没有问题。估计是不是表名和字段名带了引号。
但是,我的数据量不小,用instr运行起来比较慢(两种表的数据量分别是300和5000,执行语句需要15秒左右),还有更好的方法吗?
from table1这样不知道有什么问题???
from table1 temp