loading..
Ðóññêèé    English
16:51

CROSS APPLY / OUTER APPLY page 2

Let's imagine that along with maximal price, you need minimal price, average price and so on. Correlated subquery in SELECT clause must return only single value, so we are forced to duplicate sql-code for each aggregate:

Console
Execute
  1. SELECT *, (SELECT MAX(price) FROM Laptop L2
  2. JOIN  Product P1 ON L2.model=P1.model
  3. WHERE maker = (SELECT maker FROM Product P2 WHERE P2.model= L1.model)) max_price,
  4. (SELECT MIN(price) FROM Laptop L2
  5. JOIN  Product P1 ON L2.model=P1.model
  6. WHERE maker = (SELECT maker FROM Product P2 WHERE P2.model= L1.model)) min_price
  7. FROM Laptop L1;
etc.

But when using CROSS APPLY, we simply add into subquery a wanted aggregate function as a new column:

Console
Execute
  1. SELECT *
  2.  FROM laptop L1
  3.  CROSS APPLY
  4.  (SELECT MAX(price) max_price, MIN(price) min_price  FROM Laptop L2
  5. JOIN  Product P1 ON L2.model=P1.model
  6. WHERE maker = (SELECT maker FROM Product P2 WHERE P2.model= L1.model)) X;

Another example.

Combine each row in the Laptop table with next row in the order of sorting by (model, code).

The code column is used in sorting to get single-valued ordering for rows with the same value in the model column. We'll use CROSS APPLY operator to pass into subquery the parameters of a current row and take the first row under this current row in the given sorting. So,

Console
Execute
  1. SELECT * FROM laptop L1
  2. CROSS APPLY
  3. (SELECT TOP 1 * FROM Laptop L2
  4. WHERE L1.model < L2.model OR (L1.model = L2.model AND L1.code < L2.code)
  5. ORDER BY model, code) X
  6. ORDER BY L1.model;

Try to solve this problem using standard means and compare the efforts required.

OUTER APPLY operator

As the results of above query have shown, we "lost" the last (sixth) row in Laptop table because there is no row to
combine it with. In other words, CROSS APPLY behaves itself as inner join. An analog of outer (left) join is just OUTER APPLY operator. The latter differs from CROSS APPLY in just that OUTER APPLY returns all the rows from left-hand table when replacing the missing values from right-hand table by NULLs.

Replacement CROSS APPLY by OUTER APPLY illustrates above said.

Console
Execute
  1. SELECT * FROM laptop L1
  2. OUTER APPLY
  3. (SELECT TOP 1 *
  4. FROM Laptop L2
  5. WHERE L1.model < L2.model OR (L1.model = L2.model AND L1.code < L2.code)
  6. ORDER BY model, code) X
  7. ORDER BY L1.model;

Another popular problem is to output the same number of rows for each group, for example when it is needed to advertise the 5 most popular goods within each category. Let's consider the following exercise.

From Product table, retrieve for each group characterized by the type of the product three models with the lowest numbers.

Solutions to this problem on the site sql-ex.ru we'll supplement with another one solution using CROSS APPLY.
The idea of the solution consists in joining of unique product types (the first query) with the query which returns three models of the each type from the first query in accordance with given sorting.

Console
Execute
  1. SELECT X.* FROM
  2. (SELECT DISTINCT type FROM product) Pr1
  3. CROSS APPLY
  4. (SELECT TOP 3 * FROM product Pr2 WHERE  Pr1.type=Pr2.type ORDER BY pr2.model) x;

Next page


Bookmark and Share
Pages 1 2 3
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
https://exchangesumo.com/obmen/SBRFRUB-HCBRUB/
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.