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.

230 questions
97
votes
1 answer

Calculating Cumulative Sum in PostgreSQL

I want to find the cumulative or running amount of field and insert it from staging to table. My staging structure is something like this: ea_month id amount ea_year circle_id April 92570 1000 2014 1 April …
96
votes
10 answers

SQL Row_Number() function in Where Clause

I found one question answered with the Row_Number() function in the where clause. When I tried one query, I was getting the following error: "Msg 4108, Level 15, State 1, Line 1 Windowed functions can only appear in the SELECT or ORDER BY…
Joseph
90
votes
9 answers

MySQL get row position in ORDER BY

With the following MySQL table: +-----------------------------+ + id INT UNSIGNED + + name VARCHAR(100) + +-----------------------------+ How can I select a single row AND its position amongst the other rows in the table, when…
leepowers
  • 35,484
  • 22
  • 93
  • 127
54
votes
2 answers

What is ROWS UNBOUNDED PRECEDING used for in Teradata?

I am just starting on Teradata and I have come across an Ordered Analytical Function called "Rows unbounded preceding" in Teradata. I tried several sites to learn about the function but all of them uses a complicated example explaining the same.…
StrugglingCoder
  • 4,231
  • 11
  • 55
  • 90
46
votes
4 answers

Oracle 'Partition By' and 'Row_Number' keyword

I have a SQL query written by someone else and I'm trying to figure out what it does. Can someone please explain what the Partition By and Row_Number keywords does here and give a simple example of it in action, as well as why one would want to use…
HashimR
  • 3,423
  • 6
  • 28
  • 46
32
votes
7 answers

Taking the record with the max date

Let's assume I extract some set of data. i.e. SELECT A, date FROM table I want just the record with the max date (for each value of A). I could write SELECT A, col_date FROM TABLENAME t_ext WHERE col_date = (SELECT MAX (col_date) …
Revious
  • 6,994
  • 29
  • 89
  • 133
22
votes
3 answers

Using GROUP BY with FIRST_VALUE and LAST_VALUE

I'm working with some data that is currently stored in 1 minute intervals that looks like this: CREATE TABLE #MinuteData ( [Id] INT , [MinuteBar] DATETIME , [Open] NUMERIC(12, 6) , [High] NUMERIC(12, 6) , [Low]…
13
votes
4 answers

Oracle SQL Analytic query - recursive spreadsheet-like running total

I have the following data, composed of the A value, ordered by MM (month). The B column is computed as GREATEST(current value of A + previous value of B, 0) in a spreadsheet-like fashion. How can I compute B using a SQL Query? I tried using…
9
votes
3 answers

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

for SQL Server 2008 R2 I have a resultset that looks like this (note [price] is numeric, NULL below represents a NULL value, the result set is ordered by product_id and timestamp) product timestamp price ------- ---------------- ----- …
9
votes
3 answers

Group by vs Partition by in Oracle

I am writing a query to fetch records from a Oracle warehouse. Its a simple Select Query with joins on few tables and i have few columns to be aggregated. Hence i end up using Groupby on rest of the columns. Say I am picking some 10 columns and out…
user747858
9
votes
3 answers

Oracle Analytic functions - How to reuse a PARTITION BY clause?

I have written a complex SQL query with Oracle in which I want to use two analytic function over the same partition. Let's be quite simple but not too much: SELECT col1, MAX(col2) OVER(PARTITION BY col3, col4, col5, col6, …
Benoit
  • 70,220
  • 21
  • 189
  • 223
8
votes
1 answer

Oracle MIN as analytic function - odd behavior with ORDER BY?

This particular case was distilled from an example where the programmer assumed that for two shipments into a tank car, line #1 would be loaded first. I corrected this to allow for the loading to be performed in any order - however, I discovered…
Cade Roux
  • 83,561
  • 38
  • 170
  • 259
8
votes
3 answers

How do I select a fixed number of rows for each group?

Here is some example data in a mysql table a b distance 15 44 250 94 31 250 30 41 250 6 1 250 95 18 250 72 84 500 14 23 500 55 24 500 95 8 500 59 25 500 40 73 500 65 85 500 32 50 500 31 39 500 22 25 500 37 11 …
Maiasaura
  • 29,590
  • 23
  • 96
  • 103
8
votes
4 answers

Oracle Analytic function for min value in grouping

I'm new to working with analytic functions. DEPT EMP SALARY ---- ----- ------ 10 MARY 100000 10 JOHN 200000 10 SCOTT 300000 20 BOB 100000 20 BETTY 200000 30 ALAN 100000 30 TOM 200000 30 JEFF 300000 I want the department…
Travis Heseman
  • 10,909
  • 8
  • 34
  • 46
7
votes
2 answers

calculate running balance in oracle query

I have data like this id cp_id amount_a amount_b CCP1 TTP01 10.000.000 2.000.000 CCP1 TTP02 10.000.000 3.000.000 CCP1 TTP03 10.000.000 1.000.000 CCP1 TTP04 10.000.000 500.000 CCP2 TTP05 5.000.000 …
Ras Rass
  • 225
  • 2
  • 17
1
2 3
15 16