analytic-functions

Oracle Analytic functions - resetting a windowing clause

时光总嘲笑我的痴心妄想 提交于 2020-01-21 02:14:14
问题 I have the following data set. create table t1 ( dept number, date1 date ); Table created. insert into t1 values (100, '01-jan-2013'); insert into t1 values (100, '02-jan-2013'); insert into t1 values (200, '03-jan-2013'); insert into t1 values (100, '04-jan-2013'); commit; MY goal is to create a rank column that resets each time the department changes. The closest column that I can use for "partition by" clause is dept, but that won't give me the desired result. SQL> select * from t1; DEPT

Oracle Analytic function for min value in grouping

独自空忆成欢 提交于 2020-01-11 04:55:08
问题 I'm new to working with analytic functions. DEPT EMP SALARY ---- ----- ------ 10 MARY 100000 10 JOHN 200000 10 SCOTT 300000 20 BOB 100000 20 BETTY 200000 30 ALAN 100000 30 TOM 200000 30 JEFF 300000 I want the department and employee with minimum salary. Results should look like: DEPT EMP SALARY ---- ----- ------ 10 MARY 100000 20 BOB 100000 30 ALAN 100000 EDIT: Here's the SQL I have (but of course, it doesn't work as it wants staff in the group by clause as well): SELECT dept, emp, MIN(salary

In which SQL dialect was RANK() first introduced?

谁说我不能喝 提交于 2020-01-07 04:43:24
问题 In which SQL standard was RANK() first introduced? List of SQL standards: SQL-86 SQL-89 SQL-92 SQL:1999 SQL:2003 SQL:2008 SQL Rank function: http://en.wikipedia.org/wiki/Select_(SQL)#RANK.28.29_window_function References would be most appreciated. 回答1: The analytic features are defined as part of the ANSI SQL 1999 standard Reference: SS64.com 来源: https://stackoverflow.com/questions/2376290/in-which-sql-dialect-was-rank-first-introduced

How do I grab the “next” event when the offset is variable for items that can be repeatedly processed?

半世苍凉 提交于 2019-12-25 03:55:40
问题 This question is virtually identical to another I recently asked, with the very important distinction that these transactions are loan transactions and, therefore, items may reappear in the data multiple times. This is why I'm currently using LEAD . With that clarification, I repost my question below. I have a table of transactions in an Oracle database. I am attempting to pull a report together for a delivery system involving a number of transaction types. The "request" type can actually be

How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?

家住魔仙堡 提交于 2019-12-24 10:49:10
问题 It doesn't look like SQL Server Compact Edition supports the RANK() function. (See Functions (SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx). How would I duplicate the RANK() function in a SQL Server Compact Edition SELECT statement. (Please use Northwind.sdf for any sample select statements, as it is the only one I can open with SQL Server 2005 Management Studio.) 回答1: SELECT x.[Product Name], x.[Unit Price], COUNT(y.[Unit Price]) Rank FROM

Getting values relating to the max and min rows in Oracle

我们两清 提交于 2019-12-23 12:08:47
问题 In Oracle 11g we need to be able to query a table to pull out information from rows with the highest and lowest values in a certain group. For example using the EMP table we'd like to find the name of person with the highest salary and the name of the person with the lowest salary in each department DEPTNO MAX_SAL MAX_EARNER MIN_SAL MIN_EARNER ------------------------------------------------------- 10 5000 KING 1300 MILLER 20 3000 FORD 2975 JONES etc (if there are two or more staff with the

How do i do running totals from second column

半城伤御伤魂 提交于 2019-12-22 12:39:25
问题 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 calculated as follows, Row 1 = Lot Size - Reported Qty (row1) => 150-100 = 50 Row 2 = Reported Qty (row1) - Reported Qty(row2) => 100-100 =0 Row 3 = Reported Qty (row2) - Reported Qty(row3) => 100-80 =20 ... till the last row My expected result is Lot Size Reported QTY Qty Balance 150 100 50 150 100 0 150 80 20 150 80 0 150 5 75 How do I achieve this in a query? 回答1:

Apply COUNT function on a subgroup of groups

孤者浪人 提交于 2019-12-22 09:18:54
问题 I made up this weird example trying to illustrate what I want to do (it's kind of stupid, but bear with me): Consider the following table: EMPLOYEES married , certified and religious are just boolean fields (in case of Oracle, they are of type NUMBER(1,0)). I need to come up with SQL that displays for each hire_year, count of married, certified and religious employees within the following salary categories: A SALARY > 2000 B SALARY BETWEEN 1000 AND 2000 C SALARY < 1000 Based on the above

Oracle MIN as analytic function - odd behavior with ORDER BY?

不羁岁月 提交于 2019-12-19 17:39:58
问题 This particular case was distilled from an example where the programmer assumed that for two shipments into a tank car, line #1 would be loaded first. I corrected this to allow for the loading to be performed in any order - however, I discovered that MIN() OVER (PARTITION BY) allows an ORDER BY in Oracle (this is not allowed in SQL Server), and additionally, it alters the behavior of the function, causing the ORDER BY to apparently be added to the PARTITION BY . WITH data AS ( SELECT 1 AS

avg sale of quarter with previous quarter avg sale

这一生的挚爱 提交于 2019-12-19 11:42:41
问题 I have a table one in which there are various attribute like region product,year,qtr,month,sale. I have to calculate the avg_qtr sale of each product having same region and show their previous avg_qtr sale.I have read about lag but here it is not possible to use as it is not fixed after how many rows it will be repeated. My table structure is like this Region Product Year Qtr Month Sales NORTH P1 2015 1 JAN 1000 NORTH P1 2015 1 FEB 2000 NORTH P1 2015 1 MAR 3000 NORTH P1 2015 2 APR 4000 NORTH