window-functions

Query to find all timestamps more than a certain interval apart

妖精的绣舞 提交于 2019-12-24 04:43:07
问题 I'm using postgres to run some analytics on user activity. I have a table of all requests(pageviews) made by every user and the timestamp of the request, and I'm trying to find the number of distinct sessions for every user. For the sake of simplicity, I'm considering every set of requests an hour or more apart from others as a distinct session. The data looks something like this: id| request_time| user_id 1 2014-01-12 08:57:16.725533 1233 2 2014-01-12 08:57:20.944193 1234 3 2014-01-12 09:15

Subtract two records of the same column in a table

别说谁变了你拦得住时间么 提交于 2019-12-24 00:24:44
问题 I am using PostgreSQL and I want to subtract two records of the same table and use the result in the same query. Here is the table: Scores 6 8 9 Result 6 2 1 What I want to do: Result = Score(i) - Score(i-1) In the end I want the sum of these results. sum(result) must be 9 in my example. 回答1: You need some way to determine the sequence of rows in score . There is no "natural order" in a table in a relational database. So I assume you have an id (or a timestamp or something) to order your

How to calculate the current row with the next one?

前提是你 提交于 2019-12-23 22:20:06
问题 In Spark-Sql version 1.6, using DataFrame s, is there a way to calculate, for a specific column, the sum of the current row and the next one, for every row? For example, if I have a table with one column, like so Age 12 23 31 67 I'd like the following output Sum 35 54 98 The last row is dropped because it has no "next row" to be added to. Right now I am doing it by ranking the table and joining it with itself, where the rank is equals to rank+1 . Is there a better way to do this? Can this be

How to determine Strahler number on a directed graph for a stream network

南笙酒味 提交于 2019-12-23 19:18:57
问题 Question / example / expected values I need to determine a Strahler number or Strahler stream order for a directed graph representing a stream network. I can derive information forwards and backwards using WITH RECURSIVE queries, but it seems I need to do something different to determine the Strahler number. For example, here is a 19 segment stream network with 10 tributaries and one outlet. The upstream portion of each segment is represented by a node ID. And the same data in a table

Return array of years as year ranges

二次信任 提交于 2019-12-23 16:33:21
问题 I'm attempting to query a table which contains a character varying[] column of years, and return those years as a string of comma-delimited year ranges. The year ranges would be determined by sequential years present within the array, and years/year ranges which are not sequential should be separated be commas. The reason the data-type is character varying[] rather than integer[] is because a few of the values contain ALL instead of a list of years. We can omit these results. So far I've had

Return array of years as year ranges

我的未来我决定 提交于 2019-12-23 16:32:40
问题 I'm attempting to query a table which contains a character varying[] column of years, and return those years as a string of comma-delimited year ranges. The year ranges would be determined by sequential years present within the array, and years/year ranges which are not sequential should be separated be commas. The reason the data-type is character varying[] rather than integer[] is because a few of the values contain ALL instead of a list of years. We can omit these results. So far I've had

SQL Condition on Window function

那年仲夏 提交于 2019-12-23 13:13:38
问题 I want to do a special request on my database (PostgreSQL v9.4.5), but I don't manage to do it. In order to simply, let's say I have the following table AvgTemperatures , representing different averages of temperature taken in different cities, and calculated on different length of time (counted in months) : id | city | avg | months ----+-----------+------+-------- 1 | New-York | 20 | 3 <--- average temperate over the last 3 months 2 | New-York | 19 | 6 <--- average temperate over the last 6

How to use a window function to determine when to perform different tasks in Hive or Postgres?

旧城冷巷雨未停 提交于 2019-12-23 01:05:07
问题 I am new to SQL and need to be able to solve the following problem in both Hive and Postgres. Data I have a some data showing the start day and end day for different pre-prioritised tasks per person: person task_key start_day end_day 1 Kate A 1 5 2 Kate B 1 5 3 Adam A 1 5 4 Adam B 2 5 5 Eve A 2 5 6 Eve B 1 5 7 Jason A 1 5 8 Jason B 4 5 9 Jason C 3 5 10 Jason D 5 5 11 Jason E 4 5 NOTE: Task key is ordered so that higher letters have higher priorities. Question I need to work out which task

Tsql union query

一世执手 提交于 2019-12-22 17:54:24
问题 I’m looking for an efficient way to query a table. The table structure is: CREATE TABLE [dbo].[CaseManager]( [CaseID] [int] IDENTITY(1,1) NOT NULL, [SystemUserCreatedBy] [int] NULL, [SystemUserAssignee] [int] NULL, CONSTRAINT [PK_Case] PRIMARY KEY CLUSTERED ( [CaseID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY] ) ON [PRIMARY] The query should return for every caseID and userid

How do i do running totals from second column

半城伤御伤魂 提交于 2019-12-22 12:39:25
问题 I have a data set like below, Lot Size Reported QTY Qty Balance 150 100 150 100 150 80 150 80 150 5 The Qty Balance needs to calculated as follows, Row 1 = Lot Size - Reported Qty (row1) => 150-100 = 50 Row 2 = Reported Qty (row1) - Reported Qty(row2) => 100-100 =0 Row 3 = Reported Qty (row2) - Reported Qty(row3) => 100-80 =20 ... till the last row My expected result is Lot Size Reported QTY Qty Balance 150 100 50 150 100 0 150 80 20 150 80 0 150 5 75 How do I achieve this in a query? 回答1: