简体   繁体   中英

SQL select another column with max during group by

I have a sqlite database, and I am executing this query.

SELECT
  thread.id as id,
  thread.title as title,
  post.posted as posted,
  user.username as username
FROM post
INNER JOIN thread ON post.thread_id = thread.id
INNER JOIN user ON post.user_id = user.id
ORDER BY posted desc;

This gives me the following rows

5|Creating Thread|2017-07-23 08:05:15.730725|zrbecker
5|Creating Thread|2017-07-23 08:05:07.881327|zrbecker
4|This is a new thread|2017-07-23 05:14:08.513643|zrbecker
4|This is a new thread|2017-07-23 05:13:40.172866|admin
1|First Thread!|2017-07-23 05:10:43.772543|zrbecker
3|Here is a post|2017-07-23 04:58:10.999243|ralph
3|Here is a post|2017-07-23 04:52:49.060482|admin
3|Here is a post|2017-07-23 04:52:30.497092|admin
3|Here is a post|2017-07-23 04:50:53.800177|admin
2|Another Thread|2017-07-23 02:21:46.544810|ralph
1|First Thread!|2017-07-23 02:17:46.544810|ralph
1|First Thread!|2017-07-23 02:16:46.544810|admin

I want to do a "group by" where I group by thread.id, I want a last_posted, and first_posted column for the dates. But I also want last_username, and first_username to know the user that posted that post.

This was my first attempt.

SELECT
  thread.id as id,
  thread.title as title,
  count(post.id) as number_posts,
  max(post.posted) as last_posted,
  user.username as last_username,
  min(post.posted) as first_posted,
  user.username as first_username
FROM post
INNER JOIN thread ON post.thread_id = thread.id
INNER JOIN user ON post.user_id = user.id
GROUP BY thread_id
ORDER BY last_posted desc;

There isn't really a good reason I should expect that to work. It's seems like I need a max function that outputs both columns. Not just the column I am taking the max of.

Any help?

EDIT: Here is a .dump of my sqlite3 database

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE thread (
        id INTEGER NOT NULL,
        title VARCHAR(80),
        PRIMARY KEY (id)
);
INSERT INTO thread VALUES(1,'First Thread!');
INSERT INTO thread VALUES(2,'Another Thread');
INSERT INTO thread VALUES(3,'Here is a post');
INSERT INTO thread VALUES(4,'This is a new thread');
INSERT INTO thread VALUES(5,'Creating Thread');
CREATE TABLE user (
        id INTEGER NOT NULL,
        username VARCHAR(30) NOT NULL,
        password VARCHAR(50) NOT NULL,
        PRIMARY KEY (id),
        UNIQUE (username)
);
INSERT INTO user VALUES(1,'admin','test123');
INSERT INTO user VALUES(2,'ralph','password123');
INSERT INTO user VALUES(3,'zrbecker','helloworld');
CREATE TABLE post (
        id INTEGER NOT NULL,
        user_id INTEGER,
        thread_id INTEGER,
        message TEXT NOT NULL,
        posted DATETIME,
        PRIMARY KEY (id),
        FOREIGN KEY(user_id) REFERENCES user (id),
        FOREIGN KEY(thread_id) REFERENCES thread (id)
);
INSERT INTO post VALUES(1,1,1,'First Post!','2017-07-23 02:16:46.544810');
INSERT INTO post VALUES(2,2,1,'Second post!','2017-07-23 02:17:46.544810');
INSERT INTO post VALUES(3,2,2,'Another post','2017-07-23 02:21:46.544810');
INSERT INTO post VALUES(4,1,3,'Lorem Ipsum ','2017-07-23 04:50:53.800177');
INSERT INTO post VALUES(5,1,3,'test test','2017-07-23 04:52:30.497092');
INSERT INTO post VALUES(6,1,3,'test trest test','2017-07-23 04:52:49.060482');
INSERT INTO post VALUES(7,2,3,'Hello There','2017-07-23 04:58:10.999243');
INSERT INTO post VALUES(8,3,1,'hello','2017-07-23 05:10:43.772543');
INSERT INTO post VALUES(9,1,4,'This is my message','2017-07-23 05:13:40.172866');
INSERT INTO post VALUES(10,3,4,'hello','2017-07-23 05:14:08.513643');
INSERT INTO post VALUES(11,3,5,'This is a thread','2017-07-23 08:05:07.881327');
INSERT INTO post VALUES(12,3,5,'New post','2017-07-23 08:05:15.730725');
COMMIT;

I ended up coming up with a SQL query that seems to work. Seems quite complicated though. Hoping for something much simpler.

Here is the query:

SELECT
  thread.id AS id,
  thread.title AS title,
  thread.post_count AS post_count,
  thread.last_posted AS last_posted,
  last_user.username AS last_username,
  thread.first_posted AS first_posted,
  first_user.username AS first_username
FROM (
  SELECT
    thread.id AS id,
    thread.title AS title,
    count(post.id) AS post_count,
    max(post.posted) AS last_posted,
    min(post.posted) AS first_posted
  FROM thread
  INNER JOIN post ON post.thread_id = thread.id
  GROUP BY thread.id
) AS thread
INNER JOIN post AS last_post
  ON last_post.thread_id = thread.id
  AND last_post.posted = thread.last_posted
INNER JOIN post AS first_post
  ON first_post.thread_id = thread.id
  AND first_post.posted = thread.first_posted
INNER JOIN user AS last_user ON last_post.user_id = last_user.id
INNER JOIN user AS first_user ON first_post.user_id = first_user.id
ORDER BY last_posted DESC;

Desired output looks like this:

5|Creating Thread|2|2017-07-23 08:05:15.730725|zrbecker|2017-07-23 08:05:07.881327|zrbecker
4|This is a new thread|2|2017-07-23 05:14:08.513643|zrbecker|2017-07-23 05:13:40.172866|admin
1|First Thread!|3|2017-07-23 05:10:43.772543|zrbecker|2017-07-23 02:16:46.544810|admin
3|Here is a post|4|2017-07-23 04:58:10.999243|ralph|2017-07-23 04:50:53.800177|admin
2|Another Thread|1|2017-07-23 02:21:46.544810|ralph|2017-07-23 02:21:46.544810|ralph

Another Edit: My ultimate goal is to craft a SQLAlchemy expression that will do this for me. When I couldn't figure out how to do it with the ORM, I decided to try to come up with a raw SQL query that could guide me. Any advice that would move me in that direction would be nice.

Thanks.

Try the query below

with postData as(
SELECT
post.*
,row_number() over (partition by  post.thread_id order by post.posted desc) descRowNumber
,row_number() over (partition by  post.thread_id order by post.posted ) ascRowNumber
FROM post
)
SELECT thread.id AS id,
thread.title AS title,
last_posted.posted AS last_postedDate,
last_user.username AS last_username,
first_posted.posted AS first_postedDate,
first_user.username AS first_username 
from
thread left join
  (select id,thread_id from postData where ascRowNumber=1) first_posted on first_posted.thread_id=thread.id
left join  
  (select id,thread_id from postData where descRowNumber=1) last_posted on last_posted.thread_id=thread.id
left JOIN [user] AS first_user ON first_posted.user_id = first_user.id
left JOIN [user] AS last_user ON last_posted.user_id = last_user.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM