window-functions

TSQL OVER clause: COUNT(*) OVER (ORDER BY a)

≯℡__Kan透↙ 提交于 2019-12-03 16:58:44
问题 This is my code: USE [tempdb]; GO IF OBJECT_ID(N'dbo.t') IS NOT NULL BEGIN DROP TABLE dbo.t END GO CREATE TABLE dbo.t ( a NVARCHAR(8), b NVARCHAR(8) ); GO INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('e', NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL

Aggregating connected sets of nodes / edges

半腔热情 提交于 2019-12-03 16:23:56
I have a connected set of edges with unique nodes. They are connected using a parent node. Consider the following example code and illustration: CREATE TABLE network ( node integer PRIMARY KEY, parent integer REFERENCES network(node), length numeric NOT NULL ); CREATE INDEX ON network (parent); INSERT INTO network (node, parent, length) VALUES (1, NULL, 1.3), (2, 1, 1.2), (3, 2, 0.9), (4, 3, 1.4), (5, 4, 1.6), (6, 2, 1.5), (7, NULL, 1.0); Visually, two groups of edges can be identified. How can the two groups be identified using PostgreSQL 9.1, and length summed? The expected result is shown:

Filtering by window function result in Postgresql

泄露秘密 提交于 2019-12-03 14:56:40
问题 Ok, initially this was just a joke we had with a friend of mine, but it turned into interesting technical question :) I have the following stuff table: CREATE TABLE stuff ( id serial PRIMARY KEY, volume integer NOT NULL DEFAULT 0, priority smallint NOT NULL DEFAULT 0, ); The table contains the records for all of my stuff, with respective volume and priority (how much I need it). I have a bag with specified volume, say 1000 . I want to select from the table all stuff I can put into a bag,

Oracle Lag function with dynamic parameter

删除回忆录丶 提交于 2019-12-03 13:45:12
I have a specific problem. I have a table which contains invalid values. I need to replace the invalid values (here 0 ) with the previous value which is bigger than 0 . The difficulty is, it is not appropiate for me to use an Update or an insert(Cursor and update would do it).Well my only way is to use a Select statement. When I use the lag(col1, 1) - function with when case, I get only one column with the correct value. select col1, col2 realcol2, (case when col2 = 0 then lag(col2,1,1) over (partition by col1 order by col1 ) else col2 end ) col2, col3 realcol3, (case when col3 = 0 then lag

SQL issue - calculate max days sequence

对着背影说爱祢 提交于 2019-12-03 07:52:48
There is a table with visits data: uid (INT) | created_at (DATETIME) I want to find how many days in a row a user has visited our app. So for instance: SELECT DISTINCT DATE(created_at) AS d FROM visits WHERE uid = 123 will return: d ------------ 2012-04-28 2012-04-29 2012-04-30 2012-05-03 2012-05-04 There are 5 records and two intervals - 3 days (28 - 30 Apr) and 2 days (3 - 4 May). My question is how to find the maximum number of days that a user has visited the app in a row (3 days in the example). Tried to find a suitable function in the SQL docs, but with no success. Am I missing something

PostgreSQL window function: row_number() over (partition col order by col2)

假如想象 提交于 2019-12-03 06:33:01
Following result set is derived from a sql query with a few joins and a union. The sql query already groups rows on Date and game. I need a column to describe the number of attempts at a game partitioned by date column. Username Game ID Date johndoe1 Game_1 100 7/22/14 1:52 AM johndoe1 Game_1 100 7/22/14 1:52 AM johndoe1 Game_1 100 7/22/14 1:52 AM johndoe1 Game_1 100 7/22/14 1:52 AM johndoe1 Game_1 121 7/22/14 1:56 AM johndoe1 Game_1 121 7/22/14 1:56 AM johndoe1 Game_1 121 7/22/14 1:56 AM johndoe1 Game_1 121 7/22/14 1:56 AM johndoe1 Game_1 121 7/22/14 1:56 AM johndoe1 Game_1 130 7/22/14 1:59

Using window functions in an update statement

廉价感情. 提交于 2019-12-03 06:23:13
问题 I have a large PostgreSQL table which I access through Django. Because Django's ORM does not support window functions, I need to bake the results of a window function into the table as a regular column. I want to do something like this: UPDATE table_name SET col1 = ROW_NUMBER() OVER ( PARTITION BY col2 ORDER BY col3 ); But I get ERROR: cannot use window function in UPDATE Can anyone suggest an alternative approach? Passing the window function syntax through Django's .raw() method is not

TSQL OVER clause: COUNT(*) OVER (ORDER BY a)

偶尔善良 提交于 2019-12-03 05:56:42
This is my code: USE [tempdb]; GO IF OBJECT_ID(N'dbo.t') IS NOT NULL BEGIN DROP TABLE dbo.t END GO CREATE TABLE dbo.t ( a NVARCHAR(8), b NVARCHAR(8) ); GO INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('e', NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); GO SELECT a, b, COUNT(*) OVER (ORDER BY a) FROM t; On this page of BOL , Microsoft says that: If

Aggregate values over a range of hours, every hour

大憨熊 提交于 2019-12-03 05:44:38
I have a PostgreSQL 9.1 database with a table containing a timestamp and a measuring value '2012-10-25 01:00' 2 '2012-10-25 02:00' 5 '2012-10-25 03:00' 12 '2012-10-25 04:00' 7 '2012-10-25 05:00' 1 ... ... I need to average the value over a range of 8 hours, every hour. In other words, I need the average of 1h-8h, 2h-9h, 3h-10h etc. I have no idea how to proceed for such a query. I have looked everywhere but have also no clue what functionalities to look for. The closes I find are hourly/daily averages or block-averages (e.g. 1h-8h, 9h-16h etc.). But in these cases, the timestamp is simply

ROW_NUMBER Without ORDER BY

五迷三道 提交于 2019-12-03 05:34:13
I've to add row number in my existing query so that I can track how much data has been added into Redis. If my query failed so I can start from that row no which is updated in other table. Query to get data start after 1000 row from table SELECT * FROM (SELECT *, ROW_NUMBER() OVER (Order by (select 1)) as rn ) as X where rn > 1000 Query is working fine. If any way that I can get the row no without using order by. What is select 1 here? Is the query optimized or I can do it by other ways. Please provide the better solution. There is no need to worry about specifying constant in the ORDER BY