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

Finding latest record entry

I have data like this. REPORTER SUMMARY CREATED UPDATED status servicetype e159299 Route card from August 7/29/2013 22:46.0 5 New Route Card e159299 I have returned the 6/11/2013 32:09.7 5 Cancel…
user218045
  • 113
  • 1
  • 2
  • 11
0
votes
1 answer

Getting prior test scores in the same query

I am trying to get prior scores and prior test date of a student in the same query. My current query works fine until the prior score falls exactly on a 28 day difference. The issue is when the scores fall on 29,30 or 31 day. Here is my current…
ozzboy
  • 2,672
  • 8
  • 42
  • 69
0
votes
1 answer

MySQL get row position with ORDER BY incorrectly numbered

I have the following mySQL query which I want to give the rows sorted on a calculated field with the corresponding row position. I've used OMG Ponies code from here as a template but the rows are coming back incorrectly numbered (they are being…
mseifert
  • 5,390
  • 9
  • 38
  • 100
0
votes
1 answer

Oracle 11g - doing analytic functions on millions of rows

My application allows users to collect measurement data as part of an experiment, and needs to have the ability to report on all of the measurements ever taken. Below is a very simplified version of the tables I have: CREATE TABLE EXPERIMENTS( …
user1578653
  • 4,888
  • 16
  • 46
  • 74
0
votes
1 answer

Count previous occurences of a value split by date ranges

Here's a simple query we do for ad hoc requests from our Marketing department on the leads we received in the last 90 days. SELECT ID ,FIRST_NAME ,LAST_NAME ,ADDRESS_1 ,ADDRESS_2 ,CITY ,STATE ,ZIP ,HOME_PHONE …
0
votes
1 answer

Need help in understanding Oracle analytic function

I have the following code to detect duplicates in a single table: UPDATE tab SET dup = 'Y' WHERE ROWID IN (SELECT tab_o.ROWID FROM tab tab_o, (SELECT * FROM tab tab_i WHERE…
user613114
  • 2,731
  • 11
  • 47
  • 73
0
votes
2 answers

Divide data from other days by data from one particular day

I am a bit stuck on one problem for a few hours now. Let`s say I have a table with the following data: month outstanding 01/05/2012 35 678 956 02/05/2012 33 678 956 03/05/2012 31 678 956 04/05/2012 27 678 956 05/05/2012 …
lakhlu
  • 3
  • 1
0
votes
2 answers

performant query using analytic function to select records with 2 date columns

I'm looking for a perfomant way to write a SQL query. I've got a table with columns (id,fname,lname,accountid,creation_date,update_date) and i have to look for the records in that table that have the same fname,lname,accountid and have the most…
R Vive L OL
  • 177
  • 3
  • 3
  • 10
-1
votes
1 answer

Bigquery SQL : When to use Aggregated Function over Analytics Function over Subquery?

Let's say i have a table with Users : user_id abc def And a table of Purchases…
-1
votes
1 answer

Create a group in sql

I have a table with the fields ORDER and FLAG as below and I want to create the GRP column using analytic functions. Can someone adivse how I can do this in sql please? ORDER FLAG GRP 1 Y 1 2 N 1 3 N 1 4 Y 2 5 N 2 6 Y 3 7 Y 4
ezryder
  • 31
  • 2
-1
votes
1 answer

How to aggregate its previous 30 days for each line in MySQL without using self-join?

Suppose we have a table Named UserSessions. Each user can have multiple sessions in a day or across different days. The columns of this table are UserSessions table: --------------------- SessionID (int) PK, UserID (int), datetime (datetime)…
-1
votes
1 answer

How to write sql query to generate a group no for each grouped record

following is scenario: I have data in following format: entryid , ac_no, db/cr, amt ----------------------------------------------- 1 10 D 5 1 11 C 5 …
Ratnesh
  • 1
  • 2
-1
votes
1 answer

Find the missing hour with Lag analytic function

I have to find the missing hour in my table , for frequency = 1 I have to find a record per hour, if it's not the case, I have to display the missing hour. here's my code declare @StartDate datetime declare @EndDate datetime declare @now datetime…
BKChedlia
  • 327
  • 2
  • 4
  • 18
-1
votes
1 answer

Ranking base on two dates in Oracle

I have a query with the following result: I thought I could use dense_rank to group user segment in a time interval. But it doesn't work. CUST_ID EVENT_ID SEGMENT_ID SEGMENT_CODE DATE_FROM DATE_TO 100 1424 21 …
Shylock
  • 3
  • 1
-2
votes
2 answers

country with the highest number of tests(oracle plsql)

Data Model: Hi, I am trying to get "country with the highest number of tests". Query: I tried using one table.. ok... but how I get it with "countryname"? How should I make this with inner join?
furkanak
  • 3
  • 2
1 2 3
16
17