简体   繁体   中英

WHERE-clause for last non-null value

I'm making a community database for a school project and have ran into an issue. I am attempting to integrate a log system but retrieve the latest non-null value from column in a table called logs and present that information on a different page. My current code (without any attempt at filtering by their rank) looks as follows:

SELECT m.MemberID, m.MemberName, o.OfficeID, o.OfficeDesignation, p.PositionAbbreviation, l.LogRank
FROM logs l
INNER JOIN (SELECT l.LogMember, MAX(l.LogDate) AS maxLogDate FROM logs l GROUP BY l.LogMember) l2
    ON (l.LogMember = l2.LogMember AND l.LogDate = l2.maxLogDate)
INNER JOIN members m
    ON (l.LogMember = m.MemberID)
INNER JOIN offices o
    ON (l.LogOffice = o.OfficeID)
INNER JOIN positions p
    ON (l.LogPosition = p.PositionID)
GROUP BY m.MemberID;

The above query returns the latest entry for each member in the logs table, but I can't figure out how to, when l.LogRank returns NULL, take the latest non-null value only for that column.

I have struggled with a variety of approaches to this problem over the past week to no avail. Any help/pointers would be appreciated.

EDIT: Sample data seen below:

+-------+-----------+---------+-----------+-------------+-----------+
| LogID | LogMember | LogRank | LogOffice | LogPosition |  LogDate  |
+-------+-----------+---------+-----------+-------------+-----------+
|   1   |     1     |    1    |     7     |      5      | TIMESTAMP |
+-------+-----------+---------+-----------+-------------+-----------+
|   2   |     1     |    1    |           |      1      | TIMESTAMP |
+-------+-----------+---------+-----------+-------------+-----------+
|   3   |     1     |         |     1     |             | TIMESTAMP |
+-------+-----------+---------+-----------+-------------+-----------+

The various INT values reference IDs in the relevant other tables.

Desired Output:

+-------+-----------+---------+-------------------+-----------------+-----------+
| LogID | LogMember | LogRank | OfficeDesignation | PositionAbbrev. |  LogDate  |
+-------+-----------+---------+-------------------+-----------------+-----------+
|   1   |     1     |    1    |        C-6        |        CO       | TIMESTAMP |
+-------+-----------+---------+-------------------+-----------------+-----------+

So basically I want to retrieve the MemberID, MemberName, OfficeID, OfficeDesignation, PositionAbbreviation, and LogRank from a variety of tables and get the latest non-null record for each column.

INNER JOIN (
    SELECT l.LogMember, l.LogRank FROM logs l
    WHERE l.LogDate=(
        SELECT LogDate FROM logs
        WHERE LogMember=l.LogMember
          AND LogRank IS NOT NULL
        ORDER BY LogDate DESC
        LIMIT 1
        )
    ) l3
    ON (l.LogMember = l3.LogMember)

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