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

Can I create index on a column with functions or analytical functions in SQL Server 2016?

Can I create an Index on a column with functions or analytical functions? I have a table ABC with columns XXX updateddatetime, YYY updatedusername. I am trying to create indices as shown below CREATE INDEX idx_Cname1 ON…
Rmani
  • 25
  • 4
0
votes
3 answers

Analytic Function SUM returns Average for window with same values

I was trying to write a query that resulted the running sum of given values. However, when applied SUM as analytic function I got result with average within a window. Example: Consider the following query: with tbl as ( select 'steve' "NAME", 2000…
Sunny KC
  • 235
  • 2
  • 6
0
votes
1 answer

Can we use analytic functions to retrieve parents and child in hierarchy?

I have the following data (a sample of real data, which is much bigger): id p_id 1 null 2 1 3 1 4 2 5 4 6 5 7 5 8 3 9 7 Would it be possible to use analytic function of SQL Server to get the following result (get the deepest hierarchy):…
Leo
  • 265
  • 1
  • 4
  • 18
0
votes
0 answers

Spark Group By/ Over Partition Has Bad Performance Java

I am trying to run group by query on dataset of 1.4 million records. With Hive it takes 2 min while in spark it takes ~40 min with same resources I am sure I am doing something wrong because this difference between hive and spark with a simple…
0
votes
1 answer

SQL Analytic Functions: Rank over Multiple Partitions

A friend asked for assistance with the below query (see image for additional clarification): "We need to generate the sales-rank that is based on the number of items sold, and is calculated by product. In other words for a given product, the year…
JohnGagliano
  • 54
  • 1
  • 6
0
votes
2 answers

How to calculate moving average value using every nth row (e.g. 24th,48th and 72nd) in sql?

Here is the snip of my database I want to calculate average energy consumption for the last three days in the exact hour. So if I have consumption at 24.10.2016. 10h, I want to add column with average consumption for the last three days at the same…
0
votes
2 answers

Updating a column in an Oracle procedure based on other two columns and using a LAG function

I'm trying to update a column by using the following procedure, it uses the LAG function as the result is calculated by subtracting the previous value to the current value as it can be seen in the image below, but it does not work so far as it…
0
votes
1 answer

How do you shift values down in a column in an Oracle table?

Given the following oracle database table: group revision comment 1 1 1 1 2 2 1 null null 2 1 1 2 2 2 2 3 3 2 …
Sam Kolier
  • 13
  • 2
0
votes
1 answer

Incoorect Count Results

I have two data tables Employees and Departments. I want to show the number of employees in each department, the average of salaries in each department, among other things. The code I have is: select d.department_id, d.department_name,…
Tim
  • 101
  • 1
  • 8
0
votes
0 answers

Partitions and groupby: first quarter with company sales

I have a table with company sales by quarter. The table only registers transactions that occurred, so if there are no sales in a given quarter, it won't appear at all in the table. I would like to find the first quarter that the company has any…
vvv
  • 337
  • 3
  • 8
0
votes
1 answer

MySQL/ Hive : Join conditioned rows using windowing or analytical functions

I have two tables which I want to join with a specific logic. Table_1 ( S_No, ID, Date1, Date2 ) S_No ID Date1 Date2 1 id1 2014-05-01 2014-07-03 2 id1 2015-03-23 2016-06-18 3 id1 2016-06-21 2016-07-29 Table_2 ( S_No_New, ID_New,…
0
votes
1 answer

Analytic Function MAX Issues

WITH t AS ( SELECT 9 a ,sysdate d ,1 c ,1 e FROM dual UNION SELECT 1 a ,sysdate - 5 d ,2 c ,1 e FROM dual UNION SELECT 2 a ,sysdate - 2…
KrGk
  • 71
  • 1
0
votes
0 answers

How to get new and returned YTD,MTD,WTD users in a user traffic table?

I would like to get all the new and returned users from a user_traffic table. Just wondering what would be the approach in solving this problem. Any thoughts/inputs would be appreciated. I am not expecting a ready made solution for this problem but…
Teja
  • 13,214
  • 36
  • 93
  • 155
0
votes
2 answers

how to use distinct on multiple columns

I have 6 fields f1,f2,f3,f4,f5,f6 only fields 4 to 6 only vary i want result as single row based on field 1 Eg name , age, policy_no, proposer_code, entry_date , …
0
votes
1 answer

SQL sliding window aggregation (without using window function)

I am looking for a query that can aggregate data on a 21 days back basis in the following way: my table has the following columns: accountid, date, measure I need, for each account, date to sum(measure) for the previous 21 days back. Any idea how to…
Nir Regev
  • 135
  • 1
  • 2
  • 7