--期盼值 找出AA,3;PDST,3;QPL-,3;TP-,2;

--基本表
create table tb_product(
id number(9,0) primary key,
name nvarchar2(20) not null);
--基本表充值
insert into tb_product(id,name) values('','AA');
insert into tb_product(id,name) values('','AA款');
insert into tb_product(id,name) values('','AA屏风');
insert into tb_product(id,name) values('','PDST');
insert into tb_product(id,name) values('','PDST款');
insert into tb_product(id,name) values('','PDST-TJ');
insert into tb_product(id,name) values('','QPL-TJ');
insert into tb_product(id,name) values('','QPL-1');
insert into tb_product(id,name) values('','QPL-2');
insert into tb_product(id,name) values('','TP-1');
insert into tb_product(id,name) values('','TP-2');
--序列表
create table tb_seq(
id number(9,0) primary key);
--序列表充值
insert into tb_seq
select rownum from dual
connect by level<10
order by dbms_random.random; --求所有名称及其长度
select name,length(name) as len from tb_product --求最大长度
select max(len) as maxlen from (select name,length(name) as len from tb_product) --求截取序列
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 --tb_product表和截取序列表求笛卡儿积
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 --看name长度和length的关系
select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
(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 --清除掉extra
select b.sery from
(select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
(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
where b.sery<>'extra' --求种类个数
select c.sery,count(*) as cnt from
(select b.sery from
(select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
(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
where b.sery<>'extra') c
group by c.sery --按种类个数排序
select d.sery,d.cnt from
(select c.sery,count(*) as cnt from
(select b.sery from
(select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
(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
where b.sery<>'extra') c
group by c.sery) d
where length(d.sery)>1 and d.cnt>1
order by d.cnt desc,d.sery --SQL实在太长了,为了简化,将上面的结果放入新表
create table tb_tmp1 as select d.sery,d.cnt from
(select c.sery,count(*) as cnt from
(select b.sery from
(select (case when a.namelen<a.cutlen then 'extra' else to_char(substr(a.name,1,a.cutlen)) end) as sery from
(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
where b.sery<>'extra') c
group by c.sery) d
where length(d.sery)>1 and d.cnt>1
order by d.cnt desc,d.sery --看看结果
select * from tb_tmp1 --把唯一的sery值做成字符串
select listagg(sery,',') within group(order by sery) from tb_tmp1 --查出的结果只能在唯一sery序列中出现一次
select *
from tb_tmp1
where LENGTH(REGEXP_REPLACE(REPLACE((select listagg(sery,',') within group(order by sery) from tb_tmp1 ), sery, '@'), '[^@]+', ''))=1 --最后结果与最开始的预期(AA,3;PDST,3;QPL-,3;TP-,2;)一致
SQL> select *
2 from tb_tmp1
3 where LENGTH(REGEXP_REPLACE(REPLACE((select listagg(sery,',') within group(order by sery) from tb_tmp1 ), sery, '@'), '[^@]+', ''))=1; SERY
----------------------------------------------------------------------------------------------------
CNT
----------
AA
3 PDST
3 QPL-
3 TP-
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. SQL Server中的集合运算: UNION, EXCEPT和INTERSECT

    SQL Server中的集合运算包括UNION(合并),EXCEPT(差集)和INTERSECT(相交)三种. 集合运算的基本使用 1.UNION(合并两个查询结果集,隐式DINSTINCT,删除重复 ...

  2. MySQL数据库——数据约束

    1 什么数据约束 对用户操作表的数据进行约束 2 默认值(缺省约束) 作用: 当用户对使用默认值的字段不插入值的时候,就使用默认值. 注意: 1)对默认值字段插入null是可以的 2)对默认值字段可以 ...

  3. MST(最小生成树)

    1.prim算法分析 prim算法是用来构建MST(最小生成树)的一种基于贪心策略的算法.prim算法通过维护lowcost数组和closest数组记录每次查询的最小权值边结点. 首先,看一个示例来理 ...

  4. 【算法•日更•第四十七期】Mac与windows系统的差别

    小编最近装了个Mac系统,因为小编已经有笔记本可以用linux了,所以就决定在台式机上装个双系统,结果一不小心把Mac装在C盘上了,哎,说多了都是泪啊. 其实用了Mac之后才发现windows特别好用 ...

  5. Oracle数据库安装教程

    一.准备文件 Oracle安装程序(64位)下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_ ...

  6. 共享&img (给作者自己

    --------------------------------------------------------- ------------------------------------------ ...

  7. QT下载速度慢的解决方法

    在官网的下载速度实在太慢了 找到了一个镜像网站 https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/

  8. linux驱动之内核多线程(二)

    本文摘自http://www.cnblogs.com/zhuyp1015/archive/2012/06/11/2545702.html 内核多线程是在项目中使用到,自己也不熟悉,遇到一个很囧的问题, ...

  9. Devops与敏捷二者能否结合?

    当前软件行业的趋势倾向于使应用程序开发和部署成为业务运营的重要组成部分.这些公司开始专注于实现像DevOps解决方案这样的方法,这有助于缩短产品开发时间.使用DevOps进行开发减少了交付软件所需的阶 ...

  10. df卡死和fork:cannot allocate memory报错

    早上到了公司,发现docker资源池的某一台主机根文件系统写满. 检查后发现该主机/data目录未挂载文件系统,直接放在了根目录下.于是联系业务方将应用迁移,联系主机工程师为/data挂载80G的存储 ...