greatest-n-per-group

Get top 1 row of each group

可紊 提交于 2020-01-25 09:26:28
问题 I have a table which I want to get the latest entry for each group. Here's the table: DocumentStatusLogs Table |ID| DocumentID | Status | DateCreated | | 2| 1 | S1 | 7/29/2011 | | 3| 1 | S2 | 7/30/2011 | | 6| 1 | S1 | 8/02/2011 | | 1| 2 | S1 | 7/28/2011 | | 4| 2 | S2 | 7/30/2011 | | 5| 2 | S3 | 8/01/2011 | | 6| 3 | S1 | 8/02/2011 | The table will be grouped by DocumentID and sorted by DateCreated in descending order. For each DocumentID , I want to get the latest status. My preferred output:

Get latest entry based on date

百般思念 提交于 2020-01-25 07:51:27
问题 I want to get the info about users and the amount of points they had before a specific date. The problem I can't know when their state was last recorded so I have to get the latest entry before the date. The query I've ended up with is something like this (simplified example): SELECT u.id, p.points, p.timestamp FROM user AS u INNER JOIN points ON u.id = p.user_id WHERE u.group = 'red' AND p.timestamp::date <= '2018-01-01 10:00:00' GROUP BY u.id, u.first_name, u.last_name, mh.gamified_mastery,

SQL select only rows with max value on a column [duplicate]

狂风中的少年 提交于 2020-01-25 07:07:08
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. This question already has answers here : Retrieving the last record in each group - MySQL (27 answers) Closed 10 months ago . I have this table for documents (simplified version here): +------+-------+--------------------------------------+ | id | rev | content | +------+-------+---------------------

SQL select only rows with max value on a column [duplicate]

北慕城南 提交于 2020-01-25 07:05:23
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. This question already has answers here : Retrieving the last record in each group - MySQL (27 answers) Closed 10 months ago . I have this table for documents (simplified version here): +------+-------+--------------------------------------+ | id | rev | content | +------+-------+---------------------

Get last row PER Group

爱⌒轻易说出口 提交于 2020-01-25 06:45:10
问题 How can I formulate a query for the below task: Let's say you are logged in as user:1 I want to get one row per conversations I've had. For each row I want to get, the "Subject" of the first row within the conversation "DateTime" of the first row "Message" last message of this conversation no matter who wrote it CREATE TABLE messages ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, FromID INT NOT NULL, ToID INT NOT NULL, ConversationID INT NOT NULL, Subject varchar(255), Message varchar(255),

Pandas groupby with identification of an element with max value in another column

旧街凉风 提交于 2020-01-25 03:57:04
问题 I have a dataframe with sales results of items with different pricing rules: import pandas as pd from datetime import timedelta df_1 = pd.DataFrame() df_2 = pd.DataFrame() df_3 = pd.DataFrame() # Create datetimes and data df_1['item'] = [1, 1, 2, 2, 2] df_1['date'] = pd.date_range('1/1/2018', periods=5, freq='D') df_1['price_rule'] = ['a', 'b', 'a', 'b', 'b'] df_1['sales']= [2, 4, 1, 5, 7] df_1['clicks']= [7, 8, 9, 10, 11] df_2['item'] = [1, 1, 2, 2, 2] df_2['date'] = pd.date_range('1/1/2018'

Use table column value for LIMIT when performing join

丶灬走出姿态 提交于 2020-01-24 23:06:35
问题 I have a situation where I'm performing a join between two tables, and I need a value from one table to be used as a LIMIT factor for a subquery in the join. Assume I have the following [extremely simplified] tables - data: experiment_id | value --------------|-------- 1 | 2.5 1 | 2.6 1 | 4.5 1 | 2.3 1 | 3.5 1 | 2.8 2 | 2.3 2 | 1.2 2 | 1.1 2 | 3.6 2 | 3.8 2 | 4.1 2 | 7.9 2 | 4.2 2 | 1.0 data_clip: experiment_id | clip_index --------------|------------ 1 | 3 2 | 5 I need to sum each experiment

Use table column value for LIMIT when performing join

我的未来我决定 提交于 2020-01-24 23:04:37
问题 I have a situation where I'm performing a join between two tables, and I need a value from one table to be used as a LIMIT factor for a subquery in the join. Assume I have the following [extremely simplified] tables - data: experiment_id | value --------------|-------- 1 | 2.5 1 | 2.6 1 | 4.5 1 | 2.3 1 | 3.5 1 | 2.8 2 | 2.3 2 | 1.2 2 | 1.1 2 | 3.6 2 | 3.8 2 | 4.1 2 | 7.9 2 | 4.2 2 | 1.0 data_clip: experiment_id | clip_index --------------|------------ 1 | 3 2 | 5 I need to sum each experiment

Use table column value for LIMIT when performing join

醉酒当歌 提交于 2020-01-24 23:04:07
问题 I have a situation where I'm performing a join between two tables, and I need a value from one table to be used as a LIMIT factor for a subquery in the join. Assume I have the following [extremely simplified] tables - data: experiment_id | value --------------|-------- 1 | 2.5 1 | 2.6 1 | 4.5 1 | 2.3 1 | 3.5 1 | 2.8 2 | 2.3 2 | 1.2 2 | 1.1 2 | 3.6 2 | 3.8 2 | 4.1 2 | 7.9 2 | 4.2 2 | 1.0 data_clip: experiment_id | clip_index --------------|------------ 1 | 3 2 | 5 I need to sum each experiment

Messages: Query a user's chats

徘徊边缘 提交于 2020-01-24 01:42:06
问题 For an app like iOS Messages: How should I design the server database queries & schema? Queries Create a user. Get a user's chats. Get the messages between two users. Add a message to a chat (creating the chat if it doesn't yet exist). Note : Every chat only has two users (no group chats). Every chat's id equals this unique unordered pairing function applied to user_id & with_id . How should I query a user's chats, each with its latest message? Example User Interface (UI) See my design &