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
2
votes
1 answer

Can I force this Oracle windowing query to return a single row of values?

This query is meant to replace another that uses nested subqueries and performs accordingly. As it is now, it does return the correct results for each column, but will return a row that has the value for that column, nulls for the rest, then another…
John O
  • 4,863
  • 8
  • 45
  • 78
2
votes
1 answer

Missing window specification for this function

Now getting the following error: ORA-30484: missing window specification for this function 30484. 00000 - "missing window specification for this function" *Cause: All window functions should be followed by window specification, like…
2
votes
2 answers

First value (Oracle) equivalent in Hive

I want to create hive query for the following. insert into tempTableName select distinct col_a , first_value(col_b) over (partition by col_a order by nvl(col_c,0) desc, length(col_b) asc, col_b asc) from…
user2978621
  • 803
  • 2
  • 11
  • 20
2
votes
6 answers

tricky Oracle analytic function question

I'm new to Oracle analytic functions and I'm trying to find the best way to write my query. The following is a simplified version of a table I'm working with... CREATE TABLE my_table ( pid NUMBER NOT NULL, my_value NUMBER, …
Kal
  • 185
  • 3
  • 12
2
votes
2 answers

Oracle analytic function window defined by data

I have a table which represents a line-by-line dump of the data read from a particular text file format. Each line may represent a "master" or a "detail" line, indicated via rec_type code. I'd like to write a query that gets the "master" lines…
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
2
votes
1 answer

Partition using Lead in Oracle

I am stuck on a problem to implement LEAD/LAG with partition. Below is the example and expected Result create table trd( key number, book number, prd_key number, direction varchar2(2), trdtime date, price…
cormalado
  • 21
  • 1
2
votes
4 answers

How to get start/end date from single date column -oracle

I've seen some brilliant answers on here and I am in need of a fix. I do not want to create table or ETL just yet and I was hoping to create a simple database view users could access to test first. A table has an item#, item_ticket_color#,…
C M
  • 21
  • 1
  • 3
2
votes
1 answer

Additional Functions for Window Functions

Thank you so much for the window functions!!! I'm curious if some more "basic" aggregates will be supported: Sum() Average() Min() Max() Current result of trying to use Sum(): Error: Unrecognized Analytic Function: SUM cannot be used with an OVER()…
2
votes
1 answer

Vertica - Creating a calendar table

I have a problem in Vertica related to filling dates that don't exist. I have seen solutions online where people have suggested to create a calendar table. Here is one such MYSQL problem from stackoverflow. Is there a way to create a calendar table…
2
votes
2 answers

Another approach to percentiles?

I have a dataset which essentially consists of a list of job batches, the number of jobs contained in each batch, and the duration of each job batch. Here is a sample dataset: CREATE TABLE test_data ( batch_id NUMBER, job_count NUMBER, …
emiller42
  • 103
  • 1
  • 6
2
votes
1 answer

Next action using analytic functions

For the need of a cohort analysis, I'm trying to get the next action of each customer (unsubscription, upgrade, downgrade...) I have a monhtly snapshot with the following data : customer | month | last_action | last_action_date 1 …
2
votes
3 answers

Return a value when a different value changes

I have a query, which returns the following, EXCEPT for the last column, which is what I need to figure out how to create. For each given ObservationID I need to return the date on which the status changes; something like a LEAD() function that…
David
  • 23
  • 3
2
votes
1 answer

SQL sort that distributes results

Given a table of products like this: ID Name Seller ID Updated at -- ---- --------- ---------- 1 First 3 2012-01-01 12:00:10 2 Second 3 2012-01-01 12:00:09 3 Third 4 2012-01-01 12:00:08 4 Fourth …
kburkhardt
  • 153
  • 1
  • 5
2
votes
1 answer

How does the analytic function FIRST_VALUE work in SQL

I have just started diving in to SQL analytic functions and am a little hung up on FIRST_VALUE, maybe it is just specific to the example query from this site: -- How many days after the first hire of each department were the next -- employees…
ferics2
  • 5,241
  • 7
  • 30
  • 46
1
vote
1 answer

Oracle - Refactor subquery with max()

I have the query below. SOFTWARE_DEVELOPMENT_CYCLE has multiple rows, but I'm interested in the latest. I would like to rewrite the query so that I don't use a subquery. I have attempted it with DENSE_RANK LAST ORDERY BY, but to no avail. Could…
retrodev
  • 2,323
  • 6
  • 24
  • 48