Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active August 30, 2020 17:49
Show Gist options
  • Save ShahOdin/a38142853fe189161ee548ae7e8009c3 to your computer and use it in GitHub Desktop.
Save ShahOdin/a38142853fe189161ee548ae7e8009c3 to your computer and use it in GitHub Desktop.
returns the full row for "highest" record of some sort
CREATE TABLE link (
ID serial PRIMARY KEY,
name VARCHAR (255) NOT NULL,
other VARCHAR (255) NOT NULL,
score INT NOT NULL
);
INSERT INTO link (name, other, score)
VALUES
('Foo', 'ABC' , 13),
('Foo', 'DEF', 14),
('Baz', 'JKL', 12),
('Baz', 'MNO', 10)
;
SELECT i1.name, i1.other, i1.score AS "MAX SCORE"
FROM link AS i1 LEFT JOIN link AS i2
ON (i1.name = i2.name AND i1.score < i2.score)
WHERE i2.score IS NULL
;
@ShahOdin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment