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
1
vote
4 answers

Count only when the next row is different from the previous row

I have a table with 12 registers. I want to count the row just if the column "AREA_OPERATIVA" is different from the previous row (ordering by date asc). For example, from row 1 to row 2 it shouldn't count anything because both have same area…
1
vote
2 answers

Using empty OVER() clause in subquery

I have an Oracle SQL query that is using a Correlated Subquery: Q1 SELECT t1.id, t3.code, t3.processed_date, (t1.total / t2.rate) FROM table1 t1 JOIN table2 t2 ON t2.code= t1.code JOIN table3 t3 ON t3.id =…
JTK
  • 1,469
  • 2
  • 22
  • 39
1
vote
1 answer

Use TOP or Rank when finding the first few or the most observations

I have searched others' code and tried to customize in my case, but it seemed when the question was the "max", it worked. When it comes to find the top 100, it was not working. I am trying to get the first 100 people hired in the firm. I first tried…
Chen
  • 383
  • 2
  • 12
1
vote
1 answer

How to get Old Value from Incremental Year values?

I have two tables: Table1: | Year | CRN | CID | Cap | | 201910 | 14 | ABC1 | 12 | | 201910 | 15 | ABC1 | 14 | | 201820 | 25 | ABC1 | 15 | | 201820 | 26 | ABC1 | 25 | | 201810 | 43 | ABC1 | 10 | | 201720 | 55 …
1
vote
2 answers

Query to find changes in a row wrt previous row in SQL query

I have a table per_all_Assignments_f with date_from and date_to and following column structure : PERSON_ID DATE_FROM DATE_TO GRADE --------- ------------ ----------- ----- 12 01-Jan-2018 28-Feb-2018 …
SSA_Tech124
  • 577
  • 1
  • 9
  • 25
1
vote
2 answers

auto increment logic in DML

I have two tables test2 and test_hist. i want to load data into test_hist from test2 but it is failing due to unique constraint . CREATE TABLE TEST2 (ID NUMBER , TEXT VARCHAR2(10)); create table test_hist (id number , text varchar2(10) , constraint…
kashi
  • 61
  • 1
  • 12
1
vote
2 answers

Windowed Average, accounting for gaps

I need to calculate an average over the preceding 4 weeks... SELECT *, AVG(val) OVER (PARTITION BY some_identifier, day_of_week_column ORDER BY date_column ROW BETWEEN 4 PRECEDING AND 1 PRECEDING …
MatBailie
  • 83,401
  • 18
  • 103
  • 137
1
vote
4 answers

Time difference under 1h for group of records

Table looks like this Play_name Status Date 1 Romeo & juliet Start 23.01.2018 16:30:00 2 Romeo & juliet Break 23.01.2018 17:15:00 3 Romeo & juliet END 23.01.2018 18:30:00 4 Hamlet Start 25.01.2018 15:45:00 …
Doniu
  • 85
  • 1
  • 10
1
vote
1 answer

Analytic function windowing clause

Column VAL is a number list from 1 to 3, the other columns are supposed to show: A) MIN of all lower values than VAL B) MAX of all lower values than VAL C) MIN of all greater values than VAL D) MAX of all greater values than VAL I would expect…
abrittaf
  • 537
  • 5
  • 11
1
vote
2 answers

How to get number of days between the earliest date and the oldest date in every row of my input

I am having difficulty getting the number of days between the oldest date and the newest date, the problem I have is if I use MIN or MAX I have to group by a column which is an issue as I do not have a column to group by. If my data looks like…
user5648938
1
vote
1 answer

Oracle get difference in Average of Current and Previous group (partition)

I am using Oracle 12.1.0.2.0 I want difference in average of current group(partition) - average of previous group(partition) My code to get current group Average is with rws as ( select rownum x, mod(rownum, 2) y from dual connect by level…
Vinaya
  • 29
  • 3
1
vote
3 answers

Find row in related table with closest date

I need to find service.service_id, service.name and service.date_begin for each row in bonus table with closest service.date_begin and service.date_begin <= bonus.date_begin. If there are more than one service with such date_begin, return any…
1
vote
3 answers

Convert a sequence of 0s and 1s to a print-style page list

I need to convert a string of 0s and 1s into a sequence of integers representing the 1s, similar to a page selection sequence in a print dialog. e.g. '0011001110101' -> '3-4,7-9,11,13' Is it possible to do this in a single SQL select (in Oracle…
Barney
  • 2,786
  • 2
  • 32
  • 34
1
vote
1 answer

Find non-consecutive raw values from DB table- SQL

I'm looking for some analytic function in Oracle or some query which would find the non-consecutive raw values from table. Sample Example ORDER FLAG | ORDER FLAG 6 TRUE | 6 TRUE 2 FALSE | 5 TRUE 2 FALSE…
inityk
  • 476
  • 1
  • 9
  • 18
1
vote
1 answer

Oracle - Increment value

I have a table holding customer invoice data. I am trying to find how many consequently months a particular transaction type is present on a customer’s invoice by creating a carryover counter. If the transaction is no longer present, the counter…
MrM
  • 389
  • 1
  • 8
  • 23