window-functions

Select until row matches in postgresql?

守給你的承諾、 提交于 2019-12-01 12:08:49
Is there a way to select rows until some condition is met? I.e. a type of limit , but not limited to N rows, but to all the rows until the first non-matching row? For example, say I have the table: CREATE TABLE t (id SERIAL PRIMARY KEY, rank INTEGER, value INTEGER); INSERT INTO t (rank, value) VALUES ( 1, 1), (2, 1), (2,2),(3,1); that is: test=# SELECT * FROM t; id | rank | value ----+------+------- 1 | 1 | 1 2 | 2 | 1 3 | 2 | 2 4 | 3 | 1 (4 rows) I want to order by rank, and select up until the first row that is over 1. I.e. SELECT * FROM t ORDER BY rank UNTIL value>1 and I want the first 2

First and last value of window function in one row in PostgreSQL

僤鯓⒐⒋嵵緔 提交于 2019-12-01 11:20:49
I'd like to have first value of one column and last value of second column in one row for a specified partition. For that I created this query: SELECT DISTINCT b.machine_id, batch, timestamp_sta, timestamp_stp, FIRST_VALUE(timestamp_sta) OVER w AS batch_start, LAST_VALUE(timestamp_stp) OVER w AS batch_end FROM db_data.sta_stp AS a JOIN db_data.ll_lu AS b ON a.ll_lu_id=b.id WINDOW w AS (PARTITION BY batch, machine_id ORDER BY timestamp_sta) ORDER BY timestamp_sta, batch, machine_id; But as you can see in the image, returned data in batch_end column are not correct. batch_start column has

Select until row matches in postgresql?

穿精又带淫゛_ 提交于 2019-12-01 10:21:44
问题 Is there a way to select rows until some condition is met? I.e. a type of limit , but not limited to N rows, but to all the rows until the first non-matching row? For example, say I have the table: CREATE TABLE t (id SERIAL PRIMARY KEY, rank INTEGER, value INTEGER); INSERT INTO t (rank, value) VALUES ( 1, 1), (2, 1), (2,2),(3,1); that is: test=# SELECT * FROM t; id | rank | value ----+------+------- 1 | 1 | 1 2 | 2 | 1 3 | 2 | 2 4 | 3 | 1 (4 rows) I want to order by rank, and select up until

Invalid count and sum in cross tab query using PostgreSQL

你离开我真会死。 提交于 2019-12-01 08:39:23
I am using PostgreSQL 9.3 version database. I have a situation where I want to count the number of products sales and sum the amount of product and also want to show the cities in a column where the product have sale. Example Setup create table products ( name varchar(20), price integer, city varchar(20) ); insert into products values ('P1',1200,'London'), ('P1',100,'Melborun'), ('P1',1400,'Moscow'), ('P2',1560,'Munich'), ('P2',2300,'Shunghai'), ('P2',3000,'Dubai'); Crosstab query : select * from crosstab ( 'select name,count(*),sum(price),city,count(city) from products group by name,city

Crosstab function in Postgres returning a one row output when I expect multiple rows

被刻印的时光 ゝ 提交于 2019-12-01 06:08:28
I currently have a table m of the following format: id scenario period ct 2 1 1 1 2 1 2 1 2 1 3 1 2 1 4 1 2 2 1 1 2 2 2 1 2 2 3 1 2 2 4 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 I want to create the following table: id scenario period 1 2 3 4 2 1 1 1 2 1 2 1 2 1 3 1 2 1 4 1 2 2 1 1 2 2 2 1 2 2 3 1 2 2 4 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 The tablefunc extension has been created in my Postgres database already. I'm currently trying to use the crosstab() function to complete the pivot. However, I'm getting a table that looks like the following: id scenario period 1 2 3 4 2 1 1 1 1 1 1 The query I tried:

Crosstab function in Postgres returning a one row output when I expect multiple rows

喜夏-厌秋 提交于 2019-12-01 03:44:15
问题 I currently have a table m of the following format: id scenario period ct 2 1 1 1 2 1 2 1 2 1 3 1 2 1 4 1 2 2 1 1 2 2 2 1 2 2 3 1 2 2 4 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 I want to create the following table: id scenario period 1 2 3 4 2 1 1 1 2 1 2 1 2 1 3 1 2 1 4 1 2 2 1 1 2 2 2 1 2 2 3 1 2 2 4 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 The tablefunc extension has been created in my Postgres database already. I'm currently trying to use the crosstab() function to complete the pivot. However, I'm

GROUP BY consecutive dates delimited by gaps

梦想的初衷 提交于 2019-12-01 03:20:53
Assume you have (in Postgres 9.1 ) a table like this: date | value which have some gaps in it (I mean: not every possible date between min(date) and max(date) has it's row). My problem is how to aggregate this data so that each consistent group (without gaps) is treated separately, like this: min_date | max_date | [some aggregate of "value" column] Any ideas how to do it? I believe it is possible with window functions but after a while trying with lag() and lead() I'm a little stuck. For instance if the data are like this: date | value ---------------+------- 2011-10-31 | 2 2011-11-01 | 8 2011

How to “reset” running SUM after it reaches a threshold?

放肆的年华 提交于 2019-12-01 01:01:49
I wrote a query that creates two columns: the_day , and the amount_raised on that day. Here is what I have: And I would like to add a column that has a running sum of amount_raised : Ultimately, I would like the sum column to reset after it reaches 1 million. The recursive approach is above my pay grade, so if anyone knows a way to reset the sum without creating an entirely new table, please comment (maybe with a RESET function?). Thank you I'd like to thank Juan Carlos Oropeza for providing a script and SQLFiddle with the test data. George, you should have done that. The query itself it

Select data for 15 minute windows - PostgreSQL

☆樱花仙子☆ 提交于 2019-11-30 23:29:52
Right so I have a table such as this in PostgreSQL: timestamp duration 2013-04-03 15:44:58 4 2013-04-03 15:56:12 2 2013-04-03 16:13:17 9 2013-04-03 16:16:30 3 2013-04-03 16:29:52 1 2013-04-03 16:38:25 1 2013-04-03 16:41:37 9 2013-04-03 16:44:49 1 2013-04-03 17:01:07 9 2013-04-03 17:07:48 1 2013-04-03 17:11:00 2 2013-04-03 17:11:16 2 2013-04-03 17:15:17 1 2013-04-03 17:16:53 4 2013-04-03 17:20:37 9 2013-04-03 17:20:53 3 2013-04-03 17:25:48 3 2013-04-03 17:29:26 1 2013-04-03 17:32:38 9 2013-04-03 17:36:55 4 And I would like to get the following output: timestampwindowstart = 2013-04-03 15:44:58

How to add a running count to rows in a 'streak' of consecutive days

匆匆过客 提交于 2019-11-30 22:23:45
Thanks to Mike for the suggestion to add the create/insert statements. create table test ( pid integer not null, date date not null, primary key (pid, date) ); insert into test values (1,'2014-10-1') , (1,'2014-10-2') , (1,'2014-10-3') , (1,'2014-10-5') , (1,'2014-10-7') , (2,'2014-10-1') , (2,'2014-10-2') , (2,'2014-10-3') , (2,'2014-10-5') , (2,'2014-10-7'); I want to add a new column that is 'days in current streak' so the result would look like: pid | date | in_streak -------|-----------|---------- 1 | 2014-10-1 | 1 1 | 2014-10-2 | 2 1 | 2014-10-3 | 3 1 | 2014-10-5 | 1 1 | 2014-10-7 | 1 2