Sql 多表查询

多表拼接查询

select t2.ariid, t2.alarmconfigid, t2.alarmtime, t2.alarmdetail, t2.actualduration, t2.isack, t2.acktime, t2.acktype, t2.ackuser, t2.ackdetail, t1.ariid,t1.alarmtypeid, t1.aridesc, t1.ariname from yf_alarmrecord t2 join  yf_alarmconfig t1 on t2.alarmconfigid=t1.ariid

多表关联查询

select t2.aridesc from RM_User t2 join  BM_DataRight t1 on t2.ariid=t1.userid and t1.itemid=5 and t1.enterpriseid=17 and t1.opttype=1 and t1.thetype=8

递归查询

select * from RD_Factory where EnterpriseId = 17 START WITH Pid is null CONNECT BY PRIOR AriId = Pid ORDER siblings BY AriSeq

sql语句从一张表查询一个字段值插入另一个表中

insert into a(name,num,class) values('小米','1001',(select top 1 class from b where name = '小米'));

表格字段连续分段分组查询:

效果

select * from
(
select deviceid, productid,theele, thetime,row_number() over(order by thetime asc) as RN from yf_deviceproductinfo
) a full join
(
select deviceid, productid,theele, thetime,row_number() over(order by thetime asc) as RN from yf_deviceproductinfo
) b on a.RN+1=b.RN
where a.deviceid<>b.Deviceid or a.productid<>b.productId or a.deviceid is null or b.deviceid is null

连续数据分组分段统计实现

CREATE global TEMPORARY TABLE temp  
ON COMMIT PRESERVE ROWS   
AS  
select a.RN,b.RN as RN1,row_number() over(order by  nvl(a.RN,0)) as Num from
(
select deviceid, productid,row_number() over(order by thetime asc) as RN from yf_deviceproductinfo
) a full join
(
select deviceid, productid,row_number() over(order by thetime asc) as RN from yf_deviceproductinfo
) b on a.RN+1=b.RN
where a.deviceid<>b.Deviceid or a.productid<>b.productId or a.deviceid is null or b.deviceid is null;
select s.deviceid,s.productid,sum(theele) as ele,ceil((max(thetime)-min(thetime))*24*60*60) as seconds,count(1) as productnum from
(select deviceid, productid,theele,thetime,row_number() over(order by thetime asc) as RN from yf_deviceproductinfo) s,
(select b.rn1 as F, a.rn as L from temp a inner join temp b on
a.num=b.num+1) n where s.rn>=n.F and s.rn<=n.L
group by s.deviceid,s.productid,n.F order by s.deviceid;
truncate TABLE temp;
drop table temp;

统计所有相同项目的总量:

select deviceid,productid,sum(theele) as ele,ceil((max(thetime)-min(thetime))*24*60*60) as seconds,count(1) as productnum from Yf_deviceProductInfo group by deviceid,productid order by deviceid;

对表格统计后关联多表查询

select A.*, B.AriName as DeviceName, C.AriName as ProductName from (select deviceid,productid,sum(theele) as ele,ceil((max(thetime)-min(thetime))*24*60*60) as seconds,max(thetime)as theStartTime,min(thetime)as theEndTime, count(1) as productnum from Yf_deviceProductInfo group by deviceid,productid order by deviceid) A, YF_Device B, YF_DeviceProduct C where A.DeviceId = B.AriId and A.ProductId = C.AriId

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌霜残雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值