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

In Oracle SQL, is it possible have an effective dated query that also show rows that occur between dates?

I have the following tables: ProductGroup |GroupID|Product Group|Product Date| |-|-------|--------|--------| |A|Bicycles|1/1/2018| |A|Two-Wheels|12/1/2018| |A|Sport Bicycles|6/1/2019| |A|Fast…
Sacheltry
  • 3
  • 3
0
votes
2 answers

Select max date from table when joining multiple tables

I am trying to write a query to retrieve the max dispatched date and also bringing in fields from multiple tables. Im using the fields from other tables since the table that contains the dispatched date does not contain any of the information that I…
0
votes
1 answer

SQL Server: Analytic window function with varying lengths

I would like to write an analytic function with aggregated functions on several windows with varying lengths. Let's say I have a table of close prices of stocks, which looks like this: Ticker | TradeDate | ClosePrice |…
tete
  • 4,859
  • 11
  • 50
  • 81
0
votes
1 answer

Oracle SQL - Count based on a condition to include distinct rows with zero matches

Is there a "better" way to refactor the query below that returns the number occurrences of a particular value (e.g. 'A') for each distinct id? The challenge seems to be keeping id = 2 in the result set even though the count is zero (id = 2 is never…
Alex Bartsmon
  • 471
  • 4
  • 9
0
votes
2 answers

SQL Oracle - Multiprocessor Scheduling: Greedy Number Partitioning

Is there an SQL statement to perform greedy number partitioning? (Oracle 19c) I want to divide jobs among N processors. Example, Given the following workload data set: job --- 4 60 50 1 100 6 Expected result set (assuming just N=2 where ties go to…
0
votes
1 answer

Evaluating multiple window function

If a window is provided multiple times in the same query, how is it evaluated? Does the query parser check if one window is the same as another or easily 'derived' from another. For example in the following: SELECT MAX(val) OVER (PARTITION BY…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
2 answers

How to check max from range in cursor?

I have a problem with transferring an Excel formula to SQL. My excel formula is: =IF(P2<(MAX($P$2:P2));"Move";""). The P column in excel is a sequence of numbers. a | b ------ 1 2 7 3 MOVE 4 MOVE 8 9 5 MOVE 10 You can find more…
anst
  • 21
  • 5
0
votes
1 answer

Oracle - Display an incremental ID by a simple query

I have a very simple query like this: SELECT Line, ID, Enum FROM tab1 the result is something like this: Line id enum A 10 1 A 10 2 A 10 3 A 10 4 ... B 20 4 B 20 5 B 20 6 B 20 7 ... I want,…
0
votes
1 answer

How to get max of one date in a table in sql

I have written the below code - select PAPF.PERSON_NUMBER, BP.NAME BENEFIT_PLAN, BBR.BENEFIT_RELATION_NAME, Round(BPER.BNFT_AMT, 2) * 100 COVERAGE_AMOUNT, …
Sree
  • 3
  • 1
0
votes
2 answers

Oracle query to fill in the missing data in the same table

I have a table in oracle which has missing data for a given id. I am trying to figure out the sql to fill in the data from start date: 01/01/2019 to end_dt: 10/1/2020. see the input data below. for status key the data can be filled based on its…
user1751356
  • 565
  • 4
  • 14
  • 33
0
votes
2 answers

Oracle SQL: How can I sum every x number of subsequent rows for each row

I have a data table that looks like this: |Contract Date | Settlement_Prcie | |--------------|------------------| | 01/10/2020 | 50 | |--------------|------------------| | 01/11/2020 | 10 …
0
votes
1 answer

Using a window function in BigQuery to create running sum of active quarters

I am working to enhance a dataset by creating a column that would allow me to track how many active quarters a given company has had for a given row. A company is "active" if they recognize revenue within that quarter. Each row of my dataset…
0
votes
2 answers

Issues with Analytic function in BigQuery

since June 2nd we are having issues with analytic functions. when the query (not the partitions) passes a certain size the query fails with the following error: Resources exceeded during query execution: The query could not be executed in the…
0
votes
1 answer

Compute a Decreasing Cumulative Sum in SQL Server

What I'm trying to achieve is a cumulative sum on a non-negative column where it decreases by 1 on every row, however the result must also be non-negative. For example, for the following table, summing over the "VALUE" column ordered by the "ID"…
satm12
  • 60
  • 1
  • 6
0
votes
2 answers

SQL query to calculate the cumulative number of trips for each date

I have a hive table called bikeshare_trips with the following schema +---------------------+------------+----------------------------------------------------+--+ | col_name | data_type | comment …
Chema
  • 2,748
  • 2
  • 13
  • 24