Friday, May 25, 2012

This is some tortured SQL. SELECT ( SELECT MIN(weight) FROM weight ) min , ( SELECT weight FROM w...



This is some tortured SQL.





SELECT

( SELECT MIN(weight) FROM weight ) min ,

( SELECT weight FROM weight WHERE id = ( SELECT MAX(id) FROM weight ) ) today ,

( SELECT COUNT(weight) FROM weight WHERE weight = ( SELECT MIN(weight) FROM weight ) ) count ;

I so rarely do sub-selects. Table has three values: id of the entry, weight and date, which is a standard timestamp. First subselect gets me the lowest weight value in the table, second one gets me what today's weight, and the third gets me how many days that's been that weight.



Yeah, I could've read in a data structure into $OB_FAVE_SCRIPTING_LANG, parsed it around, and worked out the same things, but it's faster to get that pre-cut. And with that, I can find days where min == today and count == 1, and if the booleans work out, I can automate a tweet that says "today, I am lighter than I have been in over a decade."



I know there's still a lot in SQL that I don't know, but I feel I've done something cool today.






No comments:

Post a Comment