热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

sql申请,交叉申请,外部申请

本文由编程笔记#小编为大家整理,主要介绍了sql 申请,交叉申请,外部申请相关的知识,希望对你有一定的参考价值。
本文由编程笔记#小编为大家整理,主要介绍了sql 申请,交叉申请,外部申请相关的知识,希望对你有一定的参考价值。




/*
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.
APPLY is like a JOIN, but it's mainly used to allow you to run a table-valued function against each row returned in a left input result set. For example, say you have a function that pulls the top 3 sales for each salesperson. You can use APPLY to run that function against each row of another result set and pull the top three sales for a list of salespeople (you get three result rows for each one row in the original result set).
There are two main types of APPLY:
CROSS APPLY - will only show results if the function applied to a row in the left result set returns a value (kind of like an INNER JOIN).
OUTER APPLY - will return all rows from left result set, including NULLs for on the right side column if the function does not return a value for that left side row (kind of like a LEFT JOIN).
*/
-- Example of CROSS APPLY being used to run a function that pulls top three sales for a salesperson against a table of salespeople, thereby creating a final result set showing the top three sales for each salesperson.
-- first, create the function that will act as the right input. Function takes input of @SalesPersonID and returns three highest sales for the specified salesperson. Results are a table with two columsn, SalesPersonID and SalesAmount.
CREATE FUNCTION dbo.udf_sales(@SalesPersonID INT)
RETURNS TABLE
AS
RETURN
(
SELECT TOP 3
SalesPersonID,
ROUND(TotalDue, 2) AS SalesAmount
FROM Sales.SalesOrderHeader
WHERE SalesPersOnID= @SalesPersonID
ORDER BY TotalDue DESC
);
-- Second, create a SELECT statement (the left input) with CROSS APPLY which will execute the function for all rows of the result table (ie, it'll pull the top three sales for each employee who has a sale)
SELECT sp.FirstName + ' ' + sp.LastName as FullName, udf.SalesAmount
FROM Sales.vSalesPerson AS sp
CROSS APPLY udf_sales(sp.BusinessEntityID) AS udf
ORDER BY sp.LastName, udf.SalesAmount DESC;
--------------------------------------------
-- DETAIL OF CROSS APPLY SELECT STATEMENT --
--------------------------------------------
-- SELECT the left input table, BusinessEntityID, and FullName (first and last combined) FROM the vSalesPerson view, plus pull a column from the right side, which is SalesAmount which is a column defined in the function udf_sales that will be referenced later and given the alias crossapply. Note how this is set up like a JOIN, since it's pulling columns from two sources.
SELECT sp.BusinessEntityID, sp.FirstName + ' ' + sp.LastName as FullName, crossapply.SalesAmount
FROM Sales.vSalesPerson AS sp
-- CROSS APPLY the udf_sales function to the results from the SELECT statement. sp.BusinessEntityID is the ID value that will be passed as the @SalesPersonID parameter through the function for each row in the left table result set.
CROSS APPLY udf_sales(sp.BusinessEntityID) AS crossapply
-- order the final results by sp.LastName and crossapply.SalesAmount
ORDER BY sp.LastName, sp.BusinessEntityID, crossapply.SalesAmount DESC;


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • PHP反射API的功能和用途详解
    本文详细介绍了PHP反射API的功能和用途,包括动态获取信息和调用对象方法的功能,以及自动加载插件、生成文档、扩充PHP语言等用途。通过反射API,可以获取类的元数据,创建类的实例,调用方法,传递参数,动态调用类的静态方法等。PHP反射API是一种内建的OOP技术扩展,通过使用Reflection、ReflectionClass和ReflectionMethod等类,可以帮助我们分析其他类、接口、方法、属性和扩展。 ... [详细]
  • 本文整理了Java中org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc.getTypeInfo()方法的一些代码示例,展 ... [详细]
  • 概述H.323是由ITU制定的通信控制协议,用于在分组交换网中提供多媒体业务。呼叫控制是其中的重要组成部分,它可用来建立点到点的媒体会话和多点间媒体会议 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • const限定符全解一、const修饰普通变量  intconsta500;  constinta600;  上述两种情况相同,都是声明一个const型的变量,它们 ... [详细]
author-avatar
小清新快乐尘埃2008
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有