last day (19 days later) » 

9:55 AM
1
A: Fetch a row that contains the set of last non-NULL values for each column

Vérace - get VACCINATED NOWYou can do the following (all of the code below is available on the fiddle here): CREATE TABLE tab ( version INT PRIMARY KEY, col_A TEXT, col_B TEXT, col_C TEXT ); Data: INSERT INTO tab (version, col_A, col_B, col_C) VALUES (1, 'A1', 'B1', null), (2, 'A2', 'B3', null), (3, 'A3', 'B2', nu...

 
"max" is not always "last", is it? And even if it is, why can't you aggregate in a single select?
 
@mustaccio - surely in this context "last" means A5 for col_A &c. - the OP said as much in his edit. I value your erudite contributions on this site, so perhaps you'd be so kind as to explain exactly what you're getting at? I believe that I've answered the question as asked - of course, this is not always a guarantee that it's what the OP wanted in the first place :-) - but I checked the OP's profile and they have a hefty rep. on the maths site, leading me to believe that they are able to correctly formulate questions in accordance with their requirements!
 
To mustaccio's programmatic point, any reason SELECT MAX(col_A) AS a, MAX(col_B) AS b, MAX(col_C) AS c FROM tab doesn't also solve the problem (under the same assumptions OP correctly provided what they need) with potentially less passes over the table, so possibly more efficiently too? If so, feel free to add it to your answer if you'd like. 🙂
 
@J.D. Yes, with a COALESCE that works. I went down a rabbit hole! :-) Put it in as the answer!
 
@Vérace-getVACCINATEDNOW: Thanks for posting a quick answer and fiddle code. I am not very familiar with how we post things here as well as the dba terminologies and nitty-gritties -- my apologies for that. As mustaccio pointed out, in my question "max" doesn't always mean "last" - it just happens to be the case in the example I provided. As I mentioned in my EDIT - the last means row with the highest "version". So for e.g. if the row for version 5 would have been (A1, (null), (null)), then the expected out would have been (A1, B3, C1) while your query would still produce (A5, B3, C1).
 
9:55 AM
I'm not one for stealing credit, especially on such a simple problem that you already provided a fine answer for. It's all yours for the taking. 🙂👍
 
The question has been edited with more detailed explanation.
 
@J.D. - it wouldn't be stealing credit - yours is a completely different answer - and the correct approach to boot. Anyway, I put it in and mentioned your name! Thanks for your input!
 
@J.D. I can clearly see A5 in col_A
 
Oh, my previous edit was overwritten. I think the latest should be more clear for all.
 
@Aang Sorry I misread your comment, specifically I missed where you said "row for version 5" and thought you were saying the entire table only had A1 in it. I understand now, your sorting logic is based on the version column. That is a more complicated problem to be solved indeed. Thanks for the updated information.
 
9:55 AM
This answer is wrong.
 
@TheImpaler - thanks for the input Vlad - I know it's wrong! I even said as much within the answer itself. I'm leaving it as a "relic", so that future aspiring SQL programmers will know how to avoid the minefields that can be found in rabbit-holes! :-)
 
@TheImpaler the first version of the question was not clear so the answer was not wrong at that point.
 

  last day (19 days later) »