doc-exports/docs/dws/tool/dws_mt_0138.html
Lu, Huayi 346ac31da9 DWS TG 8.1.3.200 VERSION
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Reviewed-by: Jiang, Beibei <beibei.jiang@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2023-08-28 09:20:17 +00:00

160 lines
8.8 KiB
HTML

<a name="EN-US_TOPIC_0000001188681050"></a><a name="EN-US_TOPIC_0000001188681050"></a>
<h1 class="topictitle1">Analytical Functions</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p8060118">Analytical 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. Analytical functions are commonly used to compute cumulative, moving, centered, and reporting aggregates. DSC supports analytical functions including the RATIO_TO_REPORT function.</p>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p6585177111"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b697201417337">Input - <strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b11222213336">Analytical Functions</strong></strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen0989144336">SELECT empno, ename, deptno
, COUNT(*) OVER() AS cnt
, AVG(DISTINCT empno) OVER (PARTITION BY deptno) AS cnt_dst
FROM emp
ORDER BY empno;</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p51051214133320"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b1210611416334">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen1110771410335">WITH aggDistQuery1 AS (
SELECT
deptno
,AVG (
DISTINCT empno
) aggDistAlias1
FROM
emp
GROUP BY
deptno
) SELECT
empno
,ename
,deptno
,COUNT( * ) OVER( ) AS cnt
,(
SELECT
aggDistAlias1
FROM
aggDistQuery1
WHERE
deptno = MigTblAlias.deptno
) AS cnt_dst
FROM
emp MigTblAlias
ORDER BY
empno
;</pre>
<div class="section" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_section11132185520582"><h4 class="sectiontitle">RATIO_TO_REPORT</h4><p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p24446155917">RATIO_TO_REPORT is an analytic function which returns the proportion of a value to a group of values.</p>
</div>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p16778172218220"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b11778162215215">Input - <strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b16778182216216">RATIO_TO_REPORT</strong></strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen8778112211210">SELECT last_name, salary
, RATIO_TO_REPORT(salary) OVER () AS rr
FROM employees
WHERE job_id = 'PU_CLERK';</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p87807221228"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b1578015227218">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen137812022426">SELECT last_name, salary
, salary / NULLIF( SUM (salary) OVER( ), 0 ) AS rr
FROM employees
WHERE job_id = 'PU_CLERK';</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p6919112114220"></p>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p1468320178160"><strong id="EN-US_TOPIC_0000001188681050__b6781029102210">Input - <strong id="EN-US_TOPIC_0000001188681050__b107812942214">RATIO_TO_REPORT</strong></strong><strong id="EN-US_TOPIC_0000001188681050__b97822913224"> with AGGREGATE column in SELECT</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen176849171164">SELECT
Ename
,Deptno
,Empno
,SUM (salary)
,RATIO_TO_REPORT (
COUNT( DISTINCT Salary )
) OVER( PARTITION BY Deptno ) RATIO
FROM
emp1
ORDER BY
Ename
,Deptno
,Empno
;</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p136871217171614"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b1568771741613">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen362718551428">SELECT
Ename
,Deptno
,Empno
,SUM (salary)
,COUNT( DISTINCT Salary ) / NULLIF( SUM ( COUNT( DISTINCT Salary ) ) OVER( PARTITION BY Deptno ) ,0 ) RATIO
FROM
emp1
ORDER BY
Ename
,Deptno
,Empno
;</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p5272135916218"><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b11272759928">Input - <strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b72736592215">RATIO_TO_REPORT</strong></strong><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b1916514381867"> with the AGGREGATE column using extending grouping feature but </strong><strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b10605910152916">OUNT (Salary) in the RATIO TO REPORT column</strong> <strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b12165938968">is not present in SELECT</strong></p>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p196411359410">Use the <strong id="EN-US_TOPIC_0000001188681050__en-us_topic_0237712462_b99161233172310"><a href="dws_mt_0027.html#EN-US_TOPIC_0000001188202590__en-us_topic_0218440495_li121341415427">extendedGroupByClause</a></strong> configuration parameter to configure migration of the extended GROUP BY clause.</p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen6273559427">SELECT
Ename
,Deptno
,Empno
,SUM (salary)
,RATIO_TO_REPORT (
COUNT( Salary )
) OVER( PARTITION BY Deptno ) RATIO
FROM
emp1
GROUP BY
GROUPING SETS (
Ename
,Deptno
,Empno
)
ORDER BY
Ename
,Deptno
,Empno
;</pre>
<p id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_p6276859120"><strong id="EN-US_TOPIC_0000001188681050__b298559365">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188681050__en-us_topic_0238518399_en-us_topic_0237362180_en-us_topic_0202727311_screen427775914210">SELECT
Ename
,Deptno
,Empno
,ColumnAlias1
,aggColumnalias1 / NULLIF( SUM ( aggColumnalias1 ) OVER( PARTITION BY Deptno ) ,0 ) RATIO
FROM
(
SELECT
SUM (salary) AS ColumnAlias1
,COUNT( Salary ) aggColumnalias1
,NULL AS Deptno
,NULL AS Empno
,Ename
FROM
emp1
GROUP BY
Ename
UNION
ALL SELECT
SUM (salary) AS ColumnAlias1
,COUNT( Salary ) aggColumnalias1
,Deptno
,NULL AS Empno
,NULL AS Ename
FROM
emp1
GROUP BY
Deptno
UNION
ALL SELECT
SUM (salary) AS ColumnAlias1
,COUNT( Salary ) aggColumnalias1
,NULL AS Deptno
,Empno
,NULL AS Ename
FROM
emp1
GROUP BY
Empno
)
ORDER BY
Ename
,Deptno
,Empno
;</pre>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_mt_0133.html">System Functions</a></div>
</div>
</div>