Then use a Window Function to order the fires by their cause and assign a rank in descending order.
Then use a CTE to isolate the Window Function results and return all rows where the rank = 1.
withrankingsas(selectfire_name,cause,disc_date,cont_date,state,acres,dense_rank()over(partitionbycauseorderbyacresdesc)asfire_rankfromday1.wildfireorderbycause,fire_rank)select*fromrankingswhererankings.fire_rank=1-- 20 seconds to execute
Then do some checks to make sure I've got the right results
That's an awesome solution, thanks for writing that. I don't think it's convoluted at all.
In my solution I went without window functions and did a join to a subquery. The interesting thing is that I could potentially join to the wrong row if there were more than 1 fire with the same cause and max acres burned, whereas here I think any ties would end up with the same rank, but it's guaranteed that there's no mixing and matching of data.
I hope you like the rest of the series! I started it because I was one of those developers that thought they knew SQL, when really I knew SQL-92, and modern SQL has so much more to offer.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
What a great idea, thanks for setting this up :D
This may be a little convoluted but ....
I would check out how many causes there were first:
Then use a Window Function to order the fires by their cause and assign a rank in descending order.
Then use a CTE to isolate the Window Function results and return all rows where the rank = 1.
Then do some checks to make sure I've got the right results
That's an awesome solution, thanks for writing that. I don't think it's convoluted at all.
In my solution I went without window functions and did a join to a subquery. The interesting thing is that I could potentially join to the wrong row if there were more than 1 fire with the same cause and max acres burned, whereas here I think any ties would end up with the same rank, but it's guaranteed that there's no mixing and matching of data.
I hope you like the rest of the series! I started it because I was one of those developers that thought they knew SQL, when really I knew SQL-92, and modern SQL has so much more to offer.