12
返回列表 发新帖
楼主: 可爱灰狼

[SQL] SQL 开发,一道测试题

[复制链接]
论坛徽章:
1
秀才
日期:2016-11-25 16:52:36
11#
发表于 2017-4-18 10:48 | 只看该作者
newkid 发表于 2017-4-18 07:18
改成这样就行:
select * from
(select id,

大师

使用道具 举报

回复
论坛徽章:
126
ITPUB元老
日期:2007-07-04 17:27:50会员2007贡献徽章
日期:2007-09-26 18:42:10现任管理团队成员
日期:2011-05-07 01:45:08优秀写手
日期:2015-01-09 06:00:14版主7段
日期:2015-07-16 02:10:00
12#
发表于 2017-4-18 11:03 | 只看该作者
newkid 发表于 2017-4-17 22:24
你的方法是有漏洞的,比如下面这个测试例子,CC就没找出来:

create table t_xyh1 (id number,name va ...




这种反例举得很好。

我的语句也只要改改就可以了,就是复杂了点, 呵呵

测试如下:

SQL> select  id,
  2          name,
  3          to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime
  4  from
  5  (select id,
  6          name,
  7          RDateTime,
  8          flag,
  9          count(distinct name) over(partition by flag) cnt1,
10          count(name) over(partition by flag) cnt2,
11          count(*) over(partition by flag, name) cnt3
12  from
13  (select id,
14          name,
15          RDateTime,
16          min(id) over(partition by name order by RDateTime range between NUMTODSINTERVAL(2, 'hour') preceding and NUMTODSINTERVAL(2, 'hour') following) flag
17     from t_xyh1))
18  where cnt1 <> cnt2
19    and cnt2 > 1
20    and cnt3 > 1;

        ID NAME                   RDATETIME
---------- ---------------------- -----------------
         4 aa                     20120101 11:11:11
         1 aa                     20120101 12:11:11
         2 aa                     20120101 13:11:11

SQL>


SQL> select id, name, to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime from t_xyh2;

        ID NAME                   RDATETIME
---------- ---------------------- -----------------
         1 aa                     20120101 10:00:00
         2 bb                     20120101 11:11:11
         3 cc                     20120101 11:50:00
         4 cc                     20120101 13:00:00

SQL>
SQL> select  id,
  2          name,
  3          to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime
  4  from
  5  (select id,
  6          name,
  7          RDateTime,
  8          flag,
  9          count(distinct name) over(partition by flag) cnt1,
10          count(name) over(partition by flag) cnt2,
11          count(*) over(partition by flag, name) cnt3
12  from
13  (select id,
14          name,
15          RDateTime,
16          min(id) over(partition by name order by RDateTime range between NUMTODSINTERVAL(2, 'hour') preceding and NUMTODSINTERVAL(2, 'hour') following) flag
17     from t_xyh2))
18  where cnt1 <> cnt2
19    and cnt2 > 1
20    and cnt3 > 1;

        ID NAME                   RDATETIME
---------- ---------------------- -----------------
         3 cc                     20120101 11:50:00
         4 cc                     20120101 13:00:00




使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
13#
发表于 2017-4-18 11:59 | 只看该作者
分析函数就ok,newkid的真简化

使用道具 举报

回复
论坛徽章:
51
SQL数据库编程大师
日期:2013-12-06 13:59:43SQL大赛参与纪念
日期:2013-12-06 14:03:45SQL极客
日期:2013-12-09 14:13:35ITPUB社区OCM联盟徽章
日期:2014-12-30 09:12:25SQL数据库编程大师
日期:2016-01-13 10:30:43SQL大赛参与纪念
日期:2016-01-13 10:32:19双子座
日期:2016-02-26 14:50:25乌索普
日期:2018-03-02 09:19:33
14#
发表于 2017-4-18 13:55 | 只看该作者
select distinct t1.id, t1.name,t1.rdatetime
from   t_xyh11 t1,  t_xyh11 t2
where t1.name=t2.name
and abs(t2.rdatetime-t1.rdatetime)*24 <=2
and t1.rdatetime<>t2.rdatetime;

这样也可以吧

使用道具 举报

回复
论坛徽章:
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
15#
发表于 2017-4-19 06:47 | 只看该作者
bell6248 发表于 2017-4-18 11:03
这种反例举得很好。

我的语句也只要改改就可以了,就是复杂了点, 呵呵

还是有漏洞,下面例子最后一行漏掉了:

delete t_xyh1;
insert into t_xyh1 values(3,'cc',to_date('20120101 11:50:00','yyyymmdd hh24:mi:ss'));
insert into t_xyh1 values(4,'cc',to_date('20120101 13:00:00','yyyymmdd hh24:mi:ss'));
insert into t_xyh1 values(4,'cc',to_date('20120101 14:00:00','yyyymmdd hh24:mi:ss'));

select  id,
          name,
          to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime
  from
  (select id,
          name,
          RDateTime,
          flag,
          count(distinct name) over(partition by flag) cnt1,
         count(name) over(partition by flag) cnt2,
         count(*) over(partition by flag, name) cnt3
from
(select id,
         name,
         RDateTime,
         min(id) over(partition by name order by RDateTime range between NUMTODSINTERVAL(2, 'hour') preceding and NUMTODSINTERVAL(2, 'hour') following) flag
    from t_xyh1))
where cnt1 <> cnt2
   and cnt2 > 1
   and cnt3 > 1;

使用道具 举报

回复
论坛徽章:
126
ITPUB元老
日期:2007-07-04 17:27:50会员2007贡献徽章
日期:2007-09-26 18:42:10现任管理团队成员
日期:2011-05-07 01:45:08优秀写手
日期:2015-01-09 06:00:14版主7段
日期:2015-07-16 02:10:00
16#
发表于 2017-4-19 14:05 | 只看该作者
newkid 发表于 2017-4-19 06:47
还是有漏洞,下面例子最后一行漏掉了:

delete t_xyh1;



我把你给的2中特殊情况和LZ原先的数据进行了整合进行测试, 测试如下, 你看看是否还是有特殊情况

SQL> select id, name, to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime from t_xyh1;

        ID NAME                   RDATETIME
---------- ---------------------- -----------------
         1 aa                     20120101 12:11:11
         2 aa                     20120101 13:11:11
         3 aa                     20120102 12:11:11
         4 aa                     20120101 11:11:11
         5 bb                     20120101 11:11:11
         6 bb                     20120102 11:11:11
         7 bb                     20120103 11:11:11
         8 bb                     20120104 11:11:11
        11 aa                     20140101 10:00:00
        12 bb                     20140101 11:11:11
        13 cc                     20140101 11:50:00
        14 cc                     20140101 13:00:00
        23 cc                     20160101 11:50:00
        24 cc                     20160101 13:00:00
        25 cc                     20160101 14:00:00

15 rows selected

SQL>
SQL> select  id,
  2          name,
  3          to_char(RDateTime, 'yyyymmdd hh24:mi:ss') RDateTime
  4  from
  5  (select id,
  6          name,
  7          RDateTime,
  8          flag,
  9          count(distinct name) over(partition by flag) cnt1,
10          count(name) over(partition by flag) cnt2,
11          count(*) over(partition by flag, name) cnt3
12  from
13  (select id,
14          name,
15          RDateTime,
16          least(flag, max(rn) over(partition by name order by RDateTime)) flag
17  from
18  (select id,
19          name,
20          RDateTime,
21          flag,
22          case when flag <= max(id) over(partition by name order by RDateTime ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) then null else id end rn
23  from
24  (select id,
25          name,
26          RDateTime,
27          min(id) over(partition by name order by RDateTime range between NUMTODSINTERVAL(2, 'hour') preceding and NUMTODSINTERVAL(2, 'hour') following) flag
28     from t_xyh1))))
29  where cnt1 <> cnt2
30     and cnt2 > 1
31     and cnt3 > 1;

        ID NAME                   RDATETIME
---------- ---------------------- -----------------
         4 aa                     20120101 11:11:11
         1 aa                     20120101 12:11:11
         2 aa                     20120101 13:11:11
        13 cc                     20140101 11:50:00
        14 cc                     20140101 13:00:00
        25 cc                     20160101 14:00:00
        23 cc                     20160101 11:50:00
        24 cc                     20160101 13:00:00

8 rows selected

SQL>




使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
17#
发表于 2017-4-19 23:19 | 只看该作者
workflow263 发表于 2017-4-18 13:55
select distinct t1.id, t1.name,t1.rdatetime
from   t_xyh11 t1,  t_xyh11 t2
where t1.name=t2.name ...

可以,但是表扫了两次,效率不高

使用道具 举报

回复
论坛徽章:
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-4-19 23:35 | 只看该作者
bell6248 发表于 2017-4-19 14:05
我把你给的2中特殊情况和LZ原先的数据进行了整合进行测试, 测试如下, 你看看是否还是有特殊情况
...

轻易击垮:

delete t_xyh1;
insert into t_xyh1 values(3,'cc',to_date('20120101 11:50:00','yyyymmdd hh24:mi:ss'));
insert into t_xyh1 values(2,'cc',to_date('20120101 13:00:00','yyyymmdd hh24:mi:ss'));
insert into t_xyh1 values(1,'cc',to_date('20120101 14:00:00','yyyymmdd hh24:mi:ss'));


在这个新例子中你的查询漏掉了第一个。
我能猜到你下一步要怎么调整,但是劝你别再钻牛角尖了,这个写法越来越臃肿而且反复扫描数据,比原来的表连接法还低效。

使用道具 举报

回复
论坛徽章:
126
ITPUB元老
日期:2007-07-04 17:27:50会员2007贡献徽章
日期:2007-09-26 18:42:10现任管理团队成员
日期:2011-05-07 01:45:08优秀写手
日期:2015-01-09 06:00:14版主7段
日期:2015-07-16 02:10:00
19#
发表于 2017-4-19 23:50 来自手机 | 只看该作者
本帖最后由 bell6248 于 2017-4-19 23:55 编辑
newkid  2017-4-19 23:35


delete t_xyh1;


按照LZ提供数据看,列ID的值是按照日期字段升序而递增的,否则就先按照日期字段从小到大排序先得到一个类似的ID字段,之后代码不变

使用道具 举报

回复

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

本版积分规则 发表回复

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