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

Window functions vs vanilla SQL

If we would consider window functions as an extension to vanilla SQL, what is their advantage over it? Can you perform queries and things that wouldn't be possible with 'plain' SQL?
microwth
  • 1,016
  • 1
  • 14
  • 27
0
votes
1 answer

SQL Query :How do I Utilize Timestamp column among rank Column values

We are trying to rank Invoice column and Only want first shipment Invoices among parts of Invoices. That is in that, column we have multiple Invoices. We would like to select that group of invoices among rank column which consist of Earliest…
NEO
  • 389
  • 8
  • 31
0
votes
3 answers

How to make this Mysql query work?

My query: SELECT * FROM forum_topics WHERE cat_id IN(1,2,3,4,5,6,7) ORDER BY last_message DESC LIMIT 7 I want to get the biggest and only one value of each cat_id (7 values total). How to correct this query to make it work if it's even…
good_evening
  • 21,085
  • 65
  • 193
  • 298
0
votes
2 answers

Oracle SQL - How to contrain related records timewise

Given the below listed data, how to select only records, for which: a) at least 1 previous ticket for the same client_id exists and b) the maximum time difference of each predecessing ticket may not exceed 14 days. In other words, if a ticket has…
royskatt
  • 1,190
  • 2
  • 15
  • 35
0
votes
3 answers

Oracle SELECT query: collapsing NULL values when pairing up dates for different fields

This question is very much like my previous question, but a bit more complicated. Rob van Wijk's answer worked perfectly for my other question, and I've been using that as a starting point. My problem now is that I am pivoting dates for different…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
0
votes
1 answer

Analytical function range window for max date interval

I am trying to get 10 minute interval data from latest date of each group or partition. Pseudo code SQL: Select count(1) Over( partition by col1, col2, col3 Order by Col_Date Desc Range Max(Col_Date) Between …
ch12
  • 1
  • 1
0
votes
1 answer

Group and Summarize Time Series Transactions with Start and Stop Times

I have a series of transactions. I need to summarize the total time spent working on these transactions and a count of transactions within groups that break when you move from one group to another. This means that the group can repeat if there is a…
0
votes
2 answers

How do I grab the “next” event when the offset is variable for items that can be repeatedly processed?

This question is virtually identical to another I recently asked, with the very important distinction that these transactions are loan transactions and, therefore, items may reappear in the data multiple times. This is why I'm currently using LEAD.…
ND Geek
  • 398
  • 6
  • 21
0
votes
1 answer

how to calculate quota with analytic function row by row?

We have a problem with using analytic function in Oracle. We want to calculate remaining_quota column from other columns. Our data is grouped by branch_id and region_id. Each branch and region has a quota value(In example 1077). If quota value is…
neverwinter
  • 810
  • 2
  • 15
  • 42
0
votes
1 answer

oracle sql - filter out duplicate entries on condition

I have the following query to yield sale_price by date: SELECT product_name, SUM(sale_price) top_sale_price, sale_date, COUNT(*) count FROM sales WHERE sale_date IN (TO_DATE ('14-JUN-14', 'DD-MON-YY'), TO_DATE ('14-JUN-14', 'DD-MON-YY') -…
SheerSt
  • 3,229
  • 7
  • 25
  • 34
0
votes
1 answer

In which SQL dialect was RANK() first introduced?

In which SQL standard was RANK() first introduced? List of SQL standards: SQL-86 SQL-89 SQL-92 SQL:1999 SQL:2003 SQL:2008 SQL Rank function: http://en.wikipedia.org/wiki/Select_(SQL)#RANK.28.29_window_function References would be most appreciated.
JavaRocky
  • 19,203
  • 31
  • 89
  • 110
0
votes
1 answer

SQL query in ORACLE - select most recent date

I am a newbie in the world of SQL query. I need to eliminate the duplicate Staff # and retrieve only the highlighted row. Any help is highly appreciated. Staff# Pay_DT Due_DT loan_flag housing…
0
votes
2 answers

How to use a case or decode as part of an analytical window function in Oracle SQL

I would like to do something like this: select sum(nvl(total_time_out, 0)), sum(nvl((case when day_of_week = 'Mon' then total_time_out else 0 end) over (partition by person_id), 0)) from xxpay_tna_summary_v where person_id = 7926 where…
Superdooperhero
  • 7,584
  • 19
  • 83
  • 138
0
votes
1 answer

Select first and last records in grouped results - Oracle 11g

Say I have the following information in an Oracle 11g table: Qty Production order Date and time --- ----------------- --------------- 20 00000000000000001 12-JAN-14 00:02 20 00000000000000001 12-JAN-14 00:05 20 00000000000000001 …
0
votes
1 answer

Include NULLs in MAX() OVER PARTITION

I have an Oracle query that I'd like to return a MAX() over a PARTITION while incorporating NULLs. For instance, if the raw data is: NAME | DATE ----------------------- ADAM | 01/21/14 00:00 ADAM | ADAM | 01/22/14 00:01 ADAM | 01/23/14…
McArthey
  • 1,614
  • 30
  • 62