12
返回列表 发新帖
楼主: kangpengjie

[SQL] 请教一个SQL的写法

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
11#
发表于 2017-2-19 20:01 | 只看该作者
lz还需要把期望结果贴出来

使用道具 举报

回复
论坛徽章:
1
复活蛋
日期:2012-05-21 14:39:53
12#
 楼主| 发表于 2017-2-20 08:54 | 只看该作者
重新编辑了一下帖子,期望结果已经贴上了

使用道具 举报

回复
论坛徽章:
40
2014年新春福章
日期:2014-02-18 16:43:09喜羊羊
日期:2015-05-18 16:24:25慢羊羊
日期:2015-06-12 13:08:22暖羊羊
日期:2015-07-02 16:06:20暖羊羊
日期:2015-07-06 16:28:55狮子座
日期:2015-07-29 17:14:43摩羯座
日期:2015-09-02 13:58:47白羊座
日期:2015-09-08 10:39:06天枰座
日期:2015-09-17 21:41:53摩羯座
日期:2015-10-29 21:07:02
13#
发表于 2017-2-20 09:20 | 只看该作者
kangpengjie 发表于 2017-2-20 08:54
重新编辑了一下帖子,期望结果已经贴上了

1、8楼的SQL不满足吗?


使用道具 举报

回复
论坛徽章:
3
ITPUB15周年纪念
日期:2016-10-13 13:15:34秀才
日期:2017-03-20 13:42:20秀才
日期:2017-03-28 15:59:38
14#
发表于 2017-2-20 14:03 | 只看该作者
纯粹为了题目写的,用的fulljoin效率不高,在这个结果上不难写匹配条件。楼主可以试试
select id,listagg(matchstr,',') within group (order by id) str
from (
  select id,item_id ,
  listagg(idmatch,',') within group (order by idmatch) ,
  listagg(matchvalue,',') within group (order by idmatch) matchstr
  from (
  select a.id,a.item_id,a1.id as idmatch,value matchvalue from a full join a1
  on a.item_id = a1.id
  union all
   select a.id,a.item_id,a2.id as idmatch,value matchvalue from a full join a2
  on a.item_id = a2.id
  union all
  select  a.id,a.item_id,a3.id as idmatch,value matchvalue from a full join a3
  on a.item_id = a3.id
  )
  group by id ,item_id
)
group by id

使用道具 举报

回复
论坛徽章:
1
复活蛋
日期:2012-05-21 14:39:53
15#
 楼主| 发表于 2017-2-20 15:57 | 只看该作者
本帖最后由 kangpengjie 于 2017-2-20 15:59 编辑
bfc99 发表于 2017-2-20 09:20
1、8楼的SQL不满足吗?

8楼的SQL满足了。现在是前台传来一个如下这样的表达式
a contains a1 and contains(a2 or a3)  或 a contains a1 or contains(a2 and a3),
我需要根据这个表达式写出对应的SQL语句。这个表达式可能会非常复杂,有很多层的与或关系

使用道具 举报

回复
论坛徽章:
40
2014年新春福章
日期:2014-02-18 16:43:09喜羊羊
日期:2015-05-18 16:24:25慢羊羊
日期:2015-06-12 13:08:22暖羊羊
日期:2015-07-02 16:06:20暖羊羊
日期:2015-07-06 16:28:55狮子座
日期:2015-07-29 17:14:43摩羯座
日期:2015-09-02 13:58:47白羊座
日期:2015-09-08 10:39:06天枰座
日期:2015-09-17 21:41:53摩羯座
日期:2015-10-29 21:07:02
16#
发表于 2017-2-20 19:18 | 只看该作者
kangpengjie 发表于 2017-2-20 15:57
8楼的SQL满足了。现在是前台传来一个如下这样的表达式
a contains a1 and contains(a2 or a3)  或 a con ...

union 相当于你的表达式中的OR,
intersect 相当于你的表达式中的AND

“a contains a1 and contains (a2 or a2)"可以写成如下:
select id
   from a
   where a.item_id in (select id from a1)
intersect   --and
(select id
    from a
    where a.item_id in (select id form a2)
union     --or
  select id
    from a
   where a.item_id in (select id from a3)
);

"a contains a1 or contains(a2 and a3)"可以写成:
select id
   from a
   where a.item_id in (select id from a1)
union              --or
(select id
    from a
    where a.item_id in (select id form a2)
intersect       --and
  select id
    from a
   where a.item_id in (select id from a3)
);

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
17#
发表于 2017-2-21 10:04 | 只看该作者
1.
WITH all_data as (
SELECT 'a1' tname,id,value from a1
union all SELECT 'a2' tname,id,value from a2
union all SELECT 'a3' tname,id,value from a3
)
select a.id
  from a join all_data on a.item_id=all_data.id
group by a.id
having count(case when tname='a1' then 1 end)>0
       and count(case when tname in ('a2','a3') then 1 end)>0
;


2.
WITH all_data as (
SELECT 'a1' tname,id,value from a1
union all SELECT 'a2' tname,id,value from a2
union all SELECT 'a3' tname,id,value from a3
)
select a.id
  from a join all_data on a.item_id=all_data.id
group by a.id
having count(case when tname='a2' then 1 end)>0
       and count(case when tname='a3' then 1 end)>0
;

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
18#
发表于 2017-2-21 10:05 | 只看该作者
解析你的输入条件,构建输出我上面的动态SQL。

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
19#
发表于 2017-2-22 05:44 | 只看该作者
这样更容易些:

create or replace view test_contains
as
WITH all_data as (
SELECT 'a1' tname,id,value from a1
union all SELECT 'a2' tname,id,value from a2
union all SELECT 'a3' tname,id,value from a3
)
select a.id
      ,max(case when tname='a1' then 'Y' end) contain_a1
      ,max(case when tname='a2' then 'Y' end) contain_a2
      ,max(case when tname='a3' then 'Y' end) contain_a3
  from a join all_data on a.item_id=all_data.id
group by a.id
;

1. select id from test_contains where contain_a1='Y' and (contain_a2='Y' or contain_a3='Y');
2. select id from test_contains where contain_a1='Y' or (contain_a2='Y' and contain_a3='Y');

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表