I have two tables T1 and T2. There is a 1:n relationship between T1 and T2.
CREATE TABLE T1 (
PK INT
/* more columns here */
);
CREATE TABLE T2 (
PK INT,
T1_PK INT,
Active INT,
Rank INT
/* more columns here */
);
INSERT INTO T1 VALUES (1);
INSERT INTO T1 VALUES (2);
INSERT INTO T1 VALUES (3);
INSERT INTO T2 VALUES (1, 1, 1, 20);
INSERT INTO T2 VALUES (2, 1, 0, 15);
INSERT INTO T2 VALUES (3, 1, 0, 10);
INSERT INTO T2 VALUES (4, 1, 0, 5);
INSERT INTO T2 VALUES (5, 2, 1, 16);
INSERT INTO T2 VALUES (6, 2, 0, 11);
INSERT INTO T2 VALUES (7, 2, 0, 6);
INSERT INTO T2 VALUES (8, 3, 1, 7);
So each row in T1 has:
– 1 corresponding row in T2 with Active = 1
– 0 to n corresponding rows in T2 with Active = 0
The Rank column in T2 holds values for end-user sorting in the application's UI.
This is the simple query I currently have:
SELECT T1.PK, T2.Active, T2.Rank
FROM T1
LEFT JOIN T2 ON T1.PK = T2.T1_PK
WHERE T2.Active = 1
It returns this result set:
PK Active Rank
1 1 20
2 1 16
3 1 7
So that works fine.
Now I would like to get the row values with Active = 0 and MIN(Rank) in the same result set:
PK Active Rank Active Rank
1 1 20 0 5
2 1 16 0 6
3 1 7 null null
I played around with a second JOIN, with a subquery, with GROUP BY and HAVING, but to no avail.
Note that the above code pieces can be copied to http://sqlfiddle.com and will work.