当前位置: 代码网 > it编程>数据库>MsSqlserver > SQLServer 2008 R2中使用Cross apply统计最新数据和最近数据

SQLServer 2008 R2中使用Cross apply统计最新数据和最近数据

2024年05月18日 MsSqlserver 我要评论
使用 apply 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输

使用 apply 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输出。apply 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。

注意:若要使用 apply,数据库兼容级别必须至少为 90。

apply 有两种形式:cross apply 和 outer apply。cross apply 仅返回外部表中通过表值函数生成结果集的行。outer apply 既返回生成结果集的行,也返回不生成结果集的行,其中表值函数生成的列中的值为 null。

好久没写sql了,手都有点生了。哈哈,今天回答个问题。顺便记录下来。

事主的需求

事主的问题应该是想把最新的数据和次新数据放在一行里显示。

因为没有说明重复的情况如何处理,即有多个最新数据或者有多个次新数据,所以我没有做过多的处理。

--by wls
--
--网络代码有风险
--复制粘贴须谨慎
use tempdb
go
if object_id('t_testbywls','u') is not null
drop table t_testbywls
go
create table t_testbywls(pname nvarchar(),psid integer,chkdate nvarchar(),price float)
go
insert into t_testbywls values
('a',,'',.)
,('b',,'',.)
--,('b',,'',.)
,('a',,'',.)
,('b',,'',.)
,('a',,'',.)
--,('a',,'',.)
go
select * from t_testbywls
go
/*
select pname,psid,chkdate,price,dense_rank() over(partition by pname order by cast(chkdate as integer) desc ) as drid,
row_number() over(partition by pname order by cast(chkdate as integer) desc,price desc ) as rid
from t_testbywls
go
*/
with tempchkdate
as
(
select pname,psid,chkdate,price,dense_rank() over(partition by pname order by cast(chkdate as integer) desc ) as drid,
row_number() over(partition by pname order by cast(chkdate as integer) desc,price desc ) as rid
from t_testbywls
)
select tcd.pname,tcd.psid,tcd.chkdate,tcd.price,/*tcd.drid,tcd.rid,*/t.tcd,t.tp
from tempchkdate as tcd cross apply(select chkdate as tcd, price as tp 
from tempchkdate
where 
--tcd.drid= and
tcd.pname=tempchkdate.pname and
tcd.psid=tempchkdate.psid and
tempchkdate.drid= 
) as t
where tcd.drid=
go 

运行的结果应该是正确的。

但是看执行计划,不是很好啊。

有空再改改。

你可以尝试一下这个,看看是什么结果。

产生这种原因是因为你没有做出具体规定。

--by wls
--
--网络代码有风险
--复制粘贴须谨慎
------------------------------------------------------------------------
--你可以尝试一下这个,看看是什么结果。
--产生这种原因是因为没有做出具体规定。
------------------------------------------------------------------------
use tempdb
go
if object_id('t_testbywls','u') is not null
drop table t_testbywls
go
create table t_testbywls(pname nvarchar(),psid integer,chkdate nvarchar(),price float)
go
insert into t_testbywls values
('a',,'',.)
,('b',,'',.)
,('b',,'',.)
,('a',,'',.)
,('b',,'',.)
,('a',,'',.)
,('a',,'',.)
go
with tempchkdate
as
(
select pname,psid,chkdate,price,dense_rank() over(partition by pname order by cast(chkdate as integer) desc ) as drid,
row_number() over(partition by pname order by cast(chkdate as integer) desc,price desc ) as rid
from t_testbywls
)
select tcd.pname,tcd.psid,tcd.chkdate,tcd.price,tcd.drid,tcd.rid,t.tcd,t.tp
from tempchkdate as tcd 
cross apply(select chkdate as tcd, price as tp 
from tempchkdate
where 
--tcd.drid= and
tcd.pname=tempchkdate.pname and
tcd.psid=tempchkdate.psid and
tempchkdate.drid= 
) as t
where tcd.drid=
go

以上内容是小编给大家介绍的sqlserver 2008 r2中使用cross apply统计最新数据和最近数据的相关知识,希望对大家有所帮助!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com