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

oracle - group by - no aggregation

I have an oracle table , where ref_id is the flag field is the type of data and ORN is the order of data in each ref_id : ref_id data ORN flag 1 100 0 0 1 200 1 0 1 300 2 0 1 400 3 …
user123
  • 387
  • 1
  • 4
  • 13
0
votes
2 answers

SELECT row of last update with same value

We have this table that keeps inserting values of Inrunning field which is a number.. There are multple mistakenly insert. I want to get the row where the first insert happened with duplicate value. In this case 5000 value inserted 3 times in matter…
0
votes
1 answer

SQL group data taking date sequence into account

I am trying to group separate rows which might be broken by a different group. If they are broken I want them to group separately. So I have: Col1 | Col2 --------------------- | Y |01/JAN/2012 | Y |01/FEB/2012 | N |01/MAR/2012 | Y |01/APR/2012 |…
ezryder
  • 31
  • 2
0
votes
1 answer

Hive ignore nulls

I am trying to rewrite last value(col1 ignore nulls ) analytical function in hive from RDMS. I used select last_value(col1,TRUE) But I am getting null as output when used for above query. Can someone please suggest is there any other way to ignore…
user11069271
  • 109
  • 2
  • 6
0
votes
3 answers

SQL query to aggregate by period through date

I have a table that has transactional data by store number and transaction date. It also includes the year and period number of the corresponding transaction date, as such: create table transactional_data ( store_id number, tran_date date, …
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102
0
votes
2 answers

Oracle SQL to return column values from unique rows

I have a table with individual batches which can have multiple sub-batches: +--------+----------+ ¦Batch +Sub-batch ¦ ¦--------¦----------¦ ¦B_01 ¦SB_01 ¦ ¦B_02 ¦SB_02 ¦ ¦B_02 ¦SB_03 ¦ ¦B_03 ¦SB_04 …
ChemEnger
  • 131
  • 12
0
votes
3 answers

How to concatenate consecutive rows in oracle

am beginner to oracle. I tried following source code to solve and get as following O/P.But cant, please give some ideas to solve this. id product sales --- ------------- -------- 1 Soaps …
Naveen
  • 3
  • 3
0
votes
2 answers

Select first two distinct occurrences and return rowid

I have a table like this: room_id | name | time 1 | Kate | 2019-09-18 10:00:00.000 1 | Michael | 2019-09-18 12:00:00.000 1 | George | 2019-09-18 14:00:00.000 2 | Tom | 2019-09-17 09:00:00.000 2 | Ben |…
0
votes
4 answers

Count over Partition by with one condition (/don't count the NULL values)

I want to count how many houses are within a building. Dataset like the following: BuildingID, HouseID 1, 1 1, 2 1, 3 2, 4 2, 5 2, 6 NULL, 7 NULL, 8 With the following code it shows the total count of the houses, however, houses 7 and 8 don't have…
0
votes
3 answers

Query to Merge subsequent rows in Oracle/Teradata

I have a table with data as below DB DBMS INST SCHEMA TABLE COLUMN HDFT N_Identity Class IDS TD SBD IDS Data_Val cust_t HIGH IDS TD SBD IDS Data_Val cust_t GID IDS TD SBD IDS Data_Val …
user3901666
  • 399
  • 11
  • 29
0
votes
0 answers

Does Oracle recompute aggregate/analytic functions whenever they're referenced?

Not for any specific case, just a question on how SQL software (e.g. Oracle) implements aggregate/analytic functions in order to help me write more efficient code. Basically, I'm curious whether Oracle does anything smart about caching computed…
tpapalex
  • 1
  • 2
0
votes
3 answers

Update a column value within a SELECT query

I have a complicated SQL question. Can we update a column within a SELECT query? Example: Consider this table: |ID |SeenAt | ---------------- |1 |20 | |1 |21 | |1 |22 | |2 |70 | |2 …
Haha
  • 973
  • 16
  • 43
0
votes
1 answer

Grouping a column in oracle

I have three columns within my table: Amount, OrderNumber, Customerid For each customerid there will be ordernumber and amount. Now i need to display customerid,Ordernumber and Amount(total Amount- for each customerid). custid srcnumber …
author4
  • 125
  • 2
  • 12
0
votes
2 answers

ORA-00904: "PREV_TEMP": invalid identifier with LAG function

Whats is wrong with this query? It returns: ORA-00904: "PREV_TEMP": invalid identifier SELECT Id, RecordDate, Temperature, LAG(Temperature) OVER (ORDER BY RecordDate) as prev_temp FROM Weather WHERE Temperature > prev_temp; SQL schema: Create…
raiym
  • 1,439
  • 1
  • 28
  • 54
0
votes
1 answer

Analytic function to count each customer with many accounts only once

I'm having a slow day and for some reason am blanking on how to do this. Please help me. I have an Analytic function to count each customer with many accounts only once. Want a total of each distinct customer. I have to list the customers with…
Carbon
  • 313
  • 3
  • 11