SQL for Analysis and Reporting SQL papildinājumi datu analīzei un pārskatu veidošanai 1. Kārtošanas funkcijas (ranking). 2. Pārskatu funkcijas, izmantojot papildus datu logus (windowing). 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. Sliding Window Example 1
29
Embed
SQL for Analysis and Reporting - Web view3/1/2016 · SQL for Analysis and Reporting. SQL papildinājumi datu analīzei un pārskatu veidošanai. 1. Kārtošanas funkcijas...
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SQL for Analysis and Reporting
SQL papildinājumi datu analīzei un pārskatu veidošanai
1. Kārtošanas funkcijas (ranking).
2. Pārskatu funkcijas, izmantojot papildus datu logus (windowing).
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.
Sliding Window Example
1
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.
2
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
select UZV, NODALA, ALGA, SUM(ALGA) OVER (PARTITION BY NODALA ORDER BY ALGA RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) asALGU_SUMMAfrom DARBINIEKI;
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;
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 ;
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;
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
12
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);
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: