Daily SQL Quiz
Posted in 1993
And now for today's SQL pop quiz: Suppose we have a table that contains a composite primary key on columns task_id and instance_date. Now suppose that the table is populated with lots of data and we want to get a listing of all tasks of a certain type, but only the latest date for each type. Easy, right? Sure. select max(instance_date) from table group by task_type. Gee, that was easy. All we had to do was group by the max() column (otherwise we'd get only one row back). Now suppose there's another column in the table that we want to select. If it has differing values, we have a problem. This is because we'll get a row back for every differnt value in that other column. If we are obstinate and refuse to use a programming language (other than SQL) or a temporary table, can we still get the result we want? Prizes (if any) to be named later. Appreciation included now - thanks for your help!