![]() | user_id | name | start_date | team_id | ranked | WHERE a.team_id = b.team_id AND a.user_id >= b.user_id Or, based on this Stack Overflow comment: ![]() ON a.team_id = b.team_id AND a.user_id >= b.user_id WHEN = start_date THEN Note that the assignment here will always be true | user_id | name | start_date | team_id | := 0 | rank |įirst employee by start date using by setting a variable | user_id | name | start_date | team_id | rank |īased on this Stack Overflow := + 1 AS rank Second employee by start date with ties broken by user id WHERE start_date = (SELECT MIN(start_date) FROM users) Ranked by start date with ties broken by user idįirst employee by start date with ties broken by user id | user_id | name | start_date | team_id | INSERT INTO users (name, start_date, team_id) VALUES ('Anne', '', 3) INSERT INTO users (name, start_date, team_id) VALUES ('Kathy', '', 3) INSERT INTO users (name, start_date, team_id) VALUES ('Bill', '', 3) INSERT INTO users (name, start_date, team_id) VALUES ('Bob', '', 3) ![]() INSERT INTO users (name, start_date, team_id) VALUES ('Tim', '', 3) INSERT INTO users (name, start_date, team_id) VALUES ('Sara', '', 2) INSERT INTO users (name, start_date, team_id) VALUES ('John', '', 2) INSERT INTO users (name, start_date, team_id) VALUES ('Matt', '', 1) If there are any other methods worth including here, please drop a note in the comments.ĬREATE TABLE users (user_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), start_date DATE, team_id INT) In order to understand the pros and cons of each technique, I created some test data and reimplemented the solutions, all of which I’ve shared below. While trying to wrangle a MySQL query to rank a set of results, I got sucked into a few Stack Overflow threads outlining various approaches. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |