求一sql

liukang1022 2010-12-09 06:24:32
有一表如下:
columnA columnAB columnC
a1 2 b1
a1 2 b2
a1 2 b3
a1 2 b4
a1 2 b5
b1 0 c1
b2 0 c2
b3 0 c3
b4 0 c4
b5 0 c5
b6 0 c6
a2 2 b1
a2 2 b2
a2 2 b4
a3 2 b1
a3 2 b2
a3 2 b3
a3 2 b4
现有一需求:
1、给N个columnC,同过columnB=0,找到对应的columnA;
2、再通过找到的columnA=columnC和columnB=2,找到columnA;
例如:给定的columnC IN (c1,c2,c3,c4),找到a3。

注:columnC的c1,c2,c3,c4,c5,c6可能和b1,b2,b3相等。

请问,这个语句该怎么写?先在这谢谢各位了
...全文
110 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
心中的彩虹 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 liuk1022 的回复:]
引用 10 楼 zhuomingwang 的回复:
1、给N个columnC,同过columnB=0,找到对应的columnA;
2、再通过找到的columnA=columnC和columnB=2,找到columnA;
例如:给定的columnC IN (c1,c2,c3,c4),找到a3
你这个例子给的结果不到啊
columnC IN (c1,c2,c3,c4) columnB=0 找……
[/Quote]


SQL> with tb as(
2 select 'a1' columnA, '2' columnB, 'b1' columnC from dual
3 union all
4 select 'a1' columnA, '2' columnB, 'b2' columnC from dual
5 union all
6 select 'a1' columnA, '2' columnB, 'b3' columnC from dual
7 union all
8 select 'a1' columnA, '2' columnB, 'b4' columnC from dual
9 union all
10 select 'a1' columnA, '2' columnB, 'b5' columnC from dual
11 union all
12 select 'b1' columnA, '0' columnB, 'c1' columnC from dual
13 union all
14 select 'b2' columnA, '0' columnB, 'c2' columnC from dual
15 union all
16 select 'b3' columnA, '0' columnB, 'c3' columnC from dual
17 union all
18 select 'b4' columnA, '0' columnB, 'c4' columnC from dual
19 union all
20 select 'a3' columnA, '2' columnB, 'b1' columnC from dual
21 union all
22 select 'a3' columnA, '2' columnB, 'b2' columnC from dual
23 union all
24 select 'a3' columnA, '2' columnB, 'b3' columnC from dual
25 union all
26 select 'a3' columnA, '2' columnB, 'b4' columnC from dual)
27 select a.columnA from tb a,(select columnA from tb where columnC IN ('c1','c2','c3','c4') and columnB=0) b
28 where a.columnB=2 and a.columnC=b.columnA(+)
29 group by a.columnA
30 having count(*)=4
31 /

COLUMNA
-------
a3




心中的彩虹 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 liuk1022 的回复:]
有一表如下:
columnA columnAB columnC
a1 2 b1
a1 2 b2
a1 2 b3
a1 2 b4
a1 2 b5
b1 0 c1
b2 0 c2
b3 0 c3
b4 0 c4
b5 0 c5
b6 0 c6
a2 2 b1
a2 2 b2
a2 2 b4
a3 2 b1
a3 2 b2
a3 2 b3
a3 2 b4
现有一需……
[/Quote]

没描述清楚 你这样就全部是a1,a3
gelyon 2010-12-09
  • 打赏
  • 举报
回复

--上面少了别名a:
select a.cola from (
select cola,colb,wm_concat(colc)over(order by colc) colc
from table_name
group by cola,colb
) a
where where a.colb=2
and a.colc=(
select wm_concat(cola)over(order by colc) from table_nale
where colb=0 and colc in('c1','c2','c3','c4')
)
gelyon 2010-12-09
  • 打赏
  • 举报
回复

--试试:
select a.cola from (
select cola,colb,wm_concat(colc)over(order by colc) colc
from table_name
group by cola,colb
)
where where a.colb=2
and a.colc=(
select wm_concat(cola)over(order by colc) from table_nale
where colb=0 and colc in('c1','c2','c3','c4')
)
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zhuomingwang 的回复:]
SQL code

scott@YPCOST> ed
已写入 file afiedt.buf

1 with tb as(
2 select 'a1' columnA,2 columnB,'b1' columnC from dual union all
3 select 'a1', 2, 'b1' from dual union all
4 select '……
[/Quote]
不行,还是把a1也选出来了,先谢谢各位帮忙
  • 打赏
  • 举报
回复

scott@YPCOST> ed
已写入 file afiedt.buf

1 with tb as(
2 select 'a1' columnA,2 columnB,'b1' columnC from dual union all
3 select 'a1', 2, 'b1' from dual union all
4 select 'a1', 2, 'b2' from dual union all
5 select 'a1', 2, 'b3' from dual union all
6 select 'a1', 2, 'b4' from dual union all
7 select 'a1', 2, 'b5' from dual union all
8 select 'b1', 0, 'c1' from dual union all
9 select 'b2', 0, 'c2' from dual union all
10 select 'b3', 0, 'c3' from dual union all
11 select 'b4', 0, 'c4' from dual union all
12 select 'b5', 0, 'c5' from dual union all
13 select 'b6', 0, 'c6' from dual union all
14 select 'a2', 2, 'b1' from dual union all
15 select 'a2', 2, 'b2' from dual union all
16 select 'a2', 2, 'b4' from dual union all
17 select 'a3', 2, 'b1' from dual union all
18 select 'a3', 2, 'b2' from dual union all
19 select 'a3', 2, 'b3' from dual union all
20 select 'a3', 2, 'b4' from dual)
21 select columnA
22 from(select columnA,wm_concat(columnC) over (partition by columnA order by columnC) rn
23 from tb
24 where columnB=2)
25 where rn=
26 (
27 select wm_concat(t1.columnA) from tb t1
28* where t1.columnC in('c1','c2','c3','c4'))
scott@YPCOST> /

CO
--
a3
  • 打赏
  • 举报
回复
--试试这个可以不?
select t.columnA,wm_concat(t.columnC)
from tb
where t.columnB=2
group by t.columnA
having wm_concat(columnC)=
(
select wm_concat(t1.columnA) from tb t1
where t1.columnC in('c1','c2','c3','c4'))
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 java3344520 的回复:]
SQL code
select columnA
from (
select columnA,count(partition by columnB,columnC order by columnA) as cnt
from table t,
(select columnA
from table
where columnB = 0
and colu……
[/Quote]
这个是能得到结果,但是不太懂select a_1,count(*) over (partition by b_1,c_1 order by a_1) as cnt
from lk_123 t这一句,能否帮忙解释下哈
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhuomingwang 的回复:]
1、给N个columnC,同过columnB=0,找到对应的columnA;
2、再通过找到的columnA=columnC和columnB=2,找到columnA;
例如:给定的columnC IN (c1,c2,c3,c4),找到a3
你这个例子给的结果不到啊
columnC IN (c1,c2,c3,c4) columnB=0 找到的columnA有b1,b2,b3,b4
在通过 ……
[/Quote]

不会啊,只会找到a3啊,a3对应的columnC刚好等于b1,b2,b3,b4
  • 打赏
  • 举报
回复
1、给N个columnC,同过columnB=0,找到对应的columnA;
2、再通过找到的columnA=columnC和columnB=2,找到columnA;
例如:给定的columnC IN (c1,c2,c3,c4),找到a3
你这个例子给的结果不到啊
columnC IN (c1,c2,c3,c4) columnB=0 找到的columnA有b1,b2,b3,b4
在通过 找到的columnA有b1,b2,b3,b4 =columnC 和columnB 找到的columnA有 a1,a2,a3
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hong1987 的回复:]
with t as(
select 'a1' columnA, '2' columnB, 'b1' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b2' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b3' c……
[/Quote]

不行,这样把a1也选出来了,我要的有且只有columnC IN (c1,c2,c3,c4)4个
iqlife 2010-12-09
  • 打赏
  • 举报
回复
select columnA
from (
select columnA,count(*) over (partition by columnB,columnC order by columnA) as cnt
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
)
where cnt=1


晕倒,鼠标动了下,删除了些,应该是上面这个
iqlife 2010-12-09
  • 打赏
  • 举报
回复
select columnA
from (
select columnA,count(partition by columnB,columnC order by columnA) as cnt
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
)
where cnt=1

这样试试
hong1987 2010-12-09
  • 打赏
  • 举报
回复

with t as(
select 'a1' columnA, '2' columnB, 'b1' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b2' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b3' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b4' columnC from dual
union all
select 'a1' columnA, '2' columnB, 'b5' columnC from dual
union all
select 'b1' columnA, '0' columnB, 'c1' columnC from dual
union all
select 'b2' columnA, '0' columnB, 'c2' columnC from dual
union all
select 'b3' columnA, '0' columnB, 'c3' columnC from dual
union all
select 'b4' columnA, '0' columnB, 'c4' columnC from dual
union all
select 'a3' columnA, '2' columnB, 'b1' columnC from dual
union all
select 'a3' columnA, '2' columnB, 'b2' columnC from dual
union all
select 'a3' columnA, '2' columnB, 'b3' columnC from dual
union all
select 'a3' columnA, '2' columnB, 'b4' columnC from dual)
select t.columnA
from t,
(select columnA
from t
where columnB = 0
and columnC in ('c1', 'c2', 'c3', 'c4')) ta
where t.columnB = 2
and t.columnC = ta.columnA
group by t.columnB, t.columnA
iqlife 2010-12-09
  • 打赏
  • 举报
回复
select columnA
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
group by t.columnB,t.columnA
having count(*)=1

取出按A,B列分组得到的只有一条记录的啊,从你要的结果来看是这样的
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 java3344520 的回复:]
SQL code
select columnA
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
group ……
[/Quote]
试了下不行的,运行就报错了,
group by t.columnB,t.columnC
having count(*)=1 这一句看不懂是什么意思啊
iqlife 2010-12-09
  • 打赏
  • 举报
回复
select columnA
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
group by t.columnB,t.columnC
having count(*)=1
liukang1022 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gjswxhb 的回复:]
SQL code

select columnA
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA
[/Quote]

这样不行的,a1,a2,a3全部选出来了,我只要有且只有columnC IN (c1,c2,c3,c4)4个值的
物润声无 2010-12-09
  • 打赏
  • 举报
回复

select columnA
from table t,
(select columnA
from table
where columnB = 0
and columnC in (c1,c2,c3,c4)
) ta
where t.columnB = 2
and t.columnC = ta.columnA

17,089

社区成员

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

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