Questions tagged [analytic-functions]

Aggregation functions that can peek at rows returned from the same query. The SQL standard defines them as window functions and the tag `window-functions` should be preferred over `analytical-functions` unless it is something completely Oracle specific

From Oracle documentation page: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions004.htm#SQLRF06174

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 the select list or ORDER BY clause.

Analytic functions are commonly used to compute cumulative, moving, centered, and reporting aggregates.

244 questions
0
votes
1 answer

Apply MIN() OVER only on percentage of the rows

I have the following query: SELECT * FROM empno, ename, deptno, sal, job, MIN(sal) OVER (PARTITION BY deptno, job) AS min_sal_by_dept_and_job FROM emp; With this result: Query result I want the column…
0
votes
2 answers

How to update multiple rows within the same table in Oracle?

I am using Oracle DB Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production I have the following create table script. I want to update multiple rows within the same table with same values For e.g. CREATE TABLE…
Gautam S
  • 41
  • 1
  • 1
  • 7
0
votes
1 answer

Lag function to find previous record value with missing previous record

I have a requirement to fetch previous row or lag records where there are some missing previous ids. Database : Oracle 12c Example…
Vicki
  • 43
  • 7
0
votes
2 answers

How to use DENSE_RANK and order by aditional column?

I am trying to use dense_rank to get element order for instance: I have table seq_test with data: create table seq_test (sequence number, role_name varchar2(20)); insert into seq_test values (10, 'B'); insert into seq_test values (20, 'A'); select…
Goku
  • 441
  • 5
  • 20
0
votes
0 answers

Why does ORDER by itself change the results in an Analytic function?

In the following query: with SalesX as ( select 'Office Supplies' Category , 2014 Year,22593.42 Profit UNION all select 'Technology', 2014, 21492.83 UNION all select 'Furniture', 2014, 5457.73 UNION all select 'Office Supplies', …
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
4 answers

Get specific series values in order

I have a table contains the info of the access time and movements for each employee. Some employees have shift work hours between two days, I have column determine if this access time belongs to the previous or current day. I need to collect the…
M.Youssef
  • 146
  • 9
0
votes
2 answers

Summing over a numeric column with moving window of varying size in Snowflake

I have a sample dataset given as follows; time | time_diff | amount time1 | time1-time2 | 1000 time2 | time2-time3 | 2000 time3 | time3-time4 | 3000 time4 | time4-time5 | 4500 time5 | NULL | 1000 Quick explanation; first column gives…
jay
  • 1,319
  • 6
  • 23
  • 42
0
votes
1 answer

select over partiton by ... order by .. used long time

select a.id from (select /*+index(test_table, test_index)*/ row_number() over (partition by a, b, c order by d desc) rn, id from test_table ) a where a.rn = 1 test_index(a, b, c, d) limit access to 500, cost…
zfy
  • 1
0
votes
1 answer

How can I avoid self join in BigQuery

I have the following table and from there I want to add another column with the previous value: id Previous id Value 30 20 8000 20 10 5000 The output would be as…
Víctor
  • 113
  • 1
  • 6
0
votes
0 answers

Unexpected analytic function output in common table expression

In SQL Server 2019, analytic functions are not returning the results that I would expect in the context of recursive common table expressions. Consider the following non-recursive T-SQL query: WITH SourceData (RowNum, Uniform, RowVal) AS ( SELECT…
0
votes
1 answer

Oracle SQL to Mimic Nested Window Function

My actual problem involves a larger row source and more involved math, but this is a small example that still exhibits the challenge faced. Using Oracle 19c. Suppose we have a table X with four rows of data as follows. x - 1 2 3 4 Further, suppose…
Alex Bartsmon
  • 471
  • 4
  • 9
0
votes
2 answers

listagg produces ORA-01489 if used as window function in conditional expression

My query returns many (thousands of) rows. Column l has certain value for very small amount of rows (up to 10). For each such row I want to output aggregated comma-separated values of very short (up to 5 chars) varchar column v over all of these…
Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
0
votes
1 answer

sum values based on 7-day cycle in SQL Oracle

I have dates and some value, I would like to sum values within 7-day cycle starting from the first date. date value 01-01-2021 1 02-01-2021 1 05-01-2021 1 07-01-2021 1 10-01-2021 1 12-01-2021 1 13-01-2021 1 16-01-2021 …
Pato
  • 153
  • 6
0
votes
2 answers

How to: Oracle analytic function to return row

I guess my question could be answered with an Oracle analytic function in the SQL but I am not to sure. Say I have the following "DOCUMENTS" DB Table: Rank: Reverse Sequence, each document has its own sequence, latest document revision has lowest…
Spanky
  • 111
  • 9
0
votes
2 answers

SQL COUNT all ROWS in Columns

I need to Count all Rows and write it into Columns in ORACLE SQL. It schould look like that (left as it is, right as it should look like): |A|B|C|D| |A|COUNT_A|B|COUNT_B|C|COUNT_C|D|COUNT_D| - - - - - ------- - ------- - ------- -…