window-functions

Select distinct users group by time range

青春壹個敷衍的年華 提交于 2020-01-24 09:30:50
问题 I have a table with the following info |date | user_id | week_beg | month_beg| SQL to create table with test values: CREATE TABLE uniques ( date DATE, user_id INT, week_beg DATE, month_beg DATE ) INSERT INTO uniques VALUES ('2013-01-01', 1, '2012-12-30', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-03', 3, '2012-12-30', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-06', 4, '2013-01-06', '2013-01-01') INSERT INTO uniques VALUES ('2013-01-07', 4, '2013-01-06', '2013-01-01') INPUT

Count max. number of concurrent user sessions per day

时光毁灭记忆、已成空白 提交于 2020-01-22 20:38:46
问题 Situation We have a PostgreSQL 8.4 database containing user sessions with login date/time and logout date/time per row. Our web application records this time and also handles the case when user does not logout explicitly (session timeout). So a login date/time and logout date/time are given in every case. Goal I need user statistics of the max number of concurrent sessions a day. So, I can say the following: "At 2015-03-16 the peak of concurrent users logged in was six ." Similar questions A

Rank function in MySQL with Order By clause

一个人想着一个人 提交于 2020-01-20 04:06:47
问题 How could this (Oracle) SQL: select a.*, rank() over (partition by a.field1 order by a.field2 desc) field_rank from table_a a order by a.field1, a.field2 be translated into MySQL? This question seems to be similar but there is no Order By in the end of the base query. Also, does it matter that it is ordered by the fields of partition? 回答1: According to the link you gave it should look like this: SELECT a.*, ( CASE a.field1 WHEN @curType THEN @curRow := @curRow + 1 ELSE @curRow := 1 AND

Rank function in MySQL with Order By clause

不打扰是莪最后的温柔 提交于 2020-01-20 04:06:14
问题 How could this (Oracle) SQL: select a.*, rank() over (partition by a.field1 order by a.field2 desc) field_rank from table_a a order by a.field1, a.field2 be translated into MySQL? This question seems to be similar but there is no Order By in the end of the base query. Also, does it matter that it is ordered by the fields of partition? 回答1: According to the link you gave it should look like this: SELECT a.*, ( CASE a.field1 WHEN @curType THEN @curRow := @curRow + 1 ELSE @curRow := 1 AND

Rank rows in a column under conditions on a different column

拈花ヽ惹草 提交于 2020-01-17 13:09:15
问题 I have the following dataset: id | date | state ----------------------- 1 | 01/01/17 | high 1 | 02/01/17 | high 1 | 03/01/17 | high 1 | 04/01/17 | miss 1 | 05/01/17 | high 2 | 01/01/17 | miss 2 | 02/01/17 | high 2 | 03/01/17 | high 2 | 04/01/17 | miss 2 | 05/01/17 | miss 2 | 06/01/17 | high I want to create a column rank_state which ranks, within groups of id , the entries as per increasing date (starting from rank 0) which do not have the state of "miss". Furthermore, the rank repeats itself

Rank rows in a column under conditions on a different column

南楼画角 提交于 2020-01-17 13:04:06
问题 I have the following dataset: id | date | state ----------------------- 1 | 01/01/17 | high 1 | 02/01/17 | high 1 | 03/01/17 | high 1 | 04/01/17 | miss 1 | 05/01/17 | high 2 | 01/01/17 | miss 2 | 02/01/17 | high 2 | 03/01/17 | high 2 | 04/01/17 | miss 2 | 05/01/17 | miss 2 | 06/01/17 | high I want to create a column rank_state which ranks, within groups of id , the entries as per increasing date (starting from rank 0) which do not have the state of "miss". Furthermore, the rank repeats itself

PostgreSQL: Grouping then filtering table, with condition for nonexistence

こ雲淡風輕ζ 提交于 2020-01-16 04:15:07
问题 In PostgreSQL, I have a table that, abstractly, looks like this: ╔═══╦═══╦═══╦═══╗ ║ A ║ B ║ C ║ D ║ ╠═══╬═══╬═══╬═══╣ ║ x ║ 0 ║ y ║ 0 ║ ║ x ║ 0 ║ x ║ 1 ║ ║ x ║ 1 ║ y ║ 0 ║ ║ x ║ 1 ║ z ║ 1 ║ ║ y ║ 0 ║ z ║ 0 ║ ║ y ║ 0 ║ x ║ 0 ║ ║ y ║ 1 ║ y ║ 0 ║ ╚═══╩═══╩═══╩═══╝ I want to transform it in a query into this: ╔═══╦═══╦══════╗ ║ A ║ B ║ D ║ ╠═══╬═══╬══════╣ ║ x ║ 0 ║ 1 ║ ║ x ║ 1 ║ null ║ ║ y ║ 0 ║ null ║ ║ y ║ 1 ║ 0 ║ ╚═══╩═══╩══════╝ …such that: The input table’s rows are grouped by A and B, and

Iterating through PostgreSQL records. How to reference data from next row?

↘锁芯ラ 提交于 2020-01-13 13:13:17
问题 I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the

Iterating through PostgreSQL records. How to reference data from next row?

那年仲夏 提交于 2020-01-13 13:10:11
问题 I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the

Spark SQL Window over interval of between two specified time boundaries - between 3 hours and 2 hours ago

烂漫一生 提交于 2020-01-13 05:37:29
问题 What is the proper way of specifying window interval in Spark SQL, using two predefined boundaries? I am trying to sum up values from my table over a window of "between 3 hours ago and 2 hours ago". When I run this query: select *, sum(value) over ( partition by a, b order by cast(time_value as timestamp) range between interval 2 hours preceding and current row ) as sum_value from my_temp_table; That works. I get results that I expect, i.e. sums of values that fall into 2 hours rolling window