1. --期盼值 找出AA,3;PDST,3;QPL-,3;TP-,2;
  2.  
  3. --基本表
  4. create table tb_product(
  5. id number(9,0) primary key,
  6. name nvarchar2(20) not null);
  7. --基本表充值
  8. insert into tb_product(id,name) values('','AA');
  9. insert into tb_product(id,name) values('','AA款');
  10. insert into tb_product(id,name) values('','AA屏风');
  11. insert into tb_product(id,name) values('','PDST');
  12. insert into tb_product(id,name) values('','PDST款');
  13. insert into tb_product(id,name) values('','PDST-TJ');
  14. insert into tb_product(id,name) values('','QPL-TJ');
  15. insert into tb_product(id,name) values('','QPL-1');
  16. insert into tb_product(id,name) values('','QPL-2');
  17. insert into tb_product(id,name) values('','TP-1');
  18. insert into tb_product(id,name) values('','TP-2');
  19. --序列表
  20. create table tb_seq(
  21. id number(9,0) primary key);
  22. --序列表充值
  23. insert into tb_seq
  24. select rownum from dual
  25. connect by level<10
  26. order by dbms_random.random;
  27.  
  28. --求所有名称及其长度
  29. select name,length(name) as len from tb_product
  30.  
  31. --求最大长度
  32. select max(len) as maxlen from (select name,length(name) as len from tb_product)
  33.  
  34. --求截取序列
  35. select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id
  36.  
  37. --tb_product表和截取序列表求笛卡儿积
  38. select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq
  39.  
  40. --看name长度和length的关系
  41. select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
  42. (select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq) a
  43.  
  44. --清除掉extra
  45. select b.sery from
  46. (select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
  47. (select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq) a) b
  48. where b.sery<>'extra'
  49.  
  50. --求种类个数
  51. select c.sery,count(*) as cnt from
  52. (select b.sery from
  53. (select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
  54. (select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq) a) b
  55. where b.sery<>'extra') c
  56. group by c.sery
  57.  
  58. --按种类个数排序
  59. select d.sery,d.cnt from
  60. (select c.sery,count(*) as cnt from
  61. (select b.sery from
  62. (select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
  63. (select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq) a) b
  64. where b.sery<>'extra') c
  65. group by c.sery) d
  66. where length(d.sery)>1 and d.cnt>1
  67. order by d.cnt desc,d.sery
  68.  
  69. --SQL实在太长了,为了简化,将上面的结果放入新表
  70. create table tb_tmp1 as select d.sery,d.cnt from
  71. (select c.sery,count(*) as cnt from
  72. (select b.sery from
  73. (select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
  74. (select p.name,length(p.name) as namelen,seq.id as cutlen from tb_product p,(select id from tb_seq where id<=(select max(len) as maxlen from (select name,length(name) as len from tb_product)) order by id) seq) a) b
  75. where b.sery<>'extra') c
  76. group by c.sery) d
  77. where length(d.sery)>1 and d.cnt>1
  78. order by d.cnt desc,d.sery
  79.  
  80. --看看结果
  81. select * from tb_tmp1
  82.  
  83. --把唯一的sery值做成字符串
  84. select listagg(sery,',') within group(order by sery) from tb_tmp1
  85.  
  86. --查出的结果只能在唯一sery序列中出现一次
  87. select *
  88. from tb_tmp1
  89. where LENGTH(REGEXP_REPLACE(REPLACE((select listagg(sery,',') within group(order by sery) from tb_tmp1 ), sery, '@'), '[^@]+', ''))=1
  90.  
  91. --最后结果与最开始的预期(AA,3;PDST,3;QPL-,3;TP-,2;)一致
  92. SQL> select *
  93. 2 from tb_tmp1
  94. 3 where LENGTH(REGEXP_REPLACE(REPLACE((select listagg(sery,',') within group(order by sery) from tb_tmp1 ), sery, '@'), '[^@]+', ''))=1;
  95.  
  96. SERY
  97. ----------------------------------------------------------------------------------------------------
  98. CNT
  99. ----------
  100. AA
  101. 3
  102.  
  103. PDST
  104. 3
  105.  
  106. QPL-
  107. 3
  108.  
  109. TP-
  110. 2

用带变量循环函数类的程序语言弄出来不难,但全靠SQL做出来也别有一番风味。

--2020年4月15日8:40 到 2020年4月16日 0:28--

(高难度SQL)从产品表中找出相同前缀 (都云作者痴 谁解其中味)的更多相关文章

  1. 在一张id连续的表中找出缺失的id

    有这样一张表: create table tb_lostid( id number(6,0) primary key not null, name nvarchar2(20) not null ) 可 ...

  2. sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)

    sql server 关于表中只增标识问题   由于我们系统时间用的过长,数据量大,设计是采用自增ID 我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识 没有关闭的时候 ,提示一下错 ...

  3. **SQL某一表中重复某一字段重复记录查询与处理

    sql某一表中重复某一字段重复记录查询与处理   1.查询出重复记录  select 重复记录字段 form  数据表 group by houseno having count(重复记录字段)> ...

  4. SQL语句 在一个表中插入新字段

    SQL语句 在一个表中插入新字段: alter table 表名 add 字段名 字段类型 例: alter table OpenCourses add Audio varchar(50)alter ...

  5. EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的

    我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...

  6. 向SQL Server 现有表中添加新列并添加描述.

    注: sql server 2005 及以上支持. 版本估计是不支持(工作环境2005,2008). 工作需要, 需要向SQL Server 现有表中添加新列并添加描述. 从而有个如下存储过程. (先 ...

  7. oracle通过sql随机取表中的10条记录

    oracle通过sql随机取表中的10条记录: SELECT * FROM (SELECT * FROM T_USER ORDER BY DBMS_RANDOM.RANDOM()) WHERE Row ...

  8. SQL查询一个表中类别字段中Max()最大值对应的记录

      SQL查询一个表中类别字段中Max()最大值对应的记录 SELECT A.id, A.name, A.version FROM   DOC A, (SELECT id, MAX(version)  ...

  9. sql之将一个表中的数据注入另一个表中

    sql之将一个表中的数据注入另一个表中 需求:现有两张表t1,t2,现需要将t2的数据通过XZQHBM相同对应放入t1表中 t1: t2: 思路:left join 语句: select * from ...

随机推荐

  1. Elasticsearch第三篇:查询详解

    从第一篇开始,我用的ES版本就是7.8.0的,与低版本略有不同,不同点可以参考官方介绍,最大的不同就是抛弃 type 这一概念,为了方便测试,首先建立一个学生成绩的索引库(在建立的同时,规定字段类型, ...

  2. 2020-04-07:说出Servlet的生命周期,并说出Servlet和CGI的区别。

    Servlet的生命周期分为5个阶段:实例化:Servlet容器创建Servlet类的实例.初始化:该容器调用init()方法,通常会申请资源.服务:由容器调用service()方法,(也就是doGe ...

  3. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集   67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...

  4. Solon Ioc 的注解对比Spring及JSR330

    注解对比 Solon 1.0.10 Spring JSR 330 @XInject * @Autowired @Inject 字段或参数注入 @XBean * @Component @Named Be ...

  5. Linux下安装pgadmin,并外部访问

    环境: Centos 7 .已经安装的postgresql11,具体安装可以查看https://www.cnblogs.com/whitebai/p/12122240.html 1.下载阿里的 rep ...

  6. 44. Spring Security FAQ春季安全常见问题

    第44.1节,“一般问题” 第44.2节,“常见问题” 第44.3节,“春季安全架构问题” 第44.4节,“常见”如何“请求 44.1 General Questions 第44.1.1节,“Spri ...

  7. flutter开发体验

    flutter 介绍 flutter 是一种跨平台UI开发框架.这方面类似框架有: weex: Weex是一个可以使用现代化的 Web 技术开发高性能原生应用的框架. React Native: Re ...

  8. 运用sklearn进行线性判别分析(LDA)代码实现

    基于sklearn的线性判别分析(LDA)代码实现 一.前言及回顾 本文记录使用sklearn库实现有监督的数据降维技术——线性判别分析(LDA).在上一篇LDA线性判别分析原理及python应用(葡 ...

  9. Android CC框架中,新建组件无法显示布局问题

    出错: 当在创建新的组件时,跳转到新组件成功,但是无法正确显示布局,即获取到布局文件的控件等. 原因: 当在创建新的组件时,默认生成MainActivity以及其布局activity_main.每个组 ...

  10. Debian 镜像使用帮助

    链接: Debian 镜像使用帮助 buster: # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua ...