Selecting a value from a row where another column is max
0
votes
1
answer
307
views
I have the following SQL query:
SELECT bug.id
,
Max(report.date
),
Count(report.id
),
Max(version.code
),
bug.id
FROM bug
LEFT OUTER JOIN stacktrace
ON ( stacktrace.bug_id
= bug.id
)
CROSS JOIN version
LEFT OUTER JOIN report
ON ( report.stacktrace_id
= stacktrace.id
)
WHERE stacktrace.version_id
= version.id
GROUP BY bug.id
ORDER BY Max(report.date
) DESC;
I now want to select version.name
instead of version.code
from the row where version.code
is maximal. Is this possible? If so, how do I do this with minimal amount of queries/overhead?
---
Relevant Tables (stripped):
CREATE TABLE bug
(
id
int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id
)
);
INSERT INTO bug
VALUES();
INSERT INTO bug
VALUES();
CREATE TABLE version
(
id
int(11) NOT NULL AUTO_INCREMENT,
code
int(11) NOT NULL,
name
varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (id
)
);
CREATE TABLE stacktrace
(
id
int(11) NOT NULL AUTO_INCREMENT,
bug_id
int(11) NOT NULL,
version_id
int(11) NOT NULL,
PRIMARY KEY (id
),
KEY FK_s_v
(version_id
),
KEY FK_s_b
(bug_id
),
CONSTRAINT FK_s_b
FOREIGN KEY (bug_id
) REFERENCES bug
(id
) ON DELETE CASCADE,
CONSTRAINT FK_s_v
FOREIGN KEY (version_id
) REFERENCES version
(id
) ON DELETE CASCADE
);
CREATE TABLE report
(
id
varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
date
datetime NOT NULL,
stacktrace_id
int(11) NOT NULL,
PRIMARY KEY (id
),
KEY FK_r_s
(stacktrace_id
),
CONSTRAINT FK_r_s
FOREIGN KEY (stacktrace_id
) REFERENCES stacktrace
(id
) ON DELETE CASCADE
);
As SQLFiddle
Asked by F43nd1r
(101 rep)
Oct 20, 2020, 02:27 PM
Last activity: May 14, 2025, 05:10 PM
Last activity: May 14, 2025, 05:10 PM