首页 > 其他分享 >lightdb支持distinct ... connect by的使用

lightdb支持distinct ... connect by的使用

时间:2023-09-13 10:56:40浏览次数:34  
标签:... lightdb insert distinct into t1 values test area1

在LightDB 23.3版本中,支持DISTINCT 与CONNECT BY联合使用(具体connect by使用可参考文章:https://blog.csdn.net/s_lisheng/article/details/128331881,https://blog.csdn.net/qq_22066003/article/details/128339067)

使用 DISTINCT 和 CONNECT BY 可以实现一些特定的查询操作。

DISTINCT 用于返回唯一不同的值。它可以用于单个列或多个列,以消除结果集中的重复值。例如,以下查询将返回一个列表中不同的城市名称:

SELECT DISTINCT city_name FROM table_name;

CONNECT BY 用于在查询中生成层次结构。它基于表中的父子关系列(通常是自引用的外键列),递归地连接行,以构建树状结构。以下是一个示例查询,假设有一个名为 "employees" 的表,其中包含员工的 ID、姓名和上级的 ID:

SELECT employee_id, employee_name, manager_id, LEVEL FROM employees START WITH employee_id = 1 CONNECT BY PRIOR employee_id = manager_id;

上述查询将从具有 ID 为 1 的员工开始,逐级向上连接上级,直到达到树的根节点。LEVEL 列表示每个员工在层次结构中的级别。

在LightDB中两者具体使用举例如下:

 

lightdb@t1=# create table test_area(id int, name text, pid int);
into test_area values(10101, 'hangzhou', 101);
insert into test_area values(10102, 'ningbo', 101);
insert into test_area values(10103, 'shaoxing', 101);
insert into test_area values(10104, 'wenzhou', 101);
insert into test_area values(1010101, 'binjiang', 10101);
insert into test_area values(1010102, 'shangcheng', 10101);
insert into test_area values(1010103, 'xihu', 10101);
insert into test_area values(1010101, 'gongshu', 10101);
create view v_testarea as select * from test_area;
select id, name, pid fromCREATE TABLE
lightdb@t1=# insert into test_area values(1, 'china', 0);
INSERT 0 1
lightdb@t1=# insert into test_area values(101, 'zhejiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area values(102, 'jiangsu', 1);
INSERT 0 1
lightdb@t1=# insert into test_area values(103, 'heilongjiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area values(10101, 'hangzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area values(10102, 'ningbo', 101);
INSERT 0 1
lightdb@t1=# insert into test_area values(10103, 'shaoxing', 101);
INSERT 0 1
lightdb@t1=# insert into test_area values(10104, 'wenzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area values(1010101, 'binjiang', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area values(1010102, 'shangcheng', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area values(1010103, 'xihu', 10101);
v_testarea start with name = 'hangzhou' connect by pid = prior id;
drop view v_testarea;
drop table test_area;

create table test_area1(id int, name text, pid int);
insert into test_area1 values(1, 'china', 0);
insert into test_area1 values(101, 'zhejiangINSERT 0 1
lightdb@t1=# insert into test_area values(1010101, 'gongshu', 10101);
INSERT 0 1
lightdb@t1=# create view v_testarea as select * from test_area;
CREATE VIEW
lightdb@t1=# select id, name, pid from v_testarea start with name = 'hangzhou' connect by pid = prior id;
id | name | pid
---------+------------+-------
10101 | hangzhou | 101
1010101 | binjiang | 10101
1010102 | shangcheng | 10101
1010103 | xihu | 10101
1010101 | gongshu | 10101
(5 rows)

lightdb@t1=# drop view v_testarea;
DROP VIEW
lightdb@t1=# drop table test_area;
', 1);
insert into test_area1 values(102, 'jiangsu', 1);
insert into test_area1 values(103, 'heilongjiang', 1);
insert into test_area1 values(10101, 'hangzhou', 101);
insert into test_area1 values(10102, 'ningbo', 101);
insert into test_area1 values(10103,DROP TABLE
lightdb@t1=#
lightdb@t1=# create table test_area1(id int, name text, pid int);
CREATE TABLE
lightdb@t1=# insert into test_area1 values(1, 'china', 0);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(101, 'zhejiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(102, 'jiangsu', 1);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(103, 'heilongjiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(10101, 'hangzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(10102, 'ningbo', 101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(10103, 'shaoxing', 101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(10104, 'wenzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(1010101, 'binjiang', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(1010102, 'shangcheng', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(1010103, 'xihu', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area1 values(1010101, 'gongshu', 10101);
INSERT 0 1
lightdb@t1=# select id, name, pid from (select * from test_area1) start with name = 'hangzhou' connect by id = pid;
id | name | pid
-------+----------+-----
10101 | hangzhou | 101
(1 row)

lightdb@t1=# create table test_area2(id int, name text, pid int);
CREATE TABLE
lightdb@t1=# insert into test_area2 values(1, 'china', 0);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(101, 'zhejiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(102, 'jiangsu', 1);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(103, 'heilongjiang', 1);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(10101, 'hangzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(10102, 'ningbo', 101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(10103, 'shaoxing', 101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(10104, 'wenzhou', 101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(1010101, 'binjiang', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(1010102, 'shangcheng', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(1010103, 'xihu', 10101);
INSERT 0 1
lightdb@t1=# insert into test_area2 values(1010101, 'gongshu', 10101);
INSERT 0 1
lightdb@t1=# select distinct test_area1.id, test_area1.name, test_area1.pid from test_area1, test_area2 start with name = 'hangzhou' connect by test_area1.id = test_area1.pid;
ERROR: multiple sources is not support in hierarchical queries
LINE 1: ... test_area1.name, test_area1.pid from test_area1, test_area2...
^
HINT: Use [Cross | INNER | LEFT | RIGHT | FULL ] Join instead.
lightdb@t1=# select distinct test_area1.id, test_area1.name, test_area1.pid from test_area1 join test_area2 on test_area1.id = test_area2.id start with test_area1.name = 'hangzhou' connect by test_area1.id = test_area1.pid;
id | name | pid
-------+----------+-----
10101 | hangzhou | 101
(1 row)

lightdb@t1=#
lightdb@t1=# select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^,]+', 1, 2) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3 order by test_area1.id;
cle.regexp_substr(test_area2.name, '[^;]+', 1, level) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3;
create view tr as select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^,]+', 1, 2) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3 order by test_area1.id;

create table t1(id int, name text, pid int, code text);
declare
iCount integer;
begin
insert into t1 id | name | pid | code
---------+--------------+-------+------
1 | china | 0 |
101 | zhejiang | 1 |
102 | jiangsu | 1 |
103 | heilongjiang | 1 |
10101 | hangzhou | 101 |
10102 | ningbo | 101 |
10103 | shaoxing | 101 |
10104 | wenzhou | 101 |
1010101 | gongshu | 10101 |
1010101 | binjiang | 10101 |
1010102 | shangcheng | 10101 |
1010103 | xihu | 10101 |
(12 rows)

lightdb@t1=# select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^;]+', 1, level) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3;
select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^,]+', 1, 2) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3 order by test_area1.id;
end;
/
select * from t1; id | name | pid | code
---------+--------------+-------+--------------
10101 | hangzhou | 101 |
10102 | ningbo | 101 |
1010102 | shangcheng | 10101 |
101 | zhejiang | 1 | zhejiang
1010103 | xihu | 10101 |
1010101 | binjiang | 10101 |
102 | jiangsu | 1 |
10104 | wenzhou | 101 | wenzhou
1010101 | binjiang | 10101 | binjiang
102 | jiangsu | 1 | jiangsu
1010101 | gongshu | 10101 | gongshu
1010101 | binjiang | 10101 | gongshu
1010101 | gongshu | 10101 |
10103 | shaoxing | 101 |
10103 | shaoxing | 101 | shaoxing
10102 | ningbo | 101 | ningbo
103 | heilongjiang | 1 |
1010102 | shangcheng | 10101 | shangcheng
10101 | hangzhou | 101 | hangzhou
10104 | wenzhou | 101 |
1010103 | xihu | 10101 | xihu
1 | china | 0 | china
101 | zhejiang | 1 |
1 | china | 0 |
103 | heilongjiang | 1 | heilongjiang
1010101 | gongshu | 10101 | binjiang
(26 rows)

lightdb@t1=# create view tr as select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^,]+', 1, 2) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3 order by test_area1.id;
CREATE VIEW
lightdb@t1=#
lightdb@t1=# create table t1(id int, name text, pid int, code text);
CREATE TABLE
lightdb@t1=# declare
lightdb@t1$# iCount integer;
lightdb@t1$# begin
lightdb@t1$# insert into t1 select distinct test_area1.id, test_area1.name, test_area1.pid, oracle.regexp_substr(test_area2.name, '[^,]+', 1, 2) as code from test_area1 join test_area2 on test_area1.id = test_area2.id connect by level <= 3 order by test_area1.id;
lightdb@t1$# end;
lightdb@t1$# /
DO
lightdb@t1=# select * from t1;
id | name | pid | code
---------+--------------+-------+------
1 | china | 0 |
101 | zhejiang | 1 |
102 | jiangsu | 1 |
103 | heilongjiang | 1 |
10101 | hangzhou | 101 |
10102 | ningbo | 101 |
10103 | shaoxing | 101 |
10104 | wenzhou | 101 |
1010101 | gongshu | 10101 |
1010101 | binjiang | 10101 |
1010102 | shangcheng | 10101 |
1010103 | xihu | 10101 |
(12 rows)

标签:...,lightdb,insert,distinct,into,t1,values,test,area1
From: https://www.cnblogs.com/xxl-cr7/p/17698946.html

相关文章

  • 不同小图标的编码网页中的大于号,小于号,应该用编码来代替,HTML中特殊字符和与之对应的A
    上面两个符号的HTML代码,>< >< 应用场景:当使用键盘无法打出来的时候。因为我测试在html代码中使用&amp;和&是等价的。带有实体名称的ASCII实体 常用的几个结果描述实体名称实体编号"quotationmark"&#34;'apostrophe&apos;&#39;&amper......
  • Ubuntu通过终端命令下载时提示“dpkg --configure -a......"
    如果之前在下载东西时,中途取消或中断可能会出现这种情况。结果 解决办法:在终端输入sudodpkg--configure-a ......
  • uni报错TypeError: uni[a39_0x592c5e(...)] is not a function
    本次报错是因为不知名原因导致第三方的插件进行了混淆故重新将报错的插件进行安装即可如上图所示为uni.transition插件报错此时只需到uniapp官网重新安装即可......
  • bash: pip3: command not found...
     001、问题[root@pc1test01]#pip3--version 002、解决方法a、[root@pc1test01]#yum-yinstallepel-release b、[root@pc1test01]#yuminstallpython3-pip-y 003、测试[root@pc1test01]#pip3--versionpip9.0.3from/usr/lib/python3.6......
  • 如何防止僵尸 API...
    人们越来越依赖WebAPI。2023年PostmanAPI状况报告发现,整整92%的组织计划在明年增加对API的投资。API正在为从内部微服务策略到合作伙伴策略和成熟产品的一切提供动力。然而,这种新发现的API蔓延带来了后果;迫在眉睫的威胁可能会从坟墓中升起来困扰你……当然,我说的是僵尸......
  • 软件测试|DISTINCT关键字应该怎么用?
    探索SQL中的DISTINCT关键字DISTINCT简介在SQL(StructuredQueryLanguage)中,DISTINCT关键字是一个强大的工具,用于查询去重。它允许我们从数据库中获取唯一(不重复)的记录,而不考虑其他列的值。本文将深入探讨SQL中的DISTINCT关键字,包括其语法、用途和示例。DISTINCT语法DISTINCT关......
  • 万字解读B站FinOps落地经验与方法论...
    云成本优化(FinOps)一词,变得越来越流行。尽管FinOps在国内提及不多,但早在2020年12月,中国信通院就牵头成立FinOps产业推进方阵,推进规模化实践。而在那些率先拥抱云原生的互联网大厂内部,云成本优化的种子也早就生根萌芽,形成了最佳实践方法论,如阿里集团、腾讯、字节跳动、B站等。FinOps......
  • 软件测试|MySQL DISTINCT关键字过滤重复数据
    简介在MySQL中,有时候我们需要从表中检索唯一的、不重复的数据。这时,我们可以使用DISTINCT关键字来过滤掉重复的数据行。在本文中,我们将深入探讨MySQL中DISTINCT的用法以及如何在查询中使用它来得到不重复的结果集。基本语法DISTINCT关键字用于在SELECT语句中指示查询结果中去除重复......
  • 莫名显示的【Create event...】菜单问题
    问题现象:开发的程序在英语环境下,选择时间控件内的文本,按Ctrl+C时,会弹出一个【Createevent...】菜单(如下图)。 问题原因:WIN11的新功能。电脑在安装了日历APP后,选择当前日期之后的时间时,会弹出此菜单(仅支持北美)。可以让用户创建相应的日程计划。相应的功能说明:https://support.......
  • docker 打开报错 windows hypervisor is not present docker desktop is unable to de
     dockerdesktop-windowshypervisorisnotpresentdockerdesktopisunabletodetectahypervisor.hardwareassistedvirtualizationanddataexecutionprotectionmustbeenabledintheblos.seehttps://docsdocker.com/desktop/troubleshoot/topics/#virtua......