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
4
votes
3 answers

How to avoid same out time in this query?

My table:Trnevents emp_reader_id EVENTID DT 102 0 2018-01-04 15:57:04.000 102 0 2018-01-04 15:58:05.000 102 1 2018-01-04 16:46:19.000 102 0 2018-01-04 18:15:27.000 102 …
4
votes
1 answer

Oracle SQL NTILE - equal distribution

This query: SELECT min( "x" ) as "From", max( "x" ) as "To", sum("quantity") FROM ( SELECT t.*, ntile( 4 ) over (order by "x" ) as group_no FROM table1 t)GROUP BY group_no ORDER BY 1; with this…
Tim
  • 115
  • 11
4
votes
4 answers

Alternative for PERCENTILE_CONT in MySQL/MariaDB

I want to calculate percentile_cont on this table. In Oracle, the query would be SELECT PERCENTILE_CONT(0.05) FROM sometable; What would be it's alternative in MariaDB/MySQL?
sushma
  • 71
  • 1
  • 5
4
votes
3 answers

mysql: group by ID, get highest priority per each ID

I have the following mysql table called "pics", with the following fields and sample data: id vehicle_id filename priority 1 45 a.jpg 4 2 45 b.jpg 1 3 56 f.jpg 4 4 67 …
briang
  • 43
  • 1
  • 4
4
votes
1 answer

Find the maximum value in a column for each partition

I have table structure like: CREATE TABLE new_test ( col1 NUMBER(2) NOT NULL, col2 VARCHAR2(50) NOT NULL, col3 VARCHAR2(50) NOT NULL, col4 VARCHAR2(50) NOT NULL ); It has data: col1 col2 col3 col4 0 A B X 1 …
user613114
  • 2,731
  • 11
  • 47
  • 73
4
votes
3 answers

Referencing the value of the previous calculcated value in Oracle

How can one reference a calculated value from the previous row in a SQL query? In my case each row is an event that somehow manipulates the same value from the previous row. The raw data looks like this: Eventno Eventtype Totalcharge 3 …
Kristoffer
  • 1,633
  • 3
  • 19
  • 25
4
votes
1 answer

Apply COUNT function on a subgroup of groups

I made up this weird example trying to illustrate what I want to do (it's kind of stupid, but bear with me): Consider the following table: EMPLOYEES married, certified and religious are just boolean fields (in case of Oracle, they are of type…
jFrenetic
  • 5,384
  • 5
  • 42
  • 67
3
votes
1 answer

dense_rank() analytic function in oracle

SELECT empno, deptno dense_rank() OVER (PARTITION BY deptno ORDER BY sal NULLS LAST) SRLNO FROM emp WHERE deptno IN (10, 20) group by empno, deptno --,sal ORDER BY deptno, SRLNO; This Query didn't work because Sal should be in group by…
Gaurav Soni
  • 6,278
  • 9
  • 52
  • 72
3
votes
1 answer

Does BigQuery support Analytic User-Defined Functions?

BigQuery supports: User Defined Functions (UDFs) in SQL and JavaScript. Analytic functions that compute values over a group of rows and return a single result for each row. These functions can be used with OVER clause. There is a predefined set of…
Oleg Lokshyn
  • 517
  • 7
  • 14
3
votes
1 answer

SQL analytic functions - How to assign number to a partition?

I am using Oracle. Suppose I am having a table with this sample, random, content: columnA | columnB | content -------------------------------- AfBkxZ | 292 | a LDglkK | 181 | b AfBkxZ | 51 | c AfBkxZ | 315 …
Benoit
  • 76,634
  • 23
  • 210
  • 236
3
votes
1 answer

Oracle 12c Analytic Function

Is there a way to obtain the corresponding value X for a minimum value Y in a given dataset, in the same record, using Oracle Analytic functions, and without using a subquery? For example: If I have the following dataset "ds1": Col1 Col2 A 1 B…
M. Kemp
  • 107
  • 1
  • 1
  • 6
3
votes
2 answers

Oracle get row where column value changed

Say I have a table, something like ID CCTR DATE ----- ------ ---------- 1 2C 8/1/2018 2 2C 7/2/2018 3 2C 5/4/2017 4 2B 3/2/2017 5 2B 1/1/2017 6 UC 11/23/2016 There are other…
dk96m
  • 301
  • 3
  • 18
3
votes
1 answer

oracle SQL select the distinct customers in the past x day rolling period

say you have a table of customers with dates as follows: [customer_table] +----------+-----------+----------+ | customer | date | purchase | +----------+-----------+----------+ | 1 | 1/01/2016 | 12 …
barker
  • 1,005
  • 18
  • 36
3
votes
1 answer

Obtaining multiple percentiles (percentile_cont equivalent) in one pass within Teradata

I understand that we can rewrite percentile_cont within Teradata as: SELECT part_col ,data_col + ((MIN(data_col) OVER (PARTITION BY part_col ORDER BY data_col ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) - data_col) * (((COUNT(*) OVER…
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
3
votes
2 answers

Advanced tutorial on analytic functions in Oracle/SQLServer

Can anyone recommend a good tutorial (or book perhaps) that covers advanced topics in the use of Analytic Functions? I'm looking for something that covers both Oracle and SQLServer - or two separate guides if a single one doesn't exist. Something…
LBushkin
  • 129,300
  • 32
  • 216
  • 265
1 2
3
16 17