Oracle查询基础(oracle基本查询语句)
sinye56 2024-09-22 08:26 5 浏览 0 评论
这篇文章的内容主要是Oracle基础查询的内容,以及一些常用的查询技巧,比如开窗函数的应用。
--(1)全表查询
--(2)查询表中固定列
--(3)左连接查询 left join 表名 on 关联条件
--(4)where条件查询
--(5)逻辑符 and or
--(6)nvl()函数 select nvl(CTRANSPORTTYPEID,'测试nvl用法') from so_saleorder nvl(dr,0) = 0
--(7)内连接
--(8)去除重复项
--(9)对查询结果进行排序
--(10)分组查询
--(11)开窗
--(12)count() 以及count()开窗
--(13)常数查询
--(14)dual表
--查询销售订单主表 主表主键 csaleorderid
select csaleorderid,vbillcode from so_saleorder where csaleorderid = '10011A10000000021LEC'
--查询销售订单子表 主表主键 csaleorderid
select csaleorderid,csaleorderbid,cmaterialid from so_saleorder_b where csaleorderid = '10011A10000000021LEC'
--1.NC中同一张表主表和子表的连接关系(关联字段) 主表表名.主表主键 = 子表表名.子表中存主表主键值的字段名
--2.多张表查询 左连接 右连接 内连接 全连接
--(1)左连接:左表为基础 关键字 left join ... on
select a.vbillcode as 单据号 from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
--左连接查询销售订单业务员姓名
--(1)查询销售订单业务员
select cemployeeid from so_saleorder --查询结果分析:销售订单主表中,CEMPLOYEEID字段存的是业务员(人员基本信息)主键值
--(2)左连接关联销售订单主表和人员基本信息表
select a.vbillcode as 单据号,b.name as 业务员
from so_saleorder a
left join bd_psndoc b
on a.CEMPLOYEEID = b.pk_psndoc
--查询销售订单子表中对应的物料编码
select cmaterialvid from so_saleorder_b
select a.cmaterialvid ,b.code as 物料编码,b.name as 物料名称
from so_saleorder_b a
left join bd_material b
on a.cmaterialvid=b.pk_material
--查询销售订单主表单据号以及对应子表的物料信息
select a.VBILLCODE as 单据号,c.code as 物料编码, c.name as 物料名称,d.name as 业务员
from SO_SALEORDER a
left join SO_SALEORDER_B b
on a.csaleorderid=b.csaleorderid
left join BD_MATERIAL c
on b.cmaterialvid=c.pk_material
left join bd_psndoc d
on a.CEMPLOYEEID=d. pk_psndoc
--NC中做报表,必须要有的两个字段:pk_group pk_org
select a.VBILLCODE as vbillcode,c.code as matcode, c.name as matname,d.name as psnname,a.pk_group as pk_group,a.pk_org as pk_org
from SO_SALEORDER a
left join SO_SALEORDER_B b
on a.csaleorderid=b.csaleorderid
left join BD_MATERIAL c
on b.cmaterialvid=c.pk_material
left join bd_psndoc d
on a.CEMPLOYEEID=d. pk_psndoc
--条件查询 where关键字 位于from关键字之后 A1-619717
select a.VBILLCODE as 单据号,c.code as 物料编码, c.name as 物料名称,d.name as 业务员
from SO_SALEORDER a
left join SO_SALEORDER_B b
on a.csaleorderid=b.csaleorderid
left join BD_MATERIAL c
on b.cmaterialvid=c.pk_material
left join bd_psndoc d
on a.CEMPLOYEEID=d. pk_psndoc
where c.code = 'A1-619717' and d.name = '杨建军'
--逻辑符 and or
select a.VBILLCODE as 单据号,c.code as 物料编码, c.name as 物料名称,d.name as 业务员
from SO_SALEORDER a
left join SO_SALEORDER_B b
on a.csaleorderid=b.csaleorderid
left join BD_MATERIAL c
on b.cmaterialvid=c.pk_material
left join bd_psndoc d
on a.CEMPLOYEEID=d. pk_psndoc
where c.code = 'A1-619717' or d.name = '杨建军'
--NC中的逻辑删除 dr = 1 视为删除
select vbillcode from so_saleorder where dr = 1
select vbillcode from so_saleorder where nvl(dr,0) = 0
--以左表为基础
select b.name as 业务员, a.vbillcode as 单据号
from bd_psndoc b
left join so_saleorder a
on a.CEMPLOYEEID = b.pk_psndoc
select b.name as 业务员, a.vbillcode as 单据号
from bd_psndoc b
right join so_saleorder a
on a.CEMPLOYEEID = b.pk_psndoc
--SO20190324000009 SO201903240006 SO201903240000008
--内连接 笛卡尔积 两边必须同时有才能被查出来
select a.vbillcode,b.name
from so_saleorder a,bd_psndoc b
where a.CEMPLOYEEID = b.pk_psndoc
--全连接 full join
--物料和安全库存
--去除重复项 关键字 distinct
select distinct b.name
from so_saleorder a left join bd_psndoc b
on a.CEMPLOYEEID = b.pk_psndoc
--对查询结果进行排序 order by 关键字 asc 升序 desc 降序 order by 字段名 asc/desc
select a.vbillcode ,b.name
from so_saleorder a left join bd_psndoc b
on a.CEMPLOYEEID = b.pk_psndoc
where nvl(a.dr,0) = 0
order by 1 desc,2 desc
--分组查询 关键字 group by 分组查询是和统计函数一起使用
select a.vbillcode as 单据号,c.name ,sum(b.norigtaxmny),avg(b.norigtaxmny)
from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
left join bd_psndoc c on a.CEMPLOYEEID = c.pk_psndoc
where nvl(a.dr,0) = 0
group by a.vbillcode,c.name
order by 1
--开窗函数
create or replace view v_cost
as
select '0101' as date1, 100 as price from dual
union all
select '0102' as date1, 200 as price from dual
union all
select '0103' as date1, 300 as price from dual
select date1,price,sum(price) over(order by date1) from v_cost
--count()求数据条数总和
select count(*) from so_saleorder where nvl(dr,0) = 0
--每个销售订单表体有多少条数据
select distinct a.vbillcode as 单据号,c.name ,count(*) over(partition by a.vbillcode)
from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
left join bd_psndoc c on a.CEMPLOYEEID = c.pk_psndoc
where nvl(a.dr,0) = 0
order by 1
--生成序号 rownum
select distinct a.vbillcode as 单据号,c.name ,count(*) over(partition by a.vbillcode),rownum
from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
left join bd_psndoc c on a.CEMPLOYEEID = c.pk_psndoc
where nvl(a.dr,0) = 0
order by rownum
--rank 排序
select * from (
select distinct a.vbillcode as 单据号,c.name ,count(*) over(partition by a.vbillcode),rownum as rn,rank() over(order by a.vbillcode) as rk
from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
left join bd_psndoc c on a.CEMPLOYEEID = c.pk_psndoc
where nvl(a.dr,0) = 0
order by rownum
)
order by rk,rn
select * from (
select distinct a.vbillcode as 单据号,c.name ,count(*) over(partition by a.vbillcode),rownum as rn,rank() over(
partition by a.vbillcode order by b.cmaterialid) as rk
from
so_saleorder a
left join so_saleorder_b b on a.csaleorderid = b.csaleorderid
left join bd_psndoc c on a.CEMPLOYEEID = c.pk_psndoc
where nvl(a.dr,0) = 0
order by rownum
)
order by 1,rk
--union all 关键字 列数量一致,每一列数据类型对应
select vbillcode from so_saleorder
union all
select vbillcode from po_order
union all
select vbillcode from mm_mo
select distinct x.pk_material,y.pk_org,y.pk_group,y.code,y.name,z.pk_materialstock from (
select distinct cinventoryvid as pk_material from to_bill_b where nvl(dr,0) = 0
union all
select distinct cbmaterialvid as pk_material from mm_wr_product
where mm_wr_product.pk_org <> mm_wr_product.vbdef7 and nvl(dr,0) = 0
union all
select distinct F.cmaterialvid as pk_material from ic_saleout_h E
left join
ic_saleout_b F
on
E.cgeneralhid = F.cgeneralhid
WHERE E.PK_ORG <> E.csaleorgoid
AND NVL(F.DR,0) = 0 AND NVL(E.DR,0) = 0
) x
left join
bd_material y
on x.pk_material = y.pk_material
left join
bd_materialstock z
on
x.pk_material = z.pk_material
where
z.pk_org not in('00011A10000000002ST4' ,'00011A10000000002T2L','00011A10000000002T1J')
and z.innermoveprice is null
select a.vbillcode ,b.name,'常数' as 常数查询
from so_saleorder a left join bd_psndoc b
on a.CEMPLOYEEID = b.pk_psndoc
where nvl(a.dr,0) = 0
order by 1 desc,2 desc
select 2+5 from dual --dual表
select vbillcode from so_saleorder where vbillcode like '201812%' --左匹配
select vbillcode from so_saleorder where vbillcode like '%201812' --右匹配
select vbillcode from so_saleorder where vbillcode like '%201812%' --中间匹配
相关推荐
- RHEL8和CentOS8怎么重启网络
-
本文主要讲解如何重启RHEL8或者CentOS8网络以及如何解决RHEL8和CentOS8系统的网络管理服务报错,当我们安装好RHEL8或者CentOS8,重启启动网络时,会出现以下报错:...
- Linux 内、外网双网卡路由配置
-
1.路由信息的影响Linux系统中如果有多张网卡的情况下,如果路由信息配置不正确,...
- Linux——centos7修改网卡名
-
修改网卡名这个操作可能平时用不太上,可作为了解。修改网卡默认名从ens33改成eth01.首先修改网卡配置文件名(建议将原配置文件进行备份)...
- CentOS7下修改网卡名称为ethX的操作方法
-
?Linux操作系统的网卡设备的传统命名方式是eth0、eth1、eth2等,而CentOS7提供了不同的命名规则,默认是基于固件、拓扑、位置信息来分配。这样做的优点是命名全自动的、可预知的...
- Linux 网卡名称enss33修改为eth0
-
一、CentOS修改/etc/sysconfig/grub文件(修改前先备份)为GRUB_CMDLINE_LINUX变量增加2个参数(net.ifnames=0biosdevname=0),修改完成...
- CentOS下双网卡绑定,实现带宽飞速
-
方式一1.新建/etc/sysconfig/network-scripts/ifcfg-bond0文件DEVICE=bond0IPADDR=191.3.60.1NETMASK=255.255.2...
- linux 双网卡双网段设置路由转发
-
背景网络情况linux双网卡:网卡A(ens3)和网卡B(...
- Linux-VMware设置网卡保持激活
-
Linux系统只有在激活网卡的状态下才能去连接网络,进行网络通讯。修改配置文件(永久激活网卡)...
- VMware虚拟机三种网络模式
-
01.VMware虚拟机三种网络模式由于linux目前很热门,越来越多的人在学习linux,但是买一台服务放家里来学习,实在是很浪费。那么如何解决这个问题?虚拟机软件是很好的选择,常用的虚拟机软件有v...
- 2023年最新版 linux克隆虚拟机 解决网卡uuid重复问题
-
问题描述1、克隆了虚拟机,两台虚拟机里面的ip以及网卡的uuid都是一样的2、ip好改,但是uuid如何改呢?解决问题1、每台主机应该保证网卡的UUID是唯一的,避免后面网络通信有问题...
- Linux网卡的Vlan配置,你可能不了解的玩法
-
如果服务器上连的交换机端口已经预先设置了TRUNK,并允许特定的VLAN可以通过,那么服务器的网卡在配置时就必须指定所属的VLAN,否则就不通了,这种情形在虚拟化部署时较常见。例如在一个办公环境中,办...
- Centos7 网卡绑定
-
1、切换到指定目录#备份网卡数据cd/etc/sysconfig/network-scriptscpifcfg-enp5s0f0ifcfg-enp5s0f0.bak...
- Linux搭建nginx+keepalived 高可用(主备+双主模式)
-
一:keepalived简介反向代理及负载均衡参考:...
- Linux下Route 路由指令使用详解
-
linuxroute命令用于显示和操作IP路由表。要实现两个不同子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- oracle忘记用户名密码 (59)
- oracle11gr2安装教程 (55)
- mybatis调用oracle存储过程 (67)
- oracle spool的用法 (57)
- oracle asm 磁盘管理 (67)
- 前端 设计模式 (64)
- 前端面试vue (56)
- linux格式化 (55)
- linux图形界面 (62)
- linux文件压缩 (75)
- Linux设置权限 (53)
- linux服务器配置 (62)
- mysql安装linux (71)
- linux启动命令 (59)
- 查看linux磁盘 (72)
- linux用户组 (74)
- linux多线程 (70)
- linux设备驱动 (53)
- linux自启动 (59)
- linux网络命令 (55)
- linux传文件 (60)
- linux打包文件 (58)
- linux查看数据库 (61)
- linux获取ip (64)
- linux进程通信 (63)