栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

何时使用表运算符APPLY

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

何时使用表运算符APPLY

首先-

apply
可以调用 表值函数 ,其中参数值是从查询的表中获取的,如下所示:

select    t1.col3, -- column from table    f1.col1  -- column from functionfrom table1 as t1    left outer join table2 as t2 on t2.col1 = t1.col1    outer apply dbo.function1(t1.col1, t2.col2) as f1

切碎xml列

select    t1.col3,    t.c.value('@value', 'int') as valuefrom table1 as t1    -- table1.col1 is xml iike <Data @Value="...">...</Data>    outer apply t1.col1.nodes('Data') as t(c)

根据我的经验,

apply
当您需要进行一些 预先计算 时,它非常有用:

select    t1.col3,    a1.col1,  --calculated value    a2.col1   -- another calculated value, first one was usedfrom table1 as t1    outer apply (select t1.col1 * 5 as col1) as a1    outer apply (select a1.col1 - 4 as col1) as a2

使用的另一个示例

apply
不可透视的 操作:

select    t1.col1, c.name, c.valuefrom table1 as t1    outer apply (        select 'col1', t1.col1 union all        select 'col2', t1.col2    ) as c(name, value)

最后,这是在不使用 apply的* 情况下根据SQL 2005实现的 查询*

;with cte as (    select        y.Name,         y.hoursWorked,        x.game,        x.NumBets,        row_number() over(partition by x.Name order by x.NumBets) as row_num    from y        left outer join x on x.Name = y.Name)select Name, hoursWorked, game, NumBetsfrom ctewhere row_num <= 2order by Name, NumBets desc


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号