+ All Categories
Home > Documents > Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause,...

Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause,...

Date post: 23-Jan-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
53
1 1. Problēma. Vienas rindas salīdzināšana ar citām rindām. SELECT FROM T1 a WHERE nevar norādīt 2 rindas a SELECT FROM T1 a b WHERE a.KOL1 > b.KOL1 SELECT FROM T1 b WHERE
Transcript
Page 1: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

1

1. Problēma. Vienas rindas salīdzināšana ar citām rindām.

SELECT FROM T1 a WHERE nevar norādīt 2 rindas

a

SELECTFROM T1 a b WHERE a.KOL1 > b.KOL1

SELECTFROM T1 bWHERE

Page 2: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

2

2. Problēma. Kā vienlaicīgi iegūt vērtību no tabulas rindas un agregētu vērtību no vairākām tabulas rindām?

Tabula DARBINIEKINODALAS_NUMURS

UZVARDS ALGA

1 Koks 2001 Zars 3502 Sakne 3002 Ozols 2502 Liepa 350

Vaicājuma rezultātsUZVARDS ALGA Kopeja_algaKoks 200 550Zars 350 550Sakne 300 900Ozols 250 900Liepa 350 900

Page 3: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

3

Klona tabulas un analītisko funkciju izmantošana

Analītiskās funkcijas aprēķina kopvērtību jeb agregāta vērtību, izmantojot datu rakstu (rindu) grupu. Tās atšķiras no parastām apkopojuma funkcijām ar to, ka tās atgriezīsies dažādas rindas katrai grupai. Rindu grupu sauc par logu un tas tiek definēts ar analītisko teikumu (analytic_clause). Katrai rindai tiek veidots slīdošs rindu logs. Loga rindas tiek izmantotas, lai veiktu aprēķinus par pašreizējo rindu. Rindu apjomu logā var noteikt kā skaitli (skaits) vai loģisku vērtību (laika periods).Analītiskās funkcijas parasti izmanto, lai aprēķinātu kumulatīvas, dināmiskas, centrētas un citas atskaišu vērtības.

Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.

analytic_function::=

Specify the name of an analytic function. Analytic functions take 0 to 3 arguments. The arguments can be any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence and implicitly converts the remaining arguments to that data type. The return type is also that data type, unless otherwise noted for an individual function. Use OVER analytic_clause to indicate that the function operates on a query result set. This clause is computed after the FROM, WHERE, GROUP BY, and HAVING clauses. You can specify analytic functions with this clause in the select list or ORDER BY clause. To filter the results of a query based on an analytic function, nest these functions within the parent query, and then filter the results of the nested subquery. You cannot nest analytic functions by specifying any analytic function in any part of the analytic_clause. However, you can specify an analytic function in a subquery and compute another analytic function over it.You can specify OVER analytic_clause with user-defined analytic functions as well as built-in analytic functions.

analytic_clause::=

Use the PARTITION BY clause to partition the query result set into groups based on

Page 4: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

4

one or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group.To use the query_partition_clause in an analytic function, use the upper branch of the syntax (without parentheses). To use this clause in a model query (in the model_column_clauses) or a partitioned outer join (in the outer_join_clause), use the lower branch of the syntax (with parentheses).You can specify multiple analytic functions in the same query, each with the same or different PARTITION BY keys.If the objects being queried have the parallel attribute, and if you specify an analytic function with the query_partition_clause, then the function computations are parallelized as well.Valid values of value_expr are constants, columns, nonanalytic functions, function expressions, or expressions involving any of these.Use the order_by_clause to specify how data is ordered within a partition. For all analytic functions you can order the values in a partition on multiple keys, each defined by a value_expr and each qualified by an ordering sequence.Within each function, you can specify multiple ordering expressions. Doing so is especially useful when using functions that rank values, because the second expression can resolve ties between identical values for the first expression.Whenever the order_by_clause results in identical values for multiple rows, the function behaves as follows:

1) CUME_DIST, DENSE_RANK, NTILE, PERCENT_RANK, and RANK return the same result for each of the rows;

2) ROW_NUMBER assigns each row a distinct value even if there is a tie based on the order_by_clause. The value is based on the order in which the row is processed, which may be nondeterministic if the ORDER BY does not guarantee a total ordering.

3) for all other analytic functions, the result depends on the window specification. If you specify a logical window with the RANGE keyword, then the function returns the same result for each of the rows. If you specify a physical window with the ROWS keyword, then the result is nondeterministic.

Restrictions on the ORDER BY Clause The following restrictions apply to the ORDER BY clause:

1) when used in an analytic function, the order_by_clause must take an expression (expr). The SIBLINGS keyword is not valid (it is relevant only in hierarchical queries). Position (position) and column aliases (c_alias) are also invalid. Otherwise this order_by_clause is the same as that used to order the overall query or subquery.

2) an analytic function that uses the RANGE keyword can use multiple sort keys in its ORDER BY clause if it specifies any of the following windows:

Page 5: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

5

- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The short form of this is RANGE UNBOUNDED PRECEDING.

- RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING- RANGE BETWEEN CURRENT ROW AND CURRENT ROW- RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED

FOLLOWINGWindow boundaries other than these four can have only one sort key in the ORDER BY clause of the analytic function. This restriction does not apply to window boundaries specified by the ROW keyword.ASC | DESC Specify the ordering sequence (ascending or descending). ASC is the default.NULLS FIRST | NULLS LAST Specify whether returned rows containing nulls should appear first or last in the ordering sequence.NULLS LAST is the default for ascending order, and NULLS FIRST is the default for descending order.Analytic functions always operate on rows in the order specified in the order_by_clause of the function. However, the order_by_clause of the function does not guarantee the order of the result. Use the order_by_clause of the query to guarantee the final result ordering.windowing_clauseSome analytic functions allow the windowing_clause. In the listing of analytic functions at the end of this section, the functions that allow the windowing_clause are followed by an asterisk (*).ROWS | RANGE These keywords define for each row a window (a physical or logical set of rows) used for calculating the function result. The function is then applied to all the rows in the window. The window moves through the query result set or partition from top to bottom.

1) ROWS specifies the window in physical units (rows). 2) RANGE specifies the window as a logical offset.

You cannot specify this clause unless you have specified the order_by_clause. Some window boundaries defined by the RANGE clause let you specify only one expression in the order_by_clause. The value returned by an analytic function with a logical offset is always deterministic. However, the value returned by an analytic function with a physical offset may produce nondeterministic results unless the ordering expression results in a unique ordering. You may have to specify multiple columns in the order_by_clause to achieve this unique ordering.BETWEEN ... AND Use the BETWEEN ... AND clause to specify a start point and end point for the window. The first expression (before AND) defines the start point and the second expression (after AND) defines the end point.

Page 6: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

6

If you omit BETWEEN and specify only one end point, then Oracle considers it the start point, and the end point defaults to the current row.UNBOUNDED PRECEDING Specify UNBOUNDED PRECEDING to indicate that the window starts at the first row of the partition. This is the start point specification and cannot be used as an end point specification.UNBOUNDED FOLLOWING Specify UNBOUNDED FOLLOWING to indicate that the window ends at the last row of the partition. This is the end point specification and cannot be used as a start point specification.CURRENT ROW As a start point, CURRENT ROW specifies that the window begins at the current row or value (depending on whether you have specified ROW or RANGE, respectively). In this case the end point cannot be value_expr PRECEDING.As an end point, CURRENT ROW specifies that the window ends at the current row or value (depending on whether you have specified ROW or RANGE, respectively). In this case the start point cannot be value_expr FOLLOWING.value_expr PRECEDING or value_expr FOLLOWING For RANGE or ROW:

1) if value_expr FOLLOWING is the start point, then the end point must be value_expr FOLLOWING.

2) If value_expr PRECEDING is the end point, then the start point must be value_expr PRECEDING.

If you are defining a logical window defined by an interval of time in numeric format, then you may need to use conversion functions.If you specified ROWS:1) value_expr is a physical offset. It must be a constant or expression and must evaluate to a positive numeric value.2) if value_expr is part of the start point, then it must evaluate to a row before the end point.If you specified RANGE:

1) value_expr is a logical offset. It must be a constant or expression that evaluates to a positive numeric value or an interval literal. Refer to "Literals" for information on interval literals.

2) you can specify only one expression in the order_by_clause.3) if value_expr evaluates to a numeric value, then the ORDER BY expr must be

a numeric or DATE data type.4) if value_expr evaluates to an interval value, then the ORDER BY expr must be

a DATE data type.If you omit the windowing_clause entirely, then the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.Analytic functions are commonly used in data warehousing environments. In the list of analytic functions that follows, functions followed by an asterisk (*) allow the full syntax, including the windowing_clause.query_partition_clause::=

Page 8: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

8

SQL for Analysis and ReportingAnalytic Functions

Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window and is defined by the analytic_clause. For each row, a sliding window of rows is defined. The window determines the range of rows used to perform the calculations for the current row. Window sizes can be based on either a physical number of rows or a logical interval such as time.Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in 1) the select list;2) ORDER BY clause.Analytic functions are commonly used to compute cumulative, moving, centered and reporting aggregates.

Page 9: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

9

SQL papildinājumi datu analīzei

1. Kārtošanas (ranking), klona tabulas (windowing) un pārskatu

funkcijas.

2. Uzlabotās agregātfunkcijas datu analīzei.

3. Šķērstabulu (pivoting) darbības.

4. Datu sablīvēšana pārskatu veidošanai.

5. Laika sēriju aprēķini sablīvētiem datiem.

6. Dažādu analīžu (miscellaneous analysis) iespējas.

Type Used forRanking  Calculating ranks, percentiles, and n-tiles of the values in a

result set. Windowing  Calculating cumulative and moving aggregates. Works with

these functions: SUM, AVG, MIN, MAX, COUNT, VARIANCE, STDDEV, FIRST_VALUE, LAST_VALUE, and new statistical functions 

Reporting  Calculating shares (akcijas), for example, market share. Works with these functions: SUM, AVG, MIN, MAX, COUNT (with/without DISTINCT), VARIANCE, STDDEV, RATIO_TO_REPORT, and new statistical functions 

LAG/LEAD  Finding a value in a row a specified number of rows from a current row. 

FIRST/LAST  First or last value in an ordered group. Linear Regression  Calculating linear regression and other statistics (slope,

intercept, and so on). Inverse Percentile  The value in a data set that corresponds to a specified

percentile. Hypothetical Rank and Distribution 

The rank or percentile that a row would have if inserted into a specified data set. 

Page 10: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

10

Sliding Window Example

Page 11: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

11

Analītisko funkciju pamatkoncepcijas

1. Processing order. Query processing using analytic functions takes place in three stages. First, all joins, WHERE, GROUP BY and HAVING clauses are performed. Second, the result set is made available to the analytic functions, and all their calculations take place. Third, if the query has an ORDER BY clause at its end, the ORDER BY is processed to allow for precise output ordering.

2. Result set partitions. The analytic functions allow users to divide query result sets into groups of rows called partitions. Note that the term partitions used with analytic functions is unrelated to the table partitions feature. Throughout this chapter, the term partitions refers to only the meaning related to analytic functions. Partitions are created after the groups defined with GROUP BY clauses, so they are available to any aggregate results such as sums and averages. Partition divisions may be based upon any desired columns or expressions. A query result set may be partitioned into just one partition holding all the rows, a few large partitions, or many small partitions holding just a few rows each.

3. Window. For each row in a partition, you can define a sliding window of data. This window determines the range of rows used to perform the calculations for the current row. Window sizes can be based on either a physical number of rows or a logical interval such as time. The window has a starting row and an ending row. Depending on its definition, the window may move at one or both ends. For instance, a window defined for a cumulative sum function would have its starting row fixed at the first row of its partition, and its ending row would slide from the starting point all the way to the last row of the partition. In contrast, a window defined for a moving average would have both its starting and end points slide so that they maintain a constant physical or logical range.A window can be set as large as all the rows in a partition or just a sliding window of one row within a partition. When a window is near a border, the function returns results for only the available rows, rather than warning you that the results are not what you want.

4. Current row. Each calculation performed with an analytic function is based on a current row within a partition. The current row serves as the reference point determining the start and end of the window. For instance, a centered moving average calculation could be defined with a window that holds the current row, the six preceding rows, and the following six rows.

Page 12: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

12

Analītiskās funkcijas

COUNT*COVAR_POP*COVAR_SAMP*CUME_DISTDENSE_RANKFIRSTFIRST_VALUE*LAGLASTLAST_VALUE*LEADLISTAGGMAX*MEDIANMIN*NTH_VALUE*NTILEPERCENT_RANKPERCENTILE_CONTPERCENTILE_DISCRANKRATIO_TO_REPORTREGR_ (Linear Regression) Functions*ROW_NUMBERSTDDEV*STDDEV_POP*STDDEV_SAMP*SUM*VAR_POP*VAR_SAMP*VARIANCE*

Page 13: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

13

Time Series Calculations on Densified Data

Densification is not just for reporting purpose. It also enables certain types of calculations, especially, time series calculations. Time series calculations are easier when data is dense along the time dimension. Dense data has a consistent number of rows for each time periods which in turn make it simple to use analytic window functions with physical offsets.

Period-to-Period Comparison for One Time Level: Example

How do you use this feature to compare values across time periods? Specifically, how do you calculate a year-over-year sales comparison at the week level? The following query returns on the same row, for each product, the year-to-date sales for each week of 2001 with that of 2000.

Period-to-Period Comparison for Multiple Time Levels: Example

While the prior example shows us a way to create comparisons for a single time level, it would be even more useful to handle multiple time levels in a single query. For example, you could compare sales versus the prior period at the year, quarter, month and day levels. How can you create a query which performs a year-over-year comparison of year-to-date sales for all levels of our time hierarchy?

Page 14: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

14

Klona tabulas (windowing) izmantošana

Papildus konstrukcija SELECT vaicājumos: OVER (ORDER BY ROWS | RANGE (( UNBOUNDED PRECEDING | izteiksme PRECEDING) | BETWEEN ( UNBOUNDED PRECEDING | izteiksme PRECEDING) AND (CURRENT ROW | izteiksme FOLLOWING ))

Aktuāls vaicājums: Kā vienlaicīgi iegūt vērtību no tabulas rindas un agregētu vērtību no vairākām tabulas rindām?

Tabula DARBINIEKINODALAS_NUMURS UZVARDS ALGA1 Koks 2001 Zars 3502 Sakne 3002 Ozols 2502 Liepa 350

Vaicājuma rezultātsUZVARDS ALGA Kopeja_algaKoks 200 550Zars 350 550Sakne 300 900Ozols 250 900Liepa 350 900

select UZVARDS, ALGA, SUM(ALGA) OVER(order by NODALAS_NUM) Kopeja_algafrom DARBINIEKI;

Page 15: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

15

Piemēri klona tabulas izmantošanai. Tabulas izveidošana

create table DARBINIEKI(D_NUM number Primary Key,UZV varchar2(10),NODALA number,AMATS varchar2(15),ALGA number(10,2));

begininsert into DARBINIEKI values(1001, 'Koks', 1, 'direktors', 500);insert into DARBINIEKI values(1002, 'Celms', 2, 'direktors', 550);insert into DARBINIEKI values(1003, 'Sakne', 3, 'direktors', 400);insert into DARBINIEKI values(1004, 'Lapa', 1, 'sekretāre', 300);insert into DARBINIEKI values(1005, 'Oga', 2, 'sekretāre', 250);insert into DARBINIEKI values(1006, 'Zars', 3, 'sekretāre', 150);insert into DARBINIEKI values(1007, 'Irbe', 1, 'galdnieks', 400);insert into DARBINIEKI values(1008, 'Cauna', 2, 'skārdnieks', 500);insert into DARBINIEKI values(1009, 'Sesks', 3, 'krāsotājs', 450);end;

select * from DARBINIEKI;

D_NUM UZV NODALA AMATS ALGA---------------------------------------------------------------------------------------------------- 1001 Koks 1 direktors 500 1002 Celms 2 direktors 550 1003 Sakne 3 direktors 400 1004 Lapa 1 sekretāre 300 1005 Oga 2 sekretāre 250 1006 Zars 3 sekretāre 150 1007 Irbe 1 galdnieks 400 1008 Cauna 2 skārdnieks 500 1009 Sesks 3 krāsotājs 450

Page 16: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

16

Piemēri klona tabulas izmantošanaiselect NODALA, SUM(ALGA) ALGU_SUMMAfrom DARBINIEKIgroup by NODALA;

NODALA ALGU_SUMMA-------------------------------------------- 1 1200 2 1300 3 1000

select NODALA, ALGA, SUM(ALGA) OVER (PARTITION BY NODALA) ALGU_SUMMA from DARBINIEKI;

NODALA ALGA ALGU_SUMMA-------------------------------------------------------------------- 1 500 1200 1 300 1200 1 400 1200 2 550 1300 2 500 1300 2 250 1300 3 400 1000 3 450 1000 3 150 1000

Page 17: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

17

Piemēri klona tabulas izmantošanai

select NODALA, ALGA, ALGA*100/SUM(ALGA) OVER (PARTITION BY NODALA) ALGAS_PROCENTSfrom DARBINIEKI;

NODALA ALGA ALGAS_PROCENTS---------------------------------------------------------------- 1 500 41,6666667 1 300 25 1 400 33,3333333 2 550 42,3076923 2 500 38,4615385 2 250 19,2307692 3 400 40 3 450 45 3 150 15

select UZV, NODALA, ALGA, RANK() OVER (PARTITION BY NODALA ORDER BY ALGA) ALGAS_RANGSfrom DARBINIEKI;

UZV NODALA ALGA ALGAS_RANGS----------------------------------------------------------------------------Lapa 1 300 1Irbe 1 300 1 Vienādās vērtībasKoks 1 500 3Oga 2 250 1Cauna 2 500 2Celms 2 550 3Zars 3 150 1Sakne 3 400 2Sesks 3 450 3

Page 18: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

18

Piemēri klona tabulas izmantošanai

select UZV, NODALA, ALGA, DENSE_RANK() OVER (PARTITION BY NODALA ORDER BY ALGA) ALGAS_RANGSfrom DARBINIEKI;

UZV NODALA ALGA ALGAS_RANGS--------------------------------------------------------------------Lapa 1 300 1Irbe 1 300 1 Vienādās vērtībasKoks 1 500 2Oga 2 250 1Cauna 2 500 2Celms 2 550 3Zars 3 150 1Sakne 3 400 2Sesks 3 450 3

select UZV, NODALA, ALGA, SUM(ALGA) OVER (PARTITION BY NODALA ORDER BY ALGA RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) asALGU_SUMMAfrom DARBINIEKI;

UZV NODALA ALGA ALGU_SUMMA----------------------------------------------------------------------Lapa 1 300 300Irbe 1 400 700Koks 1 500 1200Oga 2 250 250Cauna 2 500 750Celms 2 550 1300Zars 3 150 150Sakne 3 400 550Sesks 3 450 1000

Page 19: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

19

Piemēri klona tabulas izmantošanai

select UZV, NODALA, ALGA, MAX(ALGA) OVER (PARTITION BY NODALA ORDER BY ALGA DESC ROWS 1 PRECEDING) AS ALGA_MAXfrom DARBINIEKI;

UZV NODALA ALGA ALGA_MAX----------------------------------------------------------------------Koks 1 500 500Irbe 1 400 500Lapa 1 300 400Celms 2 550 550Cauna 2 500 550Oga 2 250 500Sesks 3 450 450Sakne 3 400 450Zars 3 150 400

Page 20: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

20

create table DARBINIEKI(NUM number Primary key,UZV varchar2(10) NOT NULL,DAT date NOT NULL,DARBS varchar2(15),ALGA number(8,2),VAD number,DEP number);

insert into DARBINIEKI values(1, 'Koks', TO_DATE('07-05-2000', 'DD-MM-YYYY', 'projekti', 1500, NULL, 1 );insert into DARBINIEKI values(2, 'Stumbrs', TO_DATE('10-07-2000', 'DD-MM-YYYY'), 'projekts1', 1200, 1, 1 );insert into DARBINIEKI values(3, 'Sakne', TO_DATE('10-07-2000', 'DD-MM-YYYY'), 'projekt2', 1200, 1, 2 );insert into DARBINIEKI values(4, 'Lapa', TO_DATE('21-03-2001', 'DD-MM-YYYY'), 'projekts1', 1000, 2, 1 );insert into DARBINIEKI values(5, 'Zars', TO_DATE('22-04-2002', 'DD-MM-YYYY'), 'projekts1', 900, 2, 1 );insert into DARBINIEKI values(6, 'Priede', TO_DATE('15-11-2001', 'DD-MM-YYYY'), 'projekts2', 1100, 3, 2 );insert into DARBINIEKI values(7, 'Egle', TO_DATE('17-10-2002', 'DD-MM-YYYY'), 'projekts2', 800, 3, 2 ); 

Page 21: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

21

LAG funkcija (iepriekējais)

It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LAG provides access to a row at a given physical offset prior to that position.For the optional offset argument, specify an integer that is greater than zero. If you do not specify offset, then its default is 1. The optional default value is returned if the offset goes beyond the scope of the window. If you do not specify default, then its default is null.{RESPECT | IGNORE} NULLS determines whether null values of value_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.You cannot nest analytic functions by using LAG or any other analytic function for value_expr. However, you can use other built-in function expressions for value_expr.

select DAT, UZV, ALGA, LAG(ALGA, 1, 0 ) OVER (ORDER BY DAT) as iepriekš_alga from DARBINIEKI where DARBS = 'projekts1' order by DAT;

Page 22: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

22

LEAD funkcija (uz priekšu)

It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LEAD provides access to a row at a given physical offset beyond that position.If you do not specify offset, then its default is 1. The optional default value is returned if the offset goes beyond the scope of the table. If you do not specify default, then its default value is null.{RESPECT | IGNORE} NULLS determines whether null values of value_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.You cannot nest analytic functions by using LEAD or any other analytic function for value_expr. However, you can use other built-in function expressions for value_expr.

select DAT, UZV, ALGA, LEAD(ALGA, 1, 0 ) OVER (ORDER BY DAT) as iepriekš_alga from DARBINIEKI where DARBS = 'projekts1' order by DAT;

Page 23: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

23

LISTAGG funkcija

For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column.1. As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.2. As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.3. As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause.The arguments to the function are subject to the following rules:

1) the measure_expr can be any expression. Null values in the measure column are ignored.2) the delimiter_expr designates the string that is to separate the measure values. This clause is

optional and defaults to NULL.3) the order_by_clause determines the order in which the concatenated values are returned.

The function is deterministic only if the ORDER BY column list achieved unique ordering.The return data type is RAW if the measure column is RAW; otherwise the return value is VARCHAR2.

select LISTAGG(UZV, '; ') WITHIN GROUP (ORDER BY DAT, UZV) "Saraksts", MIN(DAT) "Agrākais" from DARBINIEKI where DEP =1;

select DEP as "Departaments", LISTAGG(UZV, '; ') WITHIN GROUP (ORDER BY DAT) "Darbinieki" from DARBINIEKI group by DEP order by DEP;

select DEP as "Departaments", DAT "Datums", UZV "Uzvārds", LISTAGG(UZV, '; ') WITHIN GROUP (ORDER BY DAT, UZV) OVER(PARTITION BY DEP) as "Darbin_saraksts"

from DARBINIEKI where DAT < TO_DATE('01-07-2001', 'DD-MM-YYYY') order by DEP, DAT, UZV;

Page 24: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

24

RATIO_TO_REPORT funkcija

RATIO_TO_REPORT is an analytic function. It computes the ratio of a value to the sum of a set of values. If expr evaluates to null, then the ratio-to-report value also evaluates to null.The set of values is determined by the query_partition_clause. If you omit that clause, then the ratio-to-report is computed over all rows returned by the query.You cannot nest analytic functions by using RATIO_TO_REPORT or any other analytic function for expr. However, you can use other built-in function expressions for expr.

select UZV, ALGA, RATIO_TO_REPORT(ALGA) OVER() as Attiecfrom DARBINIEKIWHERE DARBS = 'projekts1'order by UZV, ALGA, Attiec

Page 25: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

25

NTILE funkcija

NTILE is an analytic function. It divides an ordered data set into a number of buckets indicated by expr and assigns the appropriate bucket number to each row. The buckets are numbered 1 through expr. The expr value must resolve to a positive constant for each partition. Oracle Database expects an integer, and if expr is a noninteger constant, then Oracle truncates the value to an integer. The return value is NUMBER.The number of rows in the buckets can differ by at most 1. The remainder values (the remainder of number of rows divided by buckets) are distributed one for each bucket, starting with bucket 1.If expr is greater than the number of rows, then a number of buckets equal to the number of rows will be filled, and the remaining buckets will be empty.You cannot nest analytic functions by using NTILE or any other analytic function for expr. However, you can use other built-in function expressions for expr.

select UZV, ALGA, NTILE(3) OVER (ORDER BY ALGA DESC) Dalījumsfrom DARBINIEKIwhere DEP = 1order by UZV, ALGA, Dalījums;

Page 26: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

26

NTH_VALUE funkcija

NTH_VALUE returns the measure_expr value of the nth row in the window defined by the analytic_clause. The returned value has the data type of the measure_expr.

1) {RESPECT | IGNORE} NULLS determines whether null values of measure_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.

2) n determines the nth row for which the measure value is to be returned. n can be a constant, bind variable, column, or an expression involving them, as long as it resolves to a positive integer. The function returns NULL if the data source window has fewer than n rows. If n is null, then the function returns an error.

3) FROM {FIRST | LAST} determines whether the calculation begins at the first or last row of the window. The default is FROM FIRST.

If you omit the windowing_clause of the analytic_clause, it defaults to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This default sometimes returns an unexpected value for NTH_VALUE ... FROM LAST ... , because the last value in the window is at the bottom of the window, which is not fixed. It keeps changing as the current row changes. For expected results, specify the windowing_clause as RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Alternatively, you can specify the windowing_clause as RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING.

The following example shows the minimum amount_sold value for the second channel_id in ascending order for each prod_id between 13 and 16:

SELECT prod_id, channel_id, MIN(amount_sold), NTH_VALUE(MIN(amount_sold), 2) OVER (PARTITION BY prod_id ORDER BY channel_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) nv FROM sales WHERE prod_id BETWEEN 13 and 16 GROUP BY prod_id, channel_id;

PROD_ID CHANNEL_ID MIN(AMOUNT_SOLD) NV---------- ---------- ---------------- ---------- 13 2 907.34 906.2 13 3 906.2 906.2 13 4 842.21 906.2 14 2 1015.94 1036.72 14 3 1036.72 1036.72 14 4 935.79 1036.72 15 2 871.19 871.19 15 3 871.19 871.19 15 4 871.19 871.19 16 2 266.84 266.84

Page 27: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

27

16 3 266.84 266.84 16 4 266.84 266.84 16 9 11.99 266.84

Page 28: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

28

Piemēri klona tabulas izmantošanai laika datu gadījumāselect * from ALGA_T;

UZV NODALA ALGA DATUMS------------------------------------------------------------Koks 1 150 2000.01.10Koks 1 160 2000.02.10Koks 1 170 2000.03.10Koks 1 180 2000.04.10Koks 1 190 2000.05.10Koks 1 200 2000.06.10Celms 2 150 2000.02.10Celms 2 160 2000.03.10Celms 2 160 2000.04.10Celms 2 170 2000.05.10Celms 2 190 2000.06.10Celms 2 200 2000.07.10

SELECT UZV, ALGA, SUM(ALGA) OVER (ORDER BY ALGA ROWS 1 PRECEDING) as PLUS_ALGA_IEPRIEKŠĒJĀ from ALGA_Twhere NODALA =1;

UZV ALGA PLUS_ALGA_IEPRIEKŠĒJĀ-----------------------------------------------------------Koks 150 150Koks 160 310Koks 170 330Koks 180 350Koks 190 370Koks 200 390

Page 29: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

29

Piemēri klona tabulas izmantošanai laika datu gadījumā

select UZV, ALGA, SUM(ALGA) OVER (ORDER BY DATUMS RANGE BETWEEN CURRENT ROW AND INTERVAL '1' MONTH FOLLOWING) as PLUS_MĒNESIS_UZ_PRIEKŠUfrom ALGA_Twhere NODALA =1;

UZV ALGA PLUS_MĒNESIS_UZ_PRIEKŠU-----------------------------------------------------------------------------Koks 150 310Koks 160 330Koks 170 350Koks 180 370Koks 190 390Koks 200 200

select UZV, ALGA, LAST_VALUE(ALGA) OVER (ORDER BY ALGA ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS NĀKOŠĀfrom ALGA_Twhere NODALA =1;

UZV ALGA NĀKOŠĀ--------------------------------------------Koks 150 160Koks 160 170Koks 170 180Koks 180 190Koks 190 200Koks 200 200

Page 30: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

30

Piemēri klona tabulas izmantošanai laika datu gadījumā

select UZV, ALGA, ALGA - LAST_VALUE(ALGA) OVER (ORDER BY ALGA ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS NĀKOŠĀfrom ALGA_Twhere NODALA =1;

UZV ALGA NĀKOŠĀ------------------------------------------------Koks 150 -10Koks 160 -10Koks 170 -10Koks 180 -10Koks 190 -10Koks 200 0

select A.UZV, A.ALGA, ABS(A.STARPĪBA)from (select UZV, ALGA, ALGA - LAST_VALUE(ALGA) OVER (ORDER BY ALGA ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) as STARPĪBA from ALGA_T where NODALA =1) A ;

UZV ALGA ABS(A.NAKOSA)--------------------------------------------------------Koks 150 10Koks 160 10Koks 170 10Koks 180 10Koks 190 10Koks 200 0

Page 31: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

31

Piemēri klona tabulas izmantošanai laika datu gadījumā

select A.UZV, MAX(ABS(A.STARPĪBA))from (select UZV, ALGA, ALGA - LAST_VALUE(ALGA) OVER (ORDER BY ALGA ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) as STARPĪBA from ALGA_T where NODALA =1) Agroup by A.UZV;

UZV MAX(ABS(A.STARPĪBA))------------------------------------------------Koks 10

select CASE WHEN ABS(A.NAKOSA) <5 THEN 'Zem 5' WHEN ABS(A.NAKOSA) >= 5 THEN 'Virs 5' END, COUNT(ABS(A.NAKOSA))from (select UZV, ALGA, ALGA - LAST_VALUE(ALGA) OVER (ORDER BY ALGA ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS NAKOSA from ALGA_T where NODALA =1) A group by CASE WHEN ABS(A.NAKOSA) <5 THEN 'Zem 5' WHEN ABS(A.NAKOSA) >= 5 THEN 'Virs 5' END;

CASE WH COUNT(ABS(A.NAKOSA))-----------------------------------------------------------------Virs 5 5Zem 5 1

Page 32: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

32

Piemēri klona tabulas izmantošanai laika datu gadījumā

create table D_ALGA (ID number Primary Key,UZV varchar2(10),DZIMUMS char(8),VIENIBA number,ALGA number(9,2), DATUMS date);

select * from D_ALGA;

ID UZV DZIMUMS VIENIBA ALGA DATUMS------------------------------------------------------------------------------ 1 Koks sieviete 1 150 2002.01.18 2 Koks sieviete 1 160 2002.02.18 3 Koks sieviete 1 160 2002.03.18 4 Koks sieviete 1 160 2002.04.18 5 Koks sieviete 1 170 2002.05.18 6 Celms vīrietis 2 160 2002.01.18 7 Celms vīrietis 2 160 2002.02.18 8 Celms vīrietis 2 170 2002.03.18 9 Celms vīrietis 2 170 2002.04.18 10 Celms vīrietis 2 180 2002.05.18 11 Celms vīrietis 2 180 2002.06.18

select ID, ALGA, 100*CUME_DIST() OVER (PARTITION BY UZV ORDER BY ALGA)from D_ALGAwhere VIENIBA = 1;

ID ALGA 100*CUME_DIST()OVER(PARTITION BY UZV ORDER BY ALGA)---------- ---------- ---------------------------------------------- 1 150 20 2 160 80 3 160 80 4 160 80 5 170 100

Page 33: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

33

Piemēri klona tabulas izmantošanai laika datu gadījumā

select ID, ALGA, LAG(ALGA,1) OVER (PARTITION BY UZV ORDER BY ALGA)from D_ALGAwhere VIENIBA = 1;

ID ALGA LAG(ALGA,1)OVER(PARTITIONBYUZVORDERBYALGA)-------------------------------------------------------------------------------------------------- 1 150 2 160 150 3 160 160 4 160 160 5 170 160

Page 34: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

34

Piemēri klona tabulas izmantošanai laika datu gadījumā

select ID, ALGA, LEAD(ALGA,1) OVER (PARTITION BY UZV ORDER BY ALGA)from D_ALGAwhere VIENIBA = 1;

ID ALGA LEAD(ALGA,1)OVER(PARTITIONBYUZVORDERBYALGA)-------------------------------------------------------------------------------------------------- 1 150 160 2 160 160 3 160 160 4 160 170 5 170

Page 35: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

35

Sample of Sparse DataA typical situation with a sparse dimension is shown in the following example, which computes the weekly sales and year-to-date sales for the product Bounce for weeks 20-30 in 2000 and 2001:

PRODUCT_NAME YEAR WEEK SALES--------------- ---------- ---------- ----------Bounce 2000 20 801Bounce 2000 21 4062.24Bounce 2000 22 2043.16Bounce 2000 23 2731.14Bounce 2000 24 4419.36Bounce 2000 27 2297.29Bounce 2000 28 1443.13Bounce 2000 29 1927.38Bounce 2000 30 1927.38Bounce 2001 20 1483.3Bounce 2001 21 4184.49Bounce 2001 22 2609.19Bounce 2001 23 1416.95Bounce 2001 24 3149.62Bounce 2001 25 2645.98Bounce 2001 27 2125.12Bounce 2001 29 2467.92Bounce 2001 30 2620.17

Page 36: Purpose - Datu bāzes tehnoloģijas · Web viewone or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. To use

36

PRODUCT_NAME YEAR WEEK DENSE_SALES--------------- ---------- ---------- -----------Bounce 2000 20 801Bounce 2000 21 4062.24Bounce 2000 22 2043.16Bounce 2000 23 2731.14Bounce 2000 24 4419.36Bounce 2000 25 0Bounce 2000 26 0Bounce 2000 27 2297.29Bounce 2000 28 1443.13Bounce 2000 29 1927.38Bounce 2000 30 1927.38Bounce 2001 20 1483.3Bounce 2001 21 4184.49Bounce 2001 22 2609.19Bounce 2001 23 1416.95Bounce 2001 24 3149.62Bounce 2001 25 2645.98Bounce 2001 26 0Bounce 2001 27 2125.12Bounce 2001 28 0Bounce 2001 29 2467.92Bounce 2001 30 2620.17


Recommended