analytic-functions

Taking the record with the max date

*爱你&永不变心* 提交于 2019-11-28 06:20:24
Let's assume I extract some set of data. i.e. SELECT A, date FROM table I want just the record with the max date (for each value of A). I could write SELECT A, col_date FROM TABLENAME t_ext WHERE col_date = (SELECT MAX (col_date) FROM TABLENAME t_in WHERE t_in.A = t_ext.A) But my query is really long... is there a more compact way using ANALYTIC FUNCTION to do the same? The analytic function approach would look something like SELECT a, some_date_column FROM (SELECT a, some_date_column, rank() over (partition by a order by some_date_column desc) rnk FROM tablename) WHERE rnk = 1 Note that

calculate running balance in oracle query

[亡魂溺海] 提交于 2019-11-28 00:36:35
问题 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

SQL Row_Number() function in Where Clause

自古美人都是妖i 提交于 2019-11-27 19:12:44
I found one question answered with the Row_Number() function in the where clause. When I tried one query, I was getting the following error: "Msg 4108, Level 15, State 1, Line 1 Windowed functions can only appear in the SELECT or ORDER BY clauses." Here is the query I tried. If somebody knows how to solve this, please let me know. SELECT employee_id FROM V_EMPLOYEE WHERE row_number() OVER ( ORDER BY employee_id ) > 0 ORDER BY Employee_ID To get around this issue, wrap your select statement in a CTE, and then you can query against the CTE and use the windowed function's results in the where

Oracle 'Partition By' and 'Row_Number' keyword

青春壹個敷衍的年華 提交于 2019-11-27 06:20:49
I have a SQL query written by someone else and I'm trying to figure out what it does. Can someone please explain what the Partition By and Row_Number keywords does here and give a simple example of it in action, as well as why one would want to use it? An example of partition by: (SELECT cdt.*, ROW_NUMBER () OVER (PARTITION BY cdt.country_code, cdt.account, cdt.currency ORDER BY cdt.country_code, cdt.account, cdt.currency) seq_no FROM CUSTOMER_DETAILS cdt); I've seen some examples online, they are in bit too depth. Thanks in advance! Michael Buen PARTITION BY segregate sets, this enables you

Using GROUP BY with FIRST_VALUE and LAST_VALUE

北城以北 提交于 2019-11-27 05:51:49
问题 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

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

假如想象 提交于 2019-11-27 03:37:28
问题 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

Calculating Cumulative Sum in PostgreSQL

北慕城南 提交于 2019-11-26 19:50:03
I want to find the cumulative or running amount of field and insert it from staging to table. My staging structure is something like this: ea_month id amount ea_year circle_id April 92570 1000 2014 1 April 92571 3000 2014 2 April 92572 2000 2014 3 March 92573 3000 2014 1 March 92574 2500 2014 2 March 92575 3750 2014 3 February 92576 2000 2014 1 February 92577 2500 2014 2 February 92578 1450 2014 3 I want my target table to look something like this: ea_month id amount ea_year circle_id cum_amt February 92576 1000 2014 1 1000 March 92573 3000 2014 1 4000 April 92570 2000 2014 1 6000 February

SQL Row_Number() function in Where Clause

ぃ、小莉子 提交于 2019-11-26 18:07:31
问题 I found one question answered with the Row_Number() function in the where clause. When I tried one query, I was getting the following error: "Msg 4108, Level 15, State 1, Line 1 Windowed functions can only appear in the SELECT or ORDER BY clauses." Here is the query I tried. If somebody knows how to solve this, please let me know. SELECT employee_id FROM V_EMPLOYEE WHERE row_number() OVER ( ORDER BY employee_id ) > 0 ORDER BY Employee_ID 回答1: To get around this issue, wrap your select

Oracle 'Partition By' and 'Row_Number' keyword

末鹿安然 提交于 2019-11-26 12:53:23
问题 I have a SQL query written by someone else and I\'m trying to figure out what it does. Can someone please explain what the Partition By and Row_Number keywords does here and give a simple example of it in action, as well as why one would want to use it? An example of partition by: (SELECT cdt.*, ROW_NUMBER () OVER (PARTITION BY cdt.country_code, cdt.account, cdt.currency ORDER BY cdt.country_code, cdt.account, cdt.currency) seq_no FROM CUSTOMER_DETAILS cdt); I\'ve seen some examples online,

Calculating Cumulative Sum in PostgreSQL

霸气de小男生 提交于 2019-11-26 07:28:00
问题 I want to find the cumulative or running amount of field and insert it from staging to table. My staging structure is something like this: ea_month id amount ea_year circle_id April 92570 1000 2014 1 April 92571 3000 2014 2 April 92572 2000 2014 3 March 92573 3000 2014 1 March 92574 2500 2014 2 March 92575 3750 2014 3 February 92576 2000 2014 1 February 92577 2500 2014 2 February 92578 1450 2014 3 I want my target table to look something like this: ea_month id amount ea_year circle_id cum_amt