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

How to produce rank in Oracle

Need to rank the below by salary, with highest salary having rank 1. The RANK column shown is what I'm after: Empname sal address RANK ---------------------------------------------- Ram 3411 45,east road …
anirban
  • 51
  • 1
  • 2
3
votes
2 answers

Beginner SQL question: querying gold and silver tag badges in Stack Exchange Data Explorer

I'm using the Stack Exchange Data Explorer to learn SQL, but I think the fundamentals of the question is applicable to other databases. I'm trying to query the Badges table, which according to Stexdex (that's what I'm going to call it from now on)…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
3
votes
3 answers

Should first_value() over (order by something asc) be the same as last_value () over (order by something desc)?

Shouldn't first_value() ordered descending give the same results as last_value() ordered ascending? I ran the following query and got the results attached below the query. select random_date, trunc(random_date,'MM') random_month,…
MontyPython
  • 2,906
  • 11
  • 37
  • 58
3
votes
1 answer

Oracle - Use analytic function inside aggregate function

I have a table DATE_VALUE like this: Date Value ---- ----- 01/01/2012 1.5 02/01/2012 1.7 03/01/2012 1.3 04/01/2012 2.1 05/01/2012 3.4 I want to calculate variance between differences of value between 2…
Wayne Yeung
  • 145
  • 2
  • 6
3
votes
1 answer

Oracle Analytics - partitions and ordering of sql queries

This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY COL1 COL2 COL3 0 0.1 100 12 3 24 0.2
ScottCher
  • 14,651
  • 6
  • 26
  • 25
3
votes
4 answers

Oracle partition by group into date based sequence

I am trying to use the PARTITION BY OVER to 'group' rows by certain columns. I understand the use of PARTITION somewhat, however I want to 'block' the partitions by date. For example, if we have |col1|col2 | | A |01/JAN/2012| | A …
Marcus
  • 33
  • 1
  • 4
3
votes
1 answer

Over clause in SQL Server

I have the following query select * from ( SELECT distinct rx.patid ,rx.fillDate ,rx.scriptEndDate ,MAX(datediff(day, rx.filldate, rx.scriptenddate)) AS longestScript ,rx.drugClass …
wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197
3
votes
2 answers

Ranking/Counting Rows based on Column Value

I have some data in a format described in this sqlfilddle: http://sqlfiddle.com/#!4/b9cdf/2 Basically, a table with a user ID and a time that an event occured. What I'd like to do is count the events based on the time they happened by user. So, a…
DNadel
  • 495
  • 1
  • 5
  • 13
3
votes
1 answer

How does the FIRST_VALUE() function in Oracle process rows if there is no order by clause?

First_value() definition states that it returns the first value in an ordered data set. It's syntax is First_Value(ColumnName) over ( [Partition By Colum] [Order by Column] ) The Partition By and Order By are optional. If partition by is removed, we…
2
votes
2 answers

Oracle Analytics Window

Given the following table PAYMENT_Date TRANSACTION_TYPE PAYMENT_AMT 1/1/2012 P 184366 1/1/2012 R -5841 1/2/2012 P 941 1/3/2012 P …
Sumit
  • 1,661
  • 1
  • 13
  • 18
2
votes
5 answers

Finding a count of rows in an arbitrary date range using Oracle

The question I need to answer is this "What is the maximum number of page requests we have ever received in a 60 minute period?" I have a table that looks similar to this: date_page_requested date; page varchar(80); I'm…
Timothy Grant
  • 141
  • 3
  • 10
2
votes
3 answers

Oracle analytics: using LAG values in calculation?

I have a table that records when tasks were completed. Tasks belong to a workflow, but in this example I'm just trying to get the LAG working. I would like to find information about how long each task takes. I've tried: select completed_date, …
chris
  • 36,094
  • 53
  • 157
  • 237
2
votes
2 answers

Transaction activity running total in SQL

I am working with a transactions table. I want to generate a number of variables to determine the number of transactions performed by the same customer in the last 5 and 30 minutes and 1,2,3 and 4 hours and 1,2,5, and 10 days. What is an efficient…
SQLQueryR
  • 41
  • 3
2
votes
2 answers

How do I calculate a median-to-date variable using Oracle SQL?

I'm working with invoice data in Oracle SQL. I want to create a variable, "median account invoice amount to date", that gives the median invoice amount for an account up to that given invoice date.
SQLQueryR
  • 41
  • 3
2
votes
0 answers

Syntaxic analysis and parser

I want to parse something like that : path.to.variable "path" and "to" are object named "Instance" and variable reference a double. I've got the following grammar : expr ::= instancePath:i INSTANCE_SEPARATOR SHORTCUT:s …
Jerome Cance
  • 8,103
  • 12
  • 53
  • 106