window-functions

T-SQL calculate moving average

孤街醉人 提交于 2019-11-26 14:24:06
问题 I am working with SQL Server 2008 R2, trying to calculate a moving average. For each record in my view, I would like to collect the values of the 250 previous records, and then calculate the average for this selection. My view columns are as follows: TransactionID | TimeStamp | Value | MovAvg ---------------------------------------------------- 1 | 01.09.2014 10:00:12 | 5 | 2 | 01.09.2014 10:05:34 | 3 | ... 300 | 03.09.2014 09:00:23 | 4 | TransactionID is unique. For each TransactionID , I

OVER clause in Oracle

对着背影说爱祢 提交于 2019-11-26 11:59:43
问题 What is the meaning of the OVER clause in Oracle? 回答1: The OVER clause specifies the partitioning, ordering & window "over which" the analytic function operates. For example, this calculates a moving average: AVG(amt) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) date amt avg_amt ===== ==== ======= 1-Jan 10.0 10.5 2-Jan 11.0 17.0 3-Jan 30.0 17.0 4-Jan 10.0 18.0 5-Jan 14.0 12.0 It operates over a moving window (3 rows wide) over the rows, ordered by date. This calculates a

Window Functions: last_value(ORDER BY … ASC) same as last_value(ORDER BY … DESC)

本小妞迷上赌 提交于 2019-11-26 11:31:19
问题 Sample data CREATE TABLE test (id integer, session_ID integer, value integer) ; INSERT INTO test (id, session_ID, value) VALUES (0, 2, 100), (1, 2, 120), (2, 2, 140), (3, 1, 900), (4, 1, 800), (5, 1, 500) ; Current query select id, last_value(value) over (partition by session_ID order by id) as last_value_window, last_value(value) over (partition by session_ID order by id desc) as last_value_window_desc from test ORDER BY id I was running into a problem with the last_value() window function:

PostgreSQL equivalent for TOP n WITH TIES: LIMIT “with ties”?

允我心安 提交于 2019-11-26 11:22:29
问题 I\'m looking for something similar this in SQL Server: SELECT TOP n WITH TIES FROM tablename I know about LIMIT in PostgreSQL, but does the equivalent of the above exist? I\'m just curious as it would save an extra query each time for me. If I have a table Numbers with attribute nums : {10, 9, 8, 8, 2} . I want to do something like: SELECT nums FROM Numbers ORDER BY nums DESC LIMIT *with ties* 3 It should return {10, 9, 8, 8} because it takes the top 3 plus the extra 8 since it ties the other

SQL Server: Difference between PARTITION BY and GROUP BY

﹥>﹥吖頭↗ 提交于 2019-11-26 08:39:06
问题 I\'ve been using GROUP BY for all types of aggregate queries over the years. Recently, I\'ve been reverse-engineering some code that uses PARTITION BY to perform aggregations. In reading through all the documentation I can find about PARTITION BY , it sounds a lot like GROUP BY , maybe with a little extra functionality added in? Are they two versions of the same general functionality, or are they something different entirely? 回答1: They're used in different places. group by modifies the entire

Get the distinct sum of a joined table column

做~自己de王妃 提交于 2019-11-26 08:35:38
问题 I have a problem here, and I\'m hoping there is an easy solution. I\'ll try to make this as simple as possible: A ticket belongs to an attendee Example: select * from tickets JOIN attendees ON attendee.id = tickets.attendee_id An attendee has a decimal column called \"revenue\" That said, I need to run a query that will return a variety of information about the tickets, including the total revenue. The problem is that if 2 tickets belong to the same attendee, it counts their revenue twice.

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

What's the difference between RANK() and DENSE_RANK() functions in oracle?

雨燕双飞 提交于 2019-11-26 06:55:55
问题 What\'s the difference between RANK() and DENSE_RANK() functions? How to find out nth salary in the following emptbl table? DEPTNO EMPNAME SAL ------------------------------ 10 rrr 10000.00 11 nnn 20000.00 11 mmm 5000.00 12 kkk 30000.00 10 fff 40000.00 10 ddd 40000.00 10 bbb 50000.00 10 ccc 50000.00 If in the table data having nulls , what will happen if I want to find out nth salary? 回答1: RANK gives you the ranking within your ordered partition. Ties are assigned the same rank, with the next

Spark SQL window function with complex condition

雨燕双飞 提交于 2019-11-26 05:49:59
问题 This is probably easiest to explain through example. Suppose I have a DataFrame of user logins to a website, for instance: scala> df.show(5) +----------------+----------+ | user_name|login_date| +----------------+----------+ |SirChillingtonIV|2012-01-04| |Booooooo99900098|2012-01-04| |Booooooo99900098|2012-01-06| | OprahWinfreyJr|2012-01-10| |SirChillingtonIV|2012-01-11| +----------------+----------+ only showing top 5 rows I would like to add to this a column indicating when they became an

Postgres window function and group by exception

梦想与她 提交于 2019-11-26 05:37:01
问题 I\'m trying to put together a query that will retrieve the statistics of a user (profit/loss) as a cumulative result, over a period of time. Here\'s the query I have so far: SELECT p.name, e.date, sum(sp.payout) OVER (ORDER BY e.date) - sum(s.buyin) OVER (ORDER BY e.date) AS \"Profit/Loss\" FROM result r JOIN game g ON r.game_id = g.game_id JOIN event e ON g.event_id = e.event_id JOIN structure s ON g.structure_id = s.structure_id JOIN structure_payout sp ON g.structure_id = sp.structure_id