row-number

Trouble using ROW_NUMBER() OVER (PARTITION BY …)

十年热恋 提交于 2019-12-03 09:04:35
问题 I'm using SQL Server 2008 R2. I have table called EmployeeHistory with the following structure and sample data: EmployeeID Date DepartmentID SupervisorID 10001 20130101 001 10009 10001 20130909 001 10019 10001 20131201 002 10018 10001 20140501 002 10017 10001 20141001 001 10015 10001 20141201 001 10014 Notice that the Employee 10001 has been changing 2 departments and several supervisors over time. What I am trying to do is to list the start and end dates of this employee's employment in each

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

Row numbering in PostgreSQL

只谈情不闲聊 提交于 2019-12-03 04:25:13
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 Is there any other way how to return ordered and numbered results without necessity of duplicating the

return mySQL original row number

丶灬走出姿态 提交于 2019-12-02 09:58:33
问题 I want to get the row_number of the original record in the database. Is there any way to retrieve the original row number of a record in mysql? Suppose my statement is select course_id from course where subject='finance'; there can be multiple records for this query. Suppose they are from row numbers 4, 5, 7, 9 and 10. How can i retrieve these row_numbers from my query? 回答1: Check: With MySQL, how can I generate a column containing the record index in a table? SELECT c.course_id, @curRow :=

How to show the maximum number for each combination of customer and product in a specific state in Postgresql?

末鹿安然 提交于 2019-12-02 07:42:22
I just begin learning Postgresql recently. I have a table named 'sales': create table sales ( cust varchar(20), prod varchar(20), day integer, month integer, year integer, state char(2), quant integer ) insert into sales values ('Bloom', 'Pepsi', 2, 12, 2001, 'NY', 4232); insert into sales values ('Knuth', 'Bread', 23, 5, 2005, 'PA', 4167); insert into sales values ('Emily', 'Pepsi', 22, 1, 2006, 'CT', 4404); insert into sales values ('Emily', 'Fruits', 11, 1, 2000, 'NJ', 4369); insert into sales values ('Helen', 'Milk', 7, 11, 2006, 'CT', 210); ...... It looks like this: And there are 500

MySQL query: Using UNION and getting row number as part of SELECT

房东的猫 提交于 2019-12-02 07:27:29
问题 I have a union query as follows: (SELECT t.id, t.name, c.company AS owner, t.creation_date AS date, t.notes FROM tool t, client c WHERE t.id_customer = '15' AND t.trash_flag = '1') UNION (SELECT f.id, f.name, CONCAT(m.first_name, ' ', m.last_name) AS owner, f.date, f.notes FROM file f, meta m WHERE ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f.trash_flag = '1' AND m.user_id = f.id_user) ORDER BY 'name' 'ASC' LIMIT 0,20 Everything works fine

MySQL query: Using UNION and getting row number as part of SELECT

只愿长相守 提交于 2019-12-02 06:34:09
I have a union query as follows: (SELECT t.id, t.name, c.company AS owner, t.creation_date AS date, t.notes FROM tool t, client c WHERE t.id_customer = '15' AND t.trash_flag = '1') UNION (SELECT f.id, f.name, CONCAT(m.first_name, ' ', m.last_name) AS owner, f.date, f.notes FROM file f, meta m WHERE ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f.trash_flag = '1' AND m.user_id = f.id_user) ORDER BY 'name' 'ASC' LIMIT 0,20 Everything works fine but I have two questions: How do I add a column to the entire result set that gives the row number Could

Query to rank rows in groups

岁酱吖の 提交于 2019-12-02 02:54:25
问题 I'm using Apache Derby 10.10. I have a list of participants and would like to calculate their rank in their country, like this: | Country | Participant | Points | country_rank | |----------------|---------------------|--------|--------------| | Australia | Bridget Ciriac | 1 | 1 | | Australia | Austin Bjorklun | 4 | 2 | | Australia | Carrol Motto | 7 | 3 | | Australia | Valeria Seligma | 8 | 4 | | Australia | Desmond Miyamot | 27 | 5 | | Australia | Maryjane Digma | 33 | 6 | | Australia |

Query to rank rows in groups

自作多情 提交于 2019-12-02 01:08:48
I'm using Apache Derby 10.10. I have a list of participants and would like to calculate their rank in their country, like this: | Country | Participant | Points | country_rank | |----------------|---------------------|--------|--------------| | Australia | Bridget Ciriac | 1 | 1 | | Australia | Austin Bjorklun | 4 | 2 | | Australia | Carrol Motto | 7 | 3 | | Australia | Valeria Seligma | 8 | 4 | | Australia | Desmond Miyamot | 27 | 5 | | Australia | Maryjane Digma | 33 | 6 | | Australia | Kena Elmendor | 38 | 7 | | Australia | Emmie Hicke | 39 | 8 | | Australia | Kaitlyn Mund | 50 | 9 | |

SQL Query With Row_Number, order by and where clause

笑着哭i 提交于 2019-12-02 00:25:31
问题 I have the following SQL query: select ID, COLUMN1, COLUMN2 from (select ID, COLUMN1, COLUMN2, row_number() over (order by 2 DESC) NO from A_TABLE) where NO between 0 and 100 What I am trying to do is to select the first 100 records of the query select ID, COLUMN1, COLUMN2 from ATABLE order by 2 DESC And here are the problems: Apparently, the order by clause is not working. I've noticed that I have to add another order by 2 DESC clause, just after (...) from ATABLE , for my query to work. Is