greatest-n-per-group

PostgreSQL GROUP BY: SELECT column on MAX of another WHERE a third column = x

不羁的心 提交于 2019-12-10 17:52:35
问题 Let's suppose we have two tables in PostgreSQL: Table "citizens" country_ref citizen_name entry_date ----------------------------------------------------- 0 peter 2013-01-14 21:00:00.000 1 fernando 2013-01-14 20:00:00.000 0 robert 2013-01-14 19:00:00.000 3 albert 2013-01-14 18:00:00.000 2 esther 2013-01-14 17:00:00.000 1 juan 2013-01-14 16:00:00.000 3 egbert 2013-01-14 15:00:00.000 1 francisco 2013-01-14 14:00:00.000 3 adolph 2013-01-14 13:00:00.000 2 emilie 2013-01-14 12:00:00.000 2 jacques

MySQL Merge Row Values into single Row

爱⌒轻易说出口 提交于 2019-12-10 17:52:15
问题 I need to create a select query for a table which merges the rows per group on the 'Number' column to get a single row with all available yet latest (By ID) columns. Here's what I mean, I need to take a table like this: ID | Number | Date 1 | Date 2 | Date 3 | ---------------------------------------------------- 1 | 1 | 2011-10-01 | NULL | NULL | 2 | 1 | NULL | 2011-10-25 | NULL | 3 | 1 | NULL | NULL | 2011-11-13 | 4 | 1 | 2011-10-03 | NULL | 2011-11-10 | 5 | 2 | NULL | NULL | 2012-01-01 | 6

MySQL Selecting details using max

為{幸葍}努か 提交于 2019-12-10 17:47:42
问题 I have a set of MySQL tables containing information from a football management game. The tables are: Players - playerID (PK), playerName Matches - matchID (PK), matchSeason, matchRound, matchType PlayersMatch - playerID, matchID (comp PK), matchRating, playerForm, playerAge, position (can be null) Data stored in these tables are related to the performance of a player. A Player plays in a match and has a rated performance (matchRating). There is a record in PlayersMatch for each match a player

Migrating Raw SQL to Eloquent

删除回忆录丶 提交于 2019-12-10 15:25:10
问题 I am trying to migrate some Raw SQL to an Eloquent (or Query Builder) scope on my model. My Parts history table looks like this: +----+---------+--------+------------+ | id | part_id | status | created_at | +----+---------+--------+------------+ | 1 | 1 | 1 | ... | | 2 | 1 | 2 | ... | | 3 | 2 | 1 | ... | | 4 | 1 | 2 | ... | | 5 | 2 | 2 | ... | | 6 | 1 | 3 | ... | Notice the same part_id can have multiple entries where the status is the same. At the moment I use the following to select the

Find max value and show corresponding value from different field in MS Access

别等时光非礼了梦想. 提交于 2019-12-10 12:04:21
问题 So I found this similar question and answer at (Find max value and show corresponding value from different field in SQL server) but I want to take it one step further. I want to get the latest date for each ID and the corresponding TYPE instead of just the absolute max for all entries. Any suggestions? ID Type Date 1 Initial 1/5/15 1 Periodic 3/5/15 2 Initial 2/5/15 3 Initial 1/10/15 3 Periodic 3/6/15 4 Initial 3/8/15 The code below shows how to get just the max date of ALL entries, but I

Return first x results from each group in SQL query

此生再无相见时 提交于 2019-12-10 11:54:33
问题 I have a very complicated SQL query that returns a few hundred results broken down as follows: user 1 id: ## stuff user 1 id: ## stuff user 1 id: ## stuff .... user 2 id: ### stuff user 2 id: ### stuff user 2 id: ### stuff .... for about 6 users. It's already sorted exactly the way I want, I just want the first 5 for each user. Is there an easy way to do this? I'm using PostgreSQL 8.4 btw, and my SQL knowledge is limited so try to make any explanations not too complicated if possible :D 回答1:

How do I efficiently query versioned rows/entities in PostgreSQL?

只愿长相守 提交于 2019-12-10 11:44:22
问题 Background I have situation where I store all versions of a given entity in my PostgreSQL database. This is implemented with two tables; one table storing the primary key and immutable properties of the entity and a second table storing the mutable properties of the entity. Both tables are insert-only (enforced by a trigger). Example The concept can easily be illustrated with an entity User , stored in the user and user_details tables: Table user : id timestamp 1 2018-04-10T12:00:00 2 2018-04

what is the query to return Name and Salary of employee Having Max Salary

邮差的信 提交于 2019-12-10 11:05:23
问题 what is the query to return Name and Salary of employee Having Max Salary 回答1: SELECT Name, Salary FROM Minions WHERE Salary = (SELECT Max(Salary) FROM Minions) Note that this will return more than one row if there is more than one employee who have the same max salary 回答2: select name, salary from (select * from salary_table order by salary desc limit 1) 回答3: SELECT FirstName, max(salary) FROM Employees WHERE salary = ( SELECT max(salary) FROM employees ) GROUP BY FirstName working in SQL

What's the most efficient way to generate this report?

元气小坏坏 提交于 2019-12-10 10:57:57
问题 Given a table (daily_sales) with say 100k rows of the following data/columns: id rep sales date 1 a 123 12/15/2011 2 b 153 12/15/2011 3 a 11 12/14/2011 4 a 300 12/13/2011 5 a 120 12/12/2011 6 b 161 11/15/2011 7 a 3 11/14/2011 8 c 13 11/14/2011 9 c 44 11/13/2011 What would be the most efficient way to write a report (completely in SQL) showing the two most recent entries (rep, sales, date) for each name, so the output would be: a 123 12/15/2011 a 11 12/14/2011 b 153 12/15/2011 b 161 11/15/2011

Get records for each person's each day's min datetime

淺唱寂寞╮ 提交于 2019-12-10 10:16:25
问题 CREATE TABLE IF NOT EXISTS `accesscards` ( `id` int(11) NOT NULL AUTO_INCREMENT, `department` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `entrydates` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; INSERT INTO `accesscards` (`id`, `department`, `name`, `entrydates`) VALUES (1, 'test', 't1', '2013-12-06 16:10:00'), (2, 'test', 't1', '2013-12-06 15:10:00'), (3, 'test', 't1', '2013-12-07 15:11:00'), (4, 'test', 't1', '2013-12-07 15:24:00'