View Single Post
Old 01-18-2024, 07:25 PM   #17
Syd Thrift
Hall Of Famer
 
Syd Thrift's Avatar
 
Join Date: May 2004
Posts: 10,611
Here's the new version of the average that lops the leading 0 off:

SUBSTR(CAST(round(bat.h / bat.ab, 3) AS CHAR(5)), 2) as avg

One thing I haaaaaaaaaate about SQL scripting is that it's next to impossible to debug. And to be fair what's going on in this line would be a pain to debug in any language. I recommend writing these kinds of things out step by step, insuring you have the result you're looking for inside-out each step of the way. Going from inside out then...

1. Divide hits by at-bats. This gives you a number that could very well be .235737532987593120578375239 or something huge like that.

2. Round off to the 3rd digit. it sure looks to me like SQL exports all 3 decimals even when it doesn't have to; a .300 doesn't export as .3 for example.

3. Cast this number as a 5-unit character. Including the decimal, BA will always be 5 characters long.

4. Get the substring of this, beginning with character number... 2. As a developer I tend to think about strings in terms of them being an array of characters and, since arrays are 0-based, you'd start with position 1. But nope, MySQL counts from 1, unlike, like, all other programming languages on the planet.
__________________
Quote:
Originally Posted by Markus Heinsohn
You bastard....
The Great American Baseball Thrift Book - Like reading the Sporting News from back in the day, only with fake players. REAL LIFE DRAMA THOUGH maybe not

Last edited by Syd Thrift; 01-18-2024 at 07:31 PM.
Syd Thrift is offline   Reply With Quote