row-number

Is it possible to add a identity to a GROUP BY using SQL?

烂漫一生 提交于 2019-12-06 06:04:14
问题 Is it possible to add a identity column to a GROUP BY so that each duplicate has a identity number? My original data looks like this: 1 AAA [timestamp] 2 AAA [timestamp] 3 BBB [timestamp] 4 CCC [timestamp] 5 CCC [timestamp] 6 CCC [timestamp] 7 DDD [timestamp] 8 DDD [timestamp] 9 EEE [timestamp] .... And I want to convert it to: 1 AAA 1 2 AAA 2 4 CCC 1 5 CCC 2 6 CCC 3 7 DDD 1 8 DDD 2 ... The solution was: CREATE PROCEDURE [dbo].[RankIt] AS BEGIN SET NOCOUNT ON; SELECT *, RANK() OVER(PARTITION

Selecting every Nth row per user in Postgres

岁酱吖の 提交于 2019-12-06 04:41:27
I was using this SQL statement: SELECT "dateId", "userId", "Salary" FROM ( SELECT *, (row_number() OVER (ORDER BY "userId", "dateId"))%2 AS rn FROM user_table ) sa WHERE sa.rn=1 AND "userId" = 789 AND "Salary" > 0; But every time the table gets new rows the result of the query is different. Am I missing something? Assuming that ("dateId", "userId") is unique and new rows always have a bigger (later) dateId . After some comments: What I think you need: SELECT "dateId", "userId", "Salary" FROM ( SELECT "dateId", "userId", "Salary" ,(row_number() OVER ( PARTITION BY "userId" -- either this ORDER

Creating a row number of each row in PySpark DataFrame using row_number() function with Spark version 2.2

雨燕双飞 提交于 2019-12-05 17:56:25
I am having a PySpark DataFrame - valuesCol = [('Sweden',31),('Norway',62),('Iceland',13),('Finland',24),('Denmark',52)] df = sqlContext.createDataFrame(valuesCol,['name','id']) +-------+---+ | name| id| +-------+---+ | Sweden| 31| | Norway| 62| |Iceland| 13| |Finland| 24| |Denmark| 52| +-------+---+ I wish to add a row column to this DataFrame, which is the row number (serial number) of the row, like shown below - My final output should be: +-------+---+--------+ | name| id|row_num | +-------+---+--------+ | Sweden| 31| 1| | Norway| 62| 2| |Iceland| 13| 3| |Finland| 24| 4| |Denmark| 52| 5| +-

LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 05:08:23
After much Google searching and code experimentation, I'm stumped on a complex C# LINQ-to-objects problem which in SQL would be easy to solve with a pair of ROW_NUMBER()...PARTITION BY functions and a subquery or two. Here's, in words, what I'm trying to do in code-- the underlying requirement is removing duplicate documents from a list: First, group a list by (Document.Title, Document.SourceId), assuming a (simplified) class definition like this: class Document { string Title; int SourceId; // sources are prioritized (ID=1 better than ID=2) } Within that group, assign each document an index

How can I delete or select a row from a table that has a specific row number?

大兔子大兔子 提交于 2019-12-04 17:16:52
I have a question about Microsoft SQL Server 2005. How can I delete or select a row from a table that has a specific row number? Edit: Modified the code so that it matches more closely to OP's intentions Declare @RowNum as INT SET @RowNum = 15 ---Just for example WITH OrdersRN AS ( SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum ,OrderID ,OrderDate ,CustomerID ,EmployeeID FROM dbo.Orders ) SELECT * FROM OrdersRN WHERE RowNum = @RowNum ORDER BY OrderDate ,OrderID; Check this URL out. Since SQL Server 2005, there is a function called "row_number()" that is what you are looking

MySQL - Select row number of a record

吃可爱长大的小学妹 提交于 2019-12-04 12:59:09
I have a table in MySQL populated as follows. Now I need to select the row number of a record in its sorted order. For example, the row number of words starting with 'c' should be 4 . Words ===== coffee banana apple cherry blackberry I tried the following query, but I get wrong results. Here dict is the table name and words is the column name. SELECT @rownum:=@rownum + 1 id FROM (SELECT * FROM dict ORDER BY words) d,(SELECT @rownum:=0) r WHERE d.words LIKE CONCAT('c','%') For the above query, I am getting the row numbers for the outer query. But I want the row numbers of the internal query. I

Is it possible to add a identity to a GROUP BY using SQL?

本小妞迷上赌 提交于 2019-12-04 11:18:37
Is it possible to add a identity column to a GROUP BY so that each duplicate has a identity number? My original data looks like this: 1 AAA [timestamp] 2 AAA [timestamp] 3 BBB [timestamp] 4 CCC [timestamp] 5 CCC [timestamp] 6 CCC [timestamp] 7 DDD [timestamp] 8 DDD [timestamp] 9 EEE [timestamp] .... And I want to convert it to: 1 AAA 1 2 AAA 2 4 CCC 1 5 CCC 2 6 CCC 3 7 DDD 1 8 DDD 2 ... The solution was: CREATE PROCEDURE [dbo].[RankIt] AS BEGIN SET NOCOUNT ON; SELECT *, RANK() OVER(PARTITION BY col2 ORDER BY timestamp DESC) AS ranking FROM MYTABLE; END You could try using ROW_NUMBER if you are

Should SQL ranking functionality be considered as “use with caution”

♀尐吖头ヾ 提交于 2019-12-04 04:05:56
问题 This question originates from a discussion on whether to use SQL ranking functionality or not in a particular case. Any common RDBMS includes some ranking functionality, i.e. it's query language has elements like TOP n ... ORDER BY key , ROW_NUMBER() OVER (ORDER BY key) , or ORDER BY key LIMIT n (overview). They do a great job in increasing performance if you want to present only a small chunk out of a huge number of records. But they also introduce a major pitfall: If key is not unique

Select subset of rows using Row_Number()

 ̄綄美尐妖づ 提交于 2019-12-03 16:21:27
Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as 'RowNo' from customers where RowNo between 50 AND 60 I am trying to select a subset of rows between 50 and 60 . The problem is 'RowNo' is an invalid column name. Thank you Using SQL SERVER 2008 R2 Michał Powaga Use your query as subquery like bellow: select * from ( Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as [RowNo] from customers ) t where RowNo between 50 AND 60 You can use CTE as well but whether to choose one over another read Difference between CTE and SubQuery? and check execution plan. You need to do something like

Row numbering in PostgreSQL

試著忘記壹切 提交于 2019-12-03 15:46:39
问题 How to get row number in PostgreSQL when the results are ordered by some column? e.g. SELECT 30+row_number() AS position, * FROM users ORDER BY salary DESC LIMIT 30 OFFSET 30 I supposed that the query would return list like this: position | name | salary 31 | Joy | 4500 32 | Katie| 4000 33 | Frank| 3500 Actually i have to duplicate the ORDER clause into the query to make it functional: SELECT 30+row_number(ORDER BY salary DESC) AS position, * FROM users ORDER BY salary DESC LIMIT 30 OFFSET 30