greatest-n-per-group

How to get the last record per group in SQL

最后都变了- 提交于 2019-12-17 03:20:07
问题 I am facing a rather interesting problem. I have a table with the following structure: CREATE TABLE [dbo].[Event] ( Id int IDENTITY(1,1) NOT NULL, ApplicationId nvarchar(32) NOT NULL, Name nvarchar(128) NOT NULL, Description nvarchar(256) NULL, Date nvarchar(16) NOT NULL, Time nvarchar(16) NOT NULL, EventType nvarchar(16) NOT NULL, CONSTRAINT Event_PK PRIMARY KEY CLUSTERED ( Id ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS

Using a limit on a left join in mysql

时光怂恿深爱的人放手 提交于 2019-12-14 03:43:25
问题 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

How do I write a join with this unusual matching criteria?

时光总嘲笑我的痴心妄想 提交于 2019-12-14 00:16:28
问题 I want to "left join" a table so that a value is joined not just to a matching row, but also to any subsequent non-matching rows, up to the next matching row. To put it another way, I want to fill in nulls with the previous non-null value. Sample data and desired result: Table x : id ---- 1 2 3 4 5 Table y : id | val ----+----- 1 | a 4 | b Result of select x.id, y.val from x left join y on x.id=y.id order by x.id; : id | val ----+----- 1 | a 2 | 3 | 4 | b 5 | Desired result: id | val ----+---

Mysql Performance Query

…衆ロ難τιáo~ 提交于 2019-12-13 17:46:32
问题 First of all thank you for taking time to read this. I'm trying to answer a few questions about mysql and I'm stuck on a fews and that's why I resolve to ask for somebody help if possible. Or at least to point me in the right direction. Any help will be very appreciated. City table contains population data for 4000 cities across 230 countries. Explain why the following query performs poorly in MySQL 5.6, and demonstrate solutions. SELECT `ID`, `CountryCode`, `Name`, `District`, `Population`

Selecting most recent as part of group by (or other solution …)

时间秒杀一切 提交于 2019-12-13 14:28:46
问题 I've got a table where the columns that matter look like this: username source description My goal is to get the 10 most recent records where a user/source combination is unique. From the following data: 1 katie facebook loved it! 2 katie facebook it could have been better. 3 tom twitter less then 140 4 katie twitter Wowzers! The query should return records 2,3 and 4 (assume higher IDs are more recent - the actual table uses a timestamp column). My current solution 'works' but requires 1

Doctrine DQL greatest-n-per-group

与世无争的帅哥 提交于 2019-12-13 14:19:36
问题 Here are 2 tables of my Symfony2 project : +-----------+ +----------------------------+ | EVENT | | PHOTO | +-----------+ +------+-----------+---------+ | id | | id | event_id | likes | +-----------+ +------+-----------+---------+ | 1 | | 1 | 1 | 90 | | 2 | | 2 | 1 | 50 | +-----------+ | 3 | 2 | 20 | | 4 | 2 | 10 | +------+-----------+---------+ I would like to select the 2 events with its most liked photo, which would look like : +------------+------------+---------+ | event_id | photo_id |

First two salaries in each department [duplicate]

十年热恋 提交于 2019-12-13 14:18:47
问题 This question already has answers here : Get top results for each group (in Oracle) (5 answers) Closed last year . I have a table called departments which consists of a column called salary. Now I want to have all the details of the highest two salaries in each department. How should we develop a query which yields the required output? Top-N analysis will give it as a whole but not for each department. I want the top two in each department. 回答1: I realize you asked for this in Oracle. I can't

Get latest dates from an array per object group in Powershell

左心房为你撑大大i 提交于 2019-12-13 14:12:52
问题 I have an array $data : PS> $data Datum User Computer ----- --------- -------- 10/06/2013 13:10:56 geb1@TESTDOMAIN.COM DC1$ 10/06/2013 13:09:25 geb2@TESTDOMAIN.COM DC2$ 10/06/2013 10:05:13 geb2@TESTDOMAIN.COM DC2$ 7/06/2013 16:32:47 geb1@TESTDOMAIN.COM DC1$ I want to get the latest dates from the $data array for each computer like this: PS> $result Datum User Computer ----- --------- -------- 10/06/2013 13:10:56 geb1@TESTDOMAIN.COM DC1$ 10/06/2013 13:09:25 geb2@TESTDOMAIN.COM DC2$ I really

SQLite: How to SELECT “most recent record for each user” from single table with composite key?

半世苍凉 提交于 2019-12-13 12:29:30
问题 I'm not a database guru and feel like I'm missing some core SQL knowledge to grok a solution to this problem. Here's the situation as briefly as I can explain it. Context: I have a SQLite database table that contains timestamped user event records. The records can be uniquely identified by the combination of timestamp and user ID (i.e., when the event took place and who the event is about). I understand this situation is called a "composite primary key." The table looks something like this

Resulting data on last thread_date

醉酒当歌 提交于 2019-12-13 11:07:04
问题 As I fixed the previous question thanks to the answer, I am sticking now in the part to retrieve a thread based on the last thread_date. The code itself seems to be working fine, but it is only printing one result out instead of others. The thread has a threads.cat_id which is linked to thesubsubcategory.extra_cat_id. SELECT parent.subcat_id, parent.subcat_name, child.subsubcat_name, child.subcat_id, child.cat_id, kid.thread_name, kid.cat_id, kid.thread_date FROM subcategories parent JOIN