两表关联查询

thinking_chou 2009-09-14 11:18:13
A表
部门编号(cid) 所属状态(state)
57 0
57 0
57 2
29 1
58 1
59 3
……..
B表
部门名称(name) 父节点(fid) 区域(area) 部门编号(cid)
办公室 -100 1 3
办公室A 44 1 57
办公室B 44 1 58
人事部 -100 6 8
人事部A 36 6 29
人事部B 36 6 59
。。。
查询统计结果为
部门名称(name) 状态0数量 状态1数量 状态2数量 状态3数量
办公室 0 0 0 0
办公室A 0 0 1 0
办公室B 0 1 0 0
人事部 0 1 0 1
说明:部门编号连接两张表,根据区域统计出各种状态的数量(就是部门编号为29,59的状态数据都属于区域6),然后把区域中父节点为-100的作为查询出数据的部门名称
也就是统计同一区域内(例外:办公室都属于区域1,按照部门单独统计) 部门 所属状态的个数
数据库为ORCLE 10g ….
...全文
456 44 打赏 收藏 转发到动态 举报
写回复
用AI写文章
44 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
在group处提示缺失关键字,其实我现在添加一个 like month = ‘%08%’,就可以
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 thinking_chou 的回复:]
谢谢你,我的意思是先按照月份把数据查出来,在统计
select  case max(b.area) when 1 then max(b.name)
    else max(decode(b.fid,-100,b.name))end name,
  count(decode(a.state,0,1))状态0数量,
  count(decode(a.state,1,1))状态1数量,
  count(decode(a.state,2,1))状态2数量,
  count(decode(a.state,3,1))状态3数量
from aa a right join bb b
on a.cid=b.cid
group by decode(b.area,1,b.name,b.area)
order by name
就想在在from aa 后面添加一句 where a.month like '%08%'.
我在on a.cid=b.cid后面添加了where a.month like '%08%'后,区域不为1的name值为空。
[/Quote]
因为你查出来的结果里没有父节点值为-100的记录吧
试试这个
select  case max(b.area) when 1 then max(b.name) 
else max(c.name) end name,
count(decode(a.state,0,1))状态0数量,
count(decode(a.state,1,1))状态1数量,
count(decode(a.state,2,1))状态2数量,
count(decode(a.state,3,1))状态3数量
from aa a inner join bb b
inner join (select name,area from bb where fid=-100)c
on a.cid=b.cid and b.area=c.area
group by decode(b.area,1,b.name,b.area)
order by name
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
谢谢你,我的意思是先按照月份把数据查出来,在统计
select case max(b.area) when 1 then max(b.name)
else max(decode(b.fid,-100,b.name))end name,
count(decode(a.state,0,1))状态0数量,
count(decode(a.state,1,1))状态1数量,
count(decode(a.state,2,1))状态2数量,
count(decode(a.state,3,1))状态3数量
from aa a right join bb b
on a.cid=b.cid
group by decode(b.area,1,b.name,b.area)
order by name
就想在在from aa 后面添加一句 where a.month like '%08%'.
我在on a.cid=b.cid后面添加了where a.month like '%08%'后,区域不为1的name值为空。
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
这个语句基于a表中并非每个月都有包含所有部门编号的记录的情况
你的表看起来是这样的,如果a表中每个月都包含所有部门编号的话,就简单多了
select a.month,case max(b.area) when 1 then max(b.name)
else max(decode(b.fid,-100,b.name))end name,
count(decode(a.state,0,1))状态0数量,
count(decode(a.state,1,1))状态1数量,
count(decode(a.state,2,1))状态2数量,
count(decode(a.state,3,1))状态3数量
from aa a inner join bb b
on a.cid=b.cid
where a.month>=:startmonth and a.month<=:endmonth
group by a.month,decode(b.area,1,b.name,b.area)
order by a.month,name
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 thinking_chou 的回复:]
测试了一下,基本好着,就是多出来了一条空白数据(无所谓),wildwave,在求你一点,如果a表中有个月份字段,我根据传入的月份查询每个月的统计数据,在你的语句中该如何???再次拜谢
[/Quote]
根据传入的月份查询每个月还是单月?如果传入的是一个月份范围的话(假设其为yyyymm的字符串格式)
select b.month,case max(b.area) when 1 then max(b.name)
else max(decode(b.fid,-100,b.name))end name,
count(decode(a.state,0,1))状态0数量,
count(decode(a.state,1,1))状态1数量,
count(decode(a.state,2,1))状态2数量,
count(decode(a.state,3,1))状态3数量
from aa a right join (
select bb.*,cc.month from bb,(
select add_months(to_date(:startmonth,'yyyymm'),rownum-1)month
from dual
connect by rownum<=months_between(to_date(:endmonth,'yyyymm'),
to_date(:startmonth,'yyyymm'))+1) b
on a.cid=b.cid
and to_date(a.month,'yyyymm')=b.month
group by b.month,decode(b.area,1,b.name,b.area)
order by b.month,name
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 42 楼 inthirties 的回复:]
引用 23 楼 inthirties 的回复:
试试这个

select   c.pcname "部门名称",
        count(decode(a.state,0,1)) "状态0数量",
        count(decode(a.state,1,1)) "状态1数量",
        count(decode(a.state,2,1)) "状态2数量",
        count(decode(a.state,3,1)) "状态3数量"
from    a,
       (select b1.*, case when b1.area = 1 then b1.name when b1.fid=-100 then b1. name else select b2.name from b b2 where b2.area=b1.area and b2.area=-100 where rownum  < 1 end pcname from b b1) c

where c.cid=a.cid(+)
group by c.pcname


看看这个行不行。
[/Quote]
我用了后报错
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
恩。好的,我试试
inthirties 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 inthirties 的回复:]
试试这个

select  c.pcname "部门名称",
        count(decode(a.state,0,1)) "状态0数量",
        count(decode(a.state,1,1)) "状态1数量",
        count(decode(a.state,2,1)) "状态2数量",
        count(decode(a.state,3,1)) "状态3数量"
from    a,
      (select b1.*, case when b1.area = 1 then b1.name when b1.fid=-100 then b1. name else select b2.name from b b2 where b2.area=b1.area and b2.area=-100 where rownum < 1 end pcname from b b1) c

where c.cid=a.cid(+)
group by c.pcname
[/Quote]

看看这个行不行。
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
结贴
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
数据也检查了。都没问题。也不知道哪里出错了。怎么会有个5呢?
算了,也辛苦你了。我先结贴了。你在帮我看看
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
那么你检查下原表的数据
我用你的例子做测试,正常(没加month字段)
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
我完全用的你写的。
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 thinking_chou 的回复:]
状态0  状态1    状态2  状态3
5 0 0 0
0 0 0 1
5 0 0 0
5 0 0 0
4 0 1 0
0 5 0 0
5 0 0 0
5 0 0 0
0 1 0 0

这里的5都应该是1
[/Quote]
...你是不是用了nvl()函数和外连接,将空置转换成0了
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
状态0 状态1 状态2 状态3
5 0 0 0
0 0 0 1
5 0 0 0
5 0 0 0
4 0 1 0
0 5 0 0
5 0 0 0
5 0 0 0
0 1 0 0

这里的5都应该是1
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
啊,统计为1的地方变成5是什么意思..
1指的是区域还是状态,什么变成5了啊..
thinking_chou 2009-09-15
  • 打赏
  • 举报
回复
用了这个语句,统计为1的地方都变成5了,其他都正常
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
晕,粗心了
select case max(b.area) when 1 then max(b.name)
else max(c.name) end name,
count(decode(a.state,0,1))状态0数量,
count(decode(a.state,1,1))状态1数量,
count(decode(a.state,2,1))状态2数量,
count(decode(a.state,3,1))状态3数量
from aa a, bb b
, (select name,area from bb where fid=-100)c
where b.area=c.area
and a.cid=b.cid
and a.month like '%08%'
group by decode(b.area,1,b.name,b.area)
order by name
小灰狼W 2009-09-15
  • 打赏
  • 举报
回复
啊,不会吧
添加and a.month like '%08%'
结果正确了吗
thinking_chou 2009-09-14
  • 打赏
  • 举报
回复
回复一楼,也要统计的。
小灰狼W 2009-09-14
  • 打赏
  • 举报
回复
忘了加b.name字段。楼主补上
是这个意思吗?
加载更多回复(24)

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧