Created
February 21, 2012 22:27
-
-
Save agborkowski/1879462 to your computer and use it in GitHub Desktop.
MySql: database values normalization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Database values normalization shows diferences betwen @OUTPUT_LOW - @OUTPUT_HIGH | |
* its sample for forex database with tips table and 5 colls | |
* open FLOAT(8,5), high FLOAT(8,5), low FLOAT(8,5), close FLOAT(8,5), | |
* volume TINYINT(4), symbol (ENUM), time (DATETIME). | |
* tested on MySql | |
* | |
* @author AgBorkowski [email protected] | |
* @uses http://www.heatonresearch.com/content/really-simple-introduction-normalization (java) | |
* @link http://blog.aeonmedia.eu/2011/02/mysql-database-values-normalization | |
*/ | |
/** | |
* Important params for charset equal with your table encoding | |
*/ | |
SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'; | |
/** | |
* The low range of the input (un-normalized data) | |
*/ | |
SET @INPUT_LOW := 0; | |
/** | |
* The high range of the input (un-normalized data) | |
*/ | |
SET @INPUT_HIGH := 0; | |
/** | |
* The low range of the output (normalized data) | |
*/ | |
SET @OUTPUT_LOW := 0; | |
/** | |
* The high range of the output (normalized data) | |
*/ | |
SET @OUTPUT_HIGH := 1; | |
/** | |
* The symbol of stock | |
*/ | |
SET @SYMBOL := 'EURUSD'; | |
/** | |
* Set @INPUT_LOW and @INPUT_HIGH | |
*/ | |
SELECT @INPUT_LOW := MIN(`low`), @INPUT_HIGH := MAX(`high`) | |
FROM forex.tips | |
WHERE symbol = @SYMBOL | |
GROUP BY symbol | |
SELECT | |
((`open` - @INPUT_LOW) / (@INPUT_HIGH - @INPUT_LOW)) * (@OUTPUT_HIGH - @OUTPUT_LOW) + @OUTPUT_LOW AS `open`, | |
((`high` - @INPUT_LOW) / (@INPUT_HIGH - @INPUT_LOW)) *(@OUTPUT_HIGH - @OUTPUT_LOW) + @OUTPUT_LOW AS `high`, | |
((`low` - @INPUT_LOW) / (@INPUT_HIGH - @INPUT_LOW)) * (@OUTPUT_HIGH - @OUTPUT_LOW) + @OUTPUT_LOW AS `low`, | |
((`close` - @INPUT_LOW) / (@INPUT_HIGH - @INPUT_LOW)) * (@OUTPUT_HIGH - @OUTPUT_LOW)+ @OUTPUT_LOW AS `close`, | |
volume | |
FROM forex.tips | |
WHERE symbol = @SYMBOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment