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

Alternative for percentile_cont in sql server 2008

In Oracle it works well ...... Query for oracle is As Follows Select distinct channel_id, position_id,datamonth, percentile_cont(.9) within group (order by TRIM_PRE_ELIG_PAY) over (partition by channel_id, position_id, datamonth) as…
1
vote
4 answers

ORA-00907 Error when using Analytic Function in a Query (PS/Query, Peopletools 8.51.12)

Query's throwing an ORA-00907 Error when I try to paste a list of values into a criteria. Background: I'm not a developer, I'm just an end user that's studied enough to where I can write queries using PS/Query within Peoplesoft, for my company's…
1
vote
1 answer

SQL Oracle - Filter on Rank/Window Partition

I have a table of receiving events by item and branch (skul) and I'm trying to pull only the most recent receipt date for each skul. I'm having trouble with an approach for the where clause. Any help would be appreciated, here is SQL I am using…
1
vote
1 answer

Access "outer row" value in an analytic expression

I would like to access the value of the "current row" on which I write the analytic expression on. For example, given the following sample data: DROP TABLE emp PURGE; CREATE TABLE emp ( empno NUMBER(4) CONSTRAINT pk_emp PRIMARY KEY, ename …
Arklur
  • 173
  • 1
  • 11
1
vote
1 answer

Analog of SQL analytic functions for R

is there an analog to SQL analytic so that one could do aggregation without collapsing rows? For example, I want to do a sum for each group without GROUP BY, in SQL I can do this: select group, x, sum(x) over(partition by group) group_sum from…
Nikolai
  • 65
  • 5
1
vote
1 answer

How to combine an arithmetic sum with an analytic function in SQL for a running

I'm trying to calculate a running % from the result of two counts (as shown in the SQL excerpt below) from a table in normal SQL that looks like this: My thoughts are this: SELECT week(beginning), (select count(enquired_at) from TABLE) /…
1
vote
3 answers

How to get adjacent value in an OVER() window

I have the following data and query to get the season with the MAX(wins) up to the current season: WITH results as ( SELECT 'DAL' as team, 2010 as season, 6 as wins union SELECT 'DET' as team, 2010 as season, 6 as wins union SELECT 'DET'…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
3 answers

Get ID alongside max value ORACLE SQL

I currently have the following: TABLE "QUARTO": CREATE TABLE Quarto ( Id number(2) NOT NULL, LotacaoMaxima number(1) NOT NULL, TipoQuartoId number(1) NOT NULL, NumeroQuartoNumSequencial…
user12949382
1
vote
2 answers

ORACLE SQL find row with max date for each grouping

I am trying to write a query which will return only the rows, which time has the greatest value for each id Table: positions id time otherCols... ---------- ----------- ---------- 1 1 1 2 3 1 …
boneash
  • 158
  • 1
  • 3
  • 19
1
vote
2 answers

Oracle SQL: LAG over a range of values

How do we use analytic function LAG over the range of values. It should return null if there is no record in the partition with an earlier in_date otherwise, year and month of the previous in_date Example: CID IN_DATE 1 2020-05-29 1 …
1
vote
1 answer

Error when using analytic function. I can not find the reason

What is the mistake in this query? select department_id, first_name, lag(first_name) over (partition by department_id order by salary rows between 1 preceding and 2 following), lead(first_name) over (partition by department_id order by salary…
LOP
  • 23
  • 2
1
vote
2 answers

How to calculate balance from credit and debit?

How to get balance amount based on credit CRD and debit DEB for each customer cust from following txn_tbl table SQL> SELECT * FROM txn_tbl; CUS AMT TXN_CDE --- ---------- ---------- A 500 CRD B 400 CRD A 350 CRD C…
Nvr
  • 171
  • 1
  • 11
1
vote
2 answers

how do i take the results of my subqueries to join with another table?

. trying to link together the results from borrower,book, to AUTHOR. desired results: AUTHORID AUTHORFIRSTNAME AUTHORLASTNAME 1 JIM SPARKS 2 JAMES ALLEN 3 …
user12349310
1
vote
2 answers

Select last row using ROW_NUMBER function

I am just wondering if there is any way to select the last row based on ROW_NUMBER() function? Basically I would like to select for each department_id THE LAST (max one) emp_id. SELECT department_id, last_name, employee_id, ROW_NUMBER()…
Bro
  • 57
  • 7
1
vote
2 answers

Nulls in rows with three common value, fourth varies with one match

My fist table TEMP1 has codes noting period when status starts. +PRSNID | LVL2 | LOC | initialQTRYR | STAT_IMPRT_VAR *a | AA | CHI | 0118 | z *b | AA | CHI | 0318 | z *b | AA | LOS…
Drew
  • 25
  • 2