analytic-functions

avg sale of quarter with previous quarter avg sale

▼魔方 西西 提交于 2019-12-01 12:54:30
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 P1 2015 2 MAY 5000 NORTH P1 2015 2 JUN 6000 NORTH P1 2015 3 JUL 7000 NORTH P1 2015 3 AUG 8000 NORTH P1

Oracle Analytic function for min value in grouping

孤人 提交于 2019-12-01 04:22:45
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) KEEP (DENSE_RANK FIRST ORDER BY salary) FROM mytable GROUP BY dept David Aldridge I think that the

How do I select a fixed number of rows for each group?

纵然是瞬间 提交于 2019-11-30 13:50:10
Here is some example data in a mysql table a b distance 15 44 250 94 31 250 30 41 250 6 1 250 95 18 250 72 84 500 14 23 500 55 24 500 95 8 500 59 25 500 40 73 500 65 85 500 32 50 500 31 39 500 22 25 500 37 11 750 98 39 750 15 57 750 9 22 750 14 44 750 69 22 750 62 50 750 89 35 750 67 65 750 74 37 750 52 36 750 66 53 750 82 74 1000 79 22 1000 98 41 1000 How do I query this table such that I get 2 rows per distance selected at random? A successful query will produce something like a b distance 30 41 250 95 18 250 59 25 500 65 85 500 15 57 750 89 35 750 79 22 1000 98 41 1000 Use: SELECT x.a, x.b,

Oracle Analytic functions - resetting a windowing clause

非 Y 不嫁゛ 提交于 2019-11-30 09:56:32
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 DATE1 ---------- --------- 100 01-JAN-13 100 02-JAN-13 200 03-JAN-13 100 04-JAN-13 select dept, date1,

mysql: group by ID, get highest priority per each ID

别说谁变了你拦得住时间么 提交于 2019-11-30 07:17:27
问题 I have the following mysql table called "pics", with the following fields and sample data: id vehicle_id filename priority 1 45 a.jpg 4 2 45 b.jpg 1 3 56 f.jpg 4 4 67 cc.jpg 4 5 45 kt.jpg 3 6 67 gg.jpg 1 Is it possible, in a single query, to get one row for each vehicle_id, and the row be the highest priority? The result I'm looking for: array ( [0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg', [priority] => '1' ), [1] => array( [id] => '3', [vehicle_id] => '56',

How do I select a fixed number of rows for each group?

家住魔仙堡 提交于 2019-11-29 20:32:59
问题 Here is some example data in a mysql table a b distance 15 44 250 94 31 250 30 41 250 6 1 250 95 18 250 72 84 500 14 23 500 55 24 500 95 8 500 59 25 500 40 73 500 65 85 500 32 50 500 31 39 500 22 25 500 37 11 750 98 39 750 15 57 750 9 22 750 14 44 750 69 22 750 62 50 750 89 35 750 67 65 750 74 37 750 52 36 750 66 53 750 82 74 1000 79 22 1000 98 41 1000 How do I query this table such that I get 2 rows per distance selected at random? A successful query will produce something like a b distance

calculate running balance in oracle query

时光怂恿深爱的人放手 提交于 2019-11-29 06:57:51
I have data like this id cp_id amount_a amount_b CCP1 TTP01 10.000.000 2.000.000 CCP1 TTP02 10.000.000 3.000.000 CCP1 TTP03 10.000.000 1.000.000 CCP1 TTP04 10.000.000 500.000 CCP2 TTP05 5.000.000 1.000.000 CCP2 TTP06 5.000.000 2.000.000 CCP3 TTP07 1.000.000 500.000 I want the the result data add one column of running_balance like this below id amount_a amount_b running_balance CCP1 10.000.000 2.000.000 8.000.000 CCP1 10.000.000 3.000.000 5.000.000 CCP1 10.000.000 1.000.000 4.000.000 CCP1 10.000.000 500.000 3.500.000 CCP2 5.000.000 1.000.000 4.000.000 CCP2 5.000.000 2.000.000 2.000.000 CCP3 1

mysql: group by ID, get highest priority per each ID

我与影子孤独终老i 提交于 2019-11-29 02:37:40
I have the following mysql table called "pics", with the following fields and sample data: id vehicle_id filename priority 1 45 a.jpg 4 2 45 b.jpg 1 3 56 f.jpg 4 4 67 cc.jpg 4 5 45 kt.jpg 3 6 67 gg.jpg 1 Is it possible, in a single query, to get one row for each vehicle_id, and the row be the highest priority? The result I'm looking for: array ( [0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg', [priority] => '1' ), [1] => array( [id] => '3', [vehicle_id] => '56', [filename] => 'f.jpg', [priority] => '4' ), [2] => array( [id] => '6', [vehicle_id] => '67', [filename] => 'gg

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

瘦欲@ 提交于 2019-11-28 11:19:51
for SQL Server 2008 R2 I have a resultset that looks like this (note [price] is numeric, NULL below represents a NULL value, the result set is ordered by product_id and timestamp) product timestamp price ------- ---------------- ----- 5678 2008-01-01 12:00 12.34 5678 2008-01-01 12:01 NULL 5678 2008-01-01 12:02 NULL 5678 2008-01-01 12:03 23.45 5678 2008-01-01 12:04 NULL I want to transform that to a result set that (essentially) copies a non-null value from the latest preceding row, to produce a resultset that looks like this: product timestamp price ------- ---------------- ----- 5678 2008-01

Using GROUP BY with FIRST_VALUE and LAST_VALUE

夙愿已清 提交于 2019-11-28 09:54:36
I'm working with some data that is currently stored in 1 minute intervals that looks like this: CREATE TABLE #MinuteData ( [Id] INT , [MinuteBar] DATETIME , [Open] NUMERIC(12, 6) , [High] NUMERIC(12, 6) , [Low] NUMERIC(12, 6) , [Close] NUMERIC(12, 6) ); INSERT INTO #MinuteData ( [Id], [MinuteBar], [Open], [High], [Low], [Close] ) VALUES ( 1, '2015-01-01 17:00:00', 1.557870, 1.557880, 1.557870, 1.557880 ), ( 2, '2015-01-01 17:01:00', 1.557900, 1.557900, 1.557880, 1.557880 ), ( 3, '2015-01-01 17:02:00', 1.557960, 1.558070, 1.557960, 1.558040 ), ( 4, '2015-01-01 17:03:00', 1.558080, 1.558100, 1