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
1
vote
1 answer

Google Analytics Cross-Domain Tracking Without Appending Cookies To URLs?

Is there a way to track two domains in Google Analytics without passing the cookie information in the URL?
1
vote
1 answer

rank() a group of items by count(*)

I have some problems with Oracle analytic functions and need help. Here's a generic example: create table test (item varchar2(10), value varchar2(10)); insert into test values ('item1','value1'); insert into test values ('item1','value1'); insert…
stee1rat
  • 720
  • 2
  • 9
  • 20
1
vote
3 answers

avg sale of quarter with previous quarter avg sale

I have a table one in which there are various attribute like region product,year,qtr,month,sale. I have to calculate the avg_qtr sale of each product having same region and show their previous avg_qtr sale.I have read about lag but here it is not…
1
vote
2 answers

How do I find the top N batters per year?

I'm playing around with the Lahman Baseball Database in a MySQL instance. I want to find the players who topped home runs (HR) for each year. The Batting table has the following (relevant parts) of its…
Drew Stephens
  • 17,207
  • 15
  • 66
  • 82
1
vote
2 answers

How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?

It doesn't look like SQL Server Compact Edition supports the RANK() function. (See Functions (SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx). How would I duplicate the RANK() function in a SQL Server…
AMissico
  • 21,470
  • 7
  • 78
  • 106
1
vote
4 answers

Interesting Row_Number() bug

I was playing with the Stack Exchange Data Explorer and ran this query: https://data.stackexchange.com/stackoverflow/query/2820/rising-stars-top-50-users-ordered-on-rep-per-day Notice down in the results, rows 11 and 12 have the same value and so…
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1
vote
2 answers

Oracle - Calculating time differences

Let's say I have following data: Create Table Pm_Test ( Ticket_id Number, Department_From varchar2(100), Department_To varchar2(100), Routing_Date Date ); Insert Into Pm_Test Values (1,'A','B',To_Date('20140101120005','yyyymmddhh24miss')); Insert…
royskatt
  • 1,190
  • 2
  • 15
  • 35
1
vote
4 answers

Analytic functions for "the attribute from the row with the max date"

I'm refactoring a colleague's code, and I have several cases where he's using a cursor to get "the latest row that matches some predicate": His technique is to write the join as a cursor, order it by the date field descending, open the cursor, get…
tpdi
  • 34,554
  • 11
  • 80
  • 120
1
vote
3 answers

How do I compress this Oracle resultset into values according to row priority, ignoring nulls?

I'll simplify the problem as much as possible: I have an oracle table: row_priority, col1, col2, col3 0, .1, 100, {null} 12, {null}, {null}, 3 24, .2, {null}, {null} Desired result: col1, col2, col3 .2, 100, 3 So according to the priority of the…
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
1
vote
5 answers

oracle sql - numbering group of rows

i have the following table with different prices in every week and need a numbering like in the last column. consecutive rows with same prices should have the same number like in weeks 11/12 or 18/19. but on the other side weeks 2 and 16 have the…
Peter
  • 60
  • 4
1
vote
1 answer

updating columns by grouping transactions

I have the following data in an object collection col_a: 00004719~BBK~US 00004719~SBK~US 00004719~OBK~GB 00004719~IBK~DE 00004720~BBK~US 00004720~SBK~GB 00004725~IBK~IN Col_a is defined in the database as: create OR REPLACE TYPE col_a AS TABLE OF…
Casey
  • 213
  • 1
  • 7
  • 17
1
vote
2 answers

How do I grab the "next" event when the offset is variable?

I have a table of transactions in an Oracle database. I am attempting to pull a report together for a delivery system involving a number of transaction types. The "request" type can actually be one of four sub-types ('A', 'B', 'C', and 'D' for…
ND Geek
  • 398
  • 6
  • 21
1
vote
2 answers

Replace selfjoin with analytic functions

How do I go about replacing the following self join using analytics: SELECT t1.col1 col1, t1.col2 col2, SUM((extract(hour FROM (t1.times_stamp - t2.times_stamp)) * 3600 + extract(minute FROM ( t1.times_stamp - t2.times_stamp)) * 60 + extract(second…
edwards
  • 33
  • 4
1
vote
3 answers

FIRST_VALUE function not working as expected

I thought I understood how it works but now I am confused. I have a dataset: id date value 1 20080101 null 2 20090101 34 3 20100101 null Three records, from Jan 2008, 2009 and 2010. Now I want to create a new column "value2" with the latest…
Steve
  • 4,935
  • 11
  • 56
  • 83
1
vote
3 answers

How do I write an SQL to get a cumulative value and a monthly total in one row?

Say, I have the following data: select 1 id, date '2007-01-16' date_created, 5 sales, 'Bob' name from dual union all select 2 id, date '2007-04-16' date_created, 2 sales, 'Bob' name from dual union all select 3 id, date '2007-05-16'…
supertonsky
  • 2,563
  • 6
  • 38
  • 68