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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

以上是关于sql 申请,交叉申请,外部申请的主要内容,如果未能解决你的问题,请参考以下文章

透视和交叉申请

电子面单打印商家请求参数错误申请的交易订单和已存在的交易订单号存在交叉

SQL子查询计数

ABAP采购申请审批增强Demo

ABAP采购申请审批增强Demo

sql 持有锁的申请