greatest-n-per-group

Sql Query to get the record what has the max id number for each type

一个人想着一个人 提交于 2019-12-10 09:53:53
问题 I have a table in sql server. It has a pid field used as a primary key and a field called deviceid that has a device name. example: pid deviceid field3 field4 1 Device1 test test 2 Device2 test2 test2 3 Device1 test3 test3 4 Device2 test4 test4 For a query, i need select * from the table where the pid is the max per device. Example result: pid deviceid field3 field4 3 Device1 test3 test3 4 Device2 test4 test4 Im not sure how do do this. Any one have any ideas? The closest i got was: Select

MYSQL Group by column with 2 rows for each group

不问归期 提交于 2019-12-10 03:36:24
问题 I need 2 id for each group. SELECT `id`, `category`.`cat_name` FROM `info` LEFT JOIN `category` ON `info`.`cat_id` = `category`.`cat_id` WHERE `category`.`cat_name` IS NOT NULL GROUP BY `category`.`cat_name` ORDER BY `category`.`cat_name` ASC How to do this? Sample Data: id cat_name 1 Cat-1 2 Cat-1 3 Cat-2 4 Cat-1 5 Cat-2 6 Cat-1 7 Cat-2 Output Will be: id cat_name 6 Cat-1 4 Cat-1 7 Cat-2 5 Cat-2 回答1: If you need two arbitrary ids, then use min() and max() : SELECT c.`cat_name` , min(id), max

Use something like TOP with GROUP BY

别来无恙 提交于 2019-12-10 03:29:10
问题 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

retrieve the most recent record for each customer

别等时光非礼了梦想. 提交于 2019-12-09 00:16:13
问题 I have this data: ID NAME DATE 3 JOHN 2011-08-08 2 YOKO 2010-07-07 1 JOHN 2009-06-06 Code (for SQL Server 2005): DECLARE @TESTABLE TABLE (id int, name char(4), date smalldatetime) INSERT INTO @TESTABLE VALUES (3, 'JOHN', '2011-08-08') INSERT INTO @TESTABLE VALUES (2, 'YOKO', '2010-07-07') INSERT INTO @TESTABLE VALUES (1, 'JOHN', '2009-06-06') I want to get, for each NAME, the ID that has the most recent DATE. Like this: 3 JOHN 2011-08-08 2 YOKO 2010-07-07 What is the most elegant way of

MySQL group by and max returns wrong rows

被刻印的时光 ゝ 提交于 2019-12-08 16:27:31
问题 I have two tables and I try to find the "post" with the highest score per day. CREATE TABLE IF NOT EXISTS `posts_points` ( `post_id` int(10) unsigned NOT NULL, `comments` smallint(5) unsigned NOT NULL, `likes` smallint(5) unsigned NOT NULL, `favorites` smallint(5) unsigned NOT NULL, PRIMARY KEY (`post_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `posts` ( `profile_id` int(10) unsigned NOT NULL, `post_id` int(10) unsigned NOT NULL, `pubdate_utc` datetime NOT NULL,

Aggregating the most recent joined records per week

眉间皱痕 提交于 2019-12-08 15:40:23
问题 I have an updates table in Postgres is 9.4.5 like this: goal_id | created_at | status 1 | 2016-01-01 | green 1 | 2016-01-02 | red 2 | 2016-01-02 | amber And a goals table like this: id | company_id 1 | 1 2 | 2 I want to create a chart for each company that shows the state of all of their goals, per week. I image this would require to generate a series of the past 8 weeks, finding the most recent update for each goal that came before that week, then counting the different statuses of the found

SQL: how to limit a join on the first found row?

旧城冷巷雨未停 提交于 2019-12-08 14:55:50
问题 How to make a join between two tables but limiting to the first row that meets the join condition ? In this simple example, I would like to get for every row in table_A the first row from table_B that satisfies the condition : select table_A.id, table_A.name, table_B.city from table_A join table_B on table_A.id = table_B.id2 where .. table_A (id, name) 1, John 2, Marc table_B (id2, city) 1, New York 1, Toronto 2, Boston The output would be: 1, John, New York 2, Marc, Boston May be Oracle

MYSQL: Limiting rows per whereIn()

我与影子孤独终老i 提交于 2019-12-08 12:48:36
问题 users Table id user_comments Table id | user_id | content | created_at I have a list of user IDs, and I want to grab the latest 3 comments for each user id. SELECT * FROM user_comments WHERE user_id IN (1, 2, 3, 4, 5) ORDER BY created_at DESC LIMIT 3; This will grab the last 3 comments from all matching IDs, I want the last 3 comments for each ID. 1 query without unions preferred. I have tried right joining the table on itself but I cant seem to get it right. ** Edit: I cannot rely on the id

Select Top 5 records of every employee in SQL Server

孤者浪人 提交于 2019-12-08 10:42:24
问题 I have the following issue, I have this query that select the latest 5 records created for an employee: SELECT TOP 5 p.value, p.record_date AS FECHA FROM employee_loan_movements p WHERE p.employee_code = '1' AND p.record_date <= '2009-11-11' AND p.movement_type = 1 AND p.value > 0 ORDER BY p.record_date DESC Now i need to build a query to select the top 5 of every employee in the loan_movements table, I know i can do it in Oracle by selecting selecting rownum and rownum <= 5 but I cant manage

Running multiple queries in MySQL without using sub-query

↘锁芯ラ 提交于 2019-12-08 06:44:45
问题 I have two tables, one table called users with, fsname emailaddress and second table called attempts with emailaddress , score and datetime . Now what I wanted to do is first order the attempts table by datetime and then pick then join the attempt table with users table if they have same emailaddress and then pick the final attempts of each unique user. In short, I have to pick the last attempt of each user by joining these table and this is the query that I have generated to achieve this,