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

SQL How to use LAG inside SUM

My table (name EPM1): TIMESTAMP INVERTER1POWER INVERTER2POWER POWERLIMIT 2021-09-30T17:19:42.309Z 100 400 0.8 2021-09-30T17:20:42.309Z 200 500 0.6 2021-09-30T17:21:42.309Z 300 600 0.7 I have a query like this: SELECT SUM(CASE WHEN…
r3vo91
  • 23
  • 3
2
votes
3 answers

SQL Impala- Aggregation with analytic function causes group by AnalysisException

I have a query where I want to calculate the percentage of sales of a certain product within its category. Thus I calculate the amount sales per product and use an analytic function and partition by category. SELECT product_id,…
Energizer1
  • 285
  • 1
  • 5
  • 15
2
votes
1 answer

Oracle Analytical Function, data from last but 1 row

I am seeing that, in one of our tables, the row with latest timestamp has NULLs in most columns, except for the last row which has correct values. Is there a way to carry forward those values from last but one row to the last row? I've been trying…
brawn
  • 21
  • 3
2
votes
2 answers

Range based window Frame can have only 1 sort key

Ive tried to run the next query select sum(balance) over (partition by client order by card desc, date_tr desc) from table_1 And in Result i have the next error message: FAILED: SemanticException Range based Window Frame can have only 1 Sort…
Denis Plotnikov
  • 105
  • 2
  • 9
2
votes
2 answers

Using ratio_to_report analytic

I am trying to get the percentage of rows that a set of particular value has. Best explained by example. I can do this by each column very simply using ratio-to-report function and over(), but am having issues with multiple groupings Assume table…
Marc
  • 51
  • 1
  • 3
2
votes
2 answers

How to add a flag to the rows after a specific value within a key in Oracle SQL?

I have the following data: Key Stage CreateDate AAF 0 01-Jan-2018 AAF 0 02-Jan-2018 AAF 0 10-Jan-2018 AAF 20 20-Jan-2018 AAF 40 20-Mar-2018 AAF 0 01-May-2018 AAF 0 10-May-2018 AAF 0 20-May-2018 AAF 30 20-Jun-2018 AAF…
CuriP
  • 83
  • 10
2
votes
2 answers

Grouping data in SQL by difference in column values

I have following data in my logs table in postgres table: logid => int (auto increment) start_time => bigint (stores epoch value) inserted_value => int Following is the data stored in the table (where start time actual is not a column, just…
Ashutosh
  • 4,371
  • 10
  • 59
  • 105
2
votes
1 answer

Oracle SQL - distributing into buckets

i'am searching for a smart oracle sql solution to distribute data into a number of buckets. The order of x is important. I know there are a lot of algorithms but iam pretty sure there must be smart sql (analytic function) solution e.g. NTILE(3) but…
Tim
  • 115
  • 11
2
votes
4 answers

How the get last change timestamp for row?

I have a data set like this (DDL below): +----+------------------+----------------------+---------------------+ | ID | NAME | EMAIL | LAST_UPD …
jrara
  • 16,239
  • 33
  • 89
  • 120
2
votes
2 answers

Find Maximal Value of other Rows per Group

I have a simple table with values (ID) in groups (GRP_ID). create table tst as select 1 grp_id, 1 id from dual union all select 1 grp_id, 1 id from dual union all select 1 grp_id, 2 id from dual union all select 2 grp_id, 1 id from dual union…
Marmite Bomber
  • 19,886
  • 4
  • 26
  • 53
2
votes
1 answer

SQL Query based on ROW_Number not working

Basically I am trying to do a query to a page that holds just an image. I need to provide the tripID number and then the ROWID (as there could be multiple images) to receive a single image. I will be looping until each image is in its corresponding…
Spooks
  • 6,937
  • 11
  • 49
  • 66
2
votes
3 answers

How do i do running totals from second column

I have a data set like below, Lot Size Reported QTY Qty Balance 150 100 150 100 150 80 150 80 150 5 The Qty Balance needs to…
Shankar
  • 879
  • 8
  • 15
2
votes
3 answers

Moving averages using analytic functions in T-SQL SQL Server 2014

I need to calculate weekly and monthly moving averages per sensor per day for a large set of sample data based on some quality criteria. I have a working solution based on correlated sub-queries (or self joins), but I was wondering if using analytic…
2
votes
2 answers

Oracle SELECT query: collapsing null values when pairing up dates

I have the following Oracle query: SELECT id, DECODE(state, 'Open', state_in, NULL) AS open_in, DECODE(state, 'Not Open', state_in, NULL) AS open_out, FROM ( SELECT id, CASE WHEN state = 'Open' …
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
2
votes
2 answers

TSQL - LEAD for Next Different Row

Is there a way to use the lead function such that I can get the next row where something has changed, as opposed it where it is the same? In this example, the RowType can be 'in' or 'out', for each 'in' I need to know the next RowNumber where it…
SomeGuy30145
  • 85
  • 1
  • 7