greatest-n-per-group

Highest per each group

眉间皱痕 提交于 2019-12-05 17:50:26
It's hard to show my actual table and data here so I'll describe my problem with a sample table and data: create table foo(id int,x_part int,y_part int,out_id int,out_idx text); insert into foo values (1,2,3,55,'BAK'),(2,3,4,77,'ZAK'),(3,4,8,55,'RGT'),(9,10,15,77,'UIT'), (3,4,8,11,'UTL'),(3,4,8,65,'MAQ'),(3,4,8,77,'YTU'); Following is the table foo : id x_part y_part out_id out_idx -- ------ ------ ------ ------- 3 4 8 11 UTL 3 4 8 55 RGT 1 2 3 55 BAK 3 4 8 65 MAQ 9 10 15 77 UIT 2 3 4 77 ZAK 3 4 8 77 YTU I need to select all fields by sorting the highest id of each out_id . Expected output: id

Select most recent rows that match a condition in SQLite

冷暖自知 提交于 2019-12-05 14:21:51
Let's say I have a table: Name, status, timestamp And I want to select the rows that match status='active' but only those that have the most recent timestamp for each of them. So if there were rows like this: Bob, active, 5/10/2010 Bob, active, 6/12/2010 Ann, inactive, 6/12/2000 Ann, active, 9/3/2009 Ann, active, 9/25/2010 I'd want it to return: Bob, active, 6/12/2010 Ann, active, 9/25/2010 How can I do this? I'm using SQLite, if it matters. Thank you. select name, status, max(timestamp) from my_table where status = 'active' group by name, status take a look at http://www.w3schools.com/sql/sql

Join single row from a table in MySQL

蓝咒 提交于 2019-12-05 12:04:04
I have two tables players and scores . I want to generate a report that looks something like this: player first score points foo 2010-05-20 19 bar 2010-04-15 29 baz 2010-02-04 13 Right now, my query looks something like this: select p.name player, min(s.date) first_score, s.points points from players p join scores s on s.player_id = p.id group by p.name, s.points I need the s.points that is associated with the row that min(s.date) returns. Is that happening with this query? That is, how can I be certain I'm getting the correct s.points value for the joined row? Side note: I imagine this is

Finding each customer group's most recent account

大憨熊 提交于 2019-12-05 10:25:08
问题 I have a table that contains customers information. Each customer is assigned a Customer ID (their SSN) that they retain as they open more accounts. Two customers may be on the same account, each with their own ID. The account numbers are not ordered by date. I would like to find the most recent account of each customer or group of customers. If two customers have ever been on an account together, I want to return the most recent account either customer has been on. Here is a sample table

SQLite: return only top 2 results within each group

时光毁灭记忆、已成空白 提交于 2019-12-05 09:42:05
I checked other solutions to similar problems, but sqlite does not support row_number() and rank() functions or there are no examples which involve joining multiple tables, grouping them by multiple columns and returning only top N results for each group at the same time. Here's the code i run db = sqlite3.connect('mydb') cursor = db.cursor() cursor.execute( ''' CREATE TABLE orders( id INTEGER PRIMARY KEY, product_id INTEGER, client_id INTEGER ) ''' ) cursor.execute( ''' CREATE TABLE clients( id INTEGER PRIMARY KEY, gender TEXT, city TEXT ) ''' ) cursor.execute( ''' CREATE TABLE products( id

SQL command for finding the second highest salary

此生再无相见时 提交于 2019-12-05 07:35:28
问题 HI, Can u tell me the syntax of the SQL command which gives as output the second highest salary from a range of salaries stored in the employee table. A description of the SQL commnd will be welcomed... Please help!!! 回答1: select min(salary) from (select top 2 salary from SalariesTable order by salary desc) as ax 回答2: This should work: select * from ( select t.*, dense_rank() over (order by salary desc) rnk from employee t ) a where rnk = 2; This returns the second highest salary. dense_rank(

sql select earliest date for multiple rows

萝らか妹 提交于 2019-12-05 03:12:26
问题 I have a database that looks like the following; circuit_uid | customer_name | location | reading_date | reading_time | amps | volts | kw | kwh | kva | pf | key -------------------------------------------------------------------------------------------------------------------------------------- cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 00:01:01 | 4.51 | 229.32 | 1.03 | 87 | 1.03 | 0.85 | 15 cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 01:01:01 | 4.18 | 230.3 | 0.96 | 90 | 0.96 |

Use something like TOP with GROUP BY

a 夏天 提交于 2019-12-05 02:50:46
With table table1 like below +--------+-------+-------+------------+-------+ | flight | orig | dest | passenger | bags | +--------+-------+-------+------------+-------+ | 1111 | sfo | chi | david | 3 | | 1112 | sfo | dal | david | 7 | | 1112 | sfo | dal | kim | 10| | 1113 | lax | san | ameera | 5 | | 1114 | lax | lfr | tim | 6 | | 1114 | lax | lfr | jake | 8 | +--------+-------+-------+------------+-------+ I'm aggregating the table by orig like below select orig , count(*) flight_cnt , count(distinct passenger) as pass_cnt , percentile_cont(0.5) within group ( order by bags ASC) as bag_cnt

SQL Server - SELECT TOP 5 rows for each FK

半世苍凉 提交于 2019-12-05 00:17:58
问题 I've got the following query, that looks up the TOP 5 Products matching the search. Each Product is associated with a Shop SELECT TOP 5 * FROM Products p, Shops s WHERE p.ShopId = s.ShopId AND p.ProductName LIKE '%christmas%' I need to extend this so that it returns me the TOP 5 Products in each Shop . Could anyone let me know how the query could be modified to achieve this? - i.e. choose the TOP 5 products matching "%christmas%" in each shop (rather than the current which shows the TOP 5

Using a limit on a left join in mysql

牧云@^-^@ 提交于 2019-12-04 23:27:12
The following query selects all posts and each post's owner, all of the comments that belong to each post, and the owner of each comment. I need to only retrieve 5 comments per post. I rewrote the query, but I get an error of "each derived table must have it's own alias". SELECT posts.id AS postId, posts.body, users.id AS userId, users.displayname, comments.id AS commentId, comments.text, commenters.id, commenters.displayname FROM posts JOIN users ON posts.owneruserid = users.id LEFT JOIN comments ON posts.id = comments.postid JOIN users AS commenters ON comments.userId = commenters.id ORDER