First, create a new output field
1, construction of the table, insert data
### the CREATE TABLE `t_stock_trans_dtl` ( ` trans_id` VARCHAR ( 100) the NOT NULL the COMMENT 'transaction serial number' , `stock_name` VARCHAR ( 20) the NOT NULL the DEFAULT '' the COMMENT 'share name' , ` stock_code` VARCHAR ( 10) NOT NULL DEFAULT '' COMMENT 'ticker' , `opt_tm` datetime the NOT NULL the DEFAULT '1900-01-01 00:00:00' the COMMENT 'operating time' , ` opt_typ` VARCHAR ( 10) NOT NULL DEFAULT '' COMMENT ' operation type ' , `price` decimal ( 18,2) the NOT NULL the DEFAULT' 0.00 'the COMMENT' monovalent ' , ` volume` int (. 11) the NOT NULL the DEFAULT' 0 'the COMMENT' number ' , `fee` decimal (18,5) NOT NULL DEFAULT '0.00000' COMMENT ' fees' , a PRIMARY KEY ( `trans_id`) ) ENGINE = MyISAM the DEFAULT the CHARSET = GBK the COMMENT =' stock transaction details' ; ### the INSERT the INTO` t_stock_trans_dtl` the VALUES ( ' 20,010,406,000,023 ',' Hongdu aviation ',' sh600316 ',' 2001-04-06 10:42:34 ',' buy ', 22.40,600,3.36000 ), ( ' 20050826000303 ',' Hongdu aviation ',' sh600316 ',' 2005-08-26 13:40:31 ', ' sell ', 6.98, -300,5.00000 ), ( ' 20130301000933 ',' BYD ',' sz002594 ',' 2013-03-01 09: 33:36 ',' buy ', 25.25,600,3.78750 ), ( ' 20140307000393 ',' BYD ',' sz002594 ',' 2014-03-07 13:39:30 ',' Buy ', 56.91,600,8.53650 ), ('20150508000075', 'Hongdu Aviation', 'sh600316', '2015-05-08 10:47:54', ' sell', 36.66, -300,2.74950 ), ( '20150724000395', 'BYD' ' sz002594 ',' 2015-07-24 13:19:55 ', ' buy ', 55.55,1200,16.66500 ), ( ' 20150902000941 ',' Yu water ',' sz300021 ',' 2015-09-02 09:44:18 ',' Buy ', 11.41,6100,17.40025 ), ( ' 20160318000314 ',' Yu water ',' sz300021 ',' 2016-03-18 13:01:44 ',' sell a '12.62, -3000,9.46500 ), ( ' 20160930000303 ',' BYD ',' sz002594 ',' 2016-09-30 13:40:31 ',' sell ', 56.98, -2400,34.18800 ), ( '20170310000425', 'BYD', 'sz002594','2017-03-10 14:22:54', 'Buy', 51.59,2000,25.79500 ), ( '20170630000132', 'Yu water', 'sz300021', '2017-06-30 11:53 : 20 ',' buy ', 7.87,1300,2.55775 ), ( '20171110000142', 'Yu water', 'sz300021', '2017-11-10 11:44:24', ' Buy', 7.25,5000,9.06250 ), ( '20171229000410', 'BYD', 'sz002594', '2017-12-29 14:51:04' , ' buy', 61.88,3600,55.69200);
2, numerical calculation
For example: the number of units of shares database, how to convert hand (1 lot = 100 shares) after the output? TRANS_ID the SELECT, stock_name,. Price, Volume, Volume / 100 the FROM t_stock_trans_dtl; how the stock trading table unit price, quantity, calculate the amount of the transaction? TRANS_ID the SELECT, stock_name,. Price, Volume,. Price * Volume t_stock_trans_dtl the FROM; the SQL arithmetic operators: + addition - subtraction * multiplication / addition arithmetic operator precedence: and mathematical operation, as in the SELECT A -b * C / D. 1 + It is equivalent to the SELECT a - +1 ((b * C) / d) suggested () wrapped arithmetic operators results: field a (the INT) =. 1 field B (the INT) =. 4 the SELECT a/ b # returns 0.2500 , certain database also returns 0 how returns 0 25 ? A the SELECT * 1.00 / B arithmetic operators only support numeric field; how the character field arithmetic operations? The converted numeric character, recalculation
3, field splicing
For example: How to press the "Hongdu Aviation (sh600316)" shows the name and stock ticker symbol? Access, SQL Server in: the SELECT stock_name + '(' + stock_code + ')' the FROM t_stock_trans_dtl; the DB2, the Oracle, in the Teradata: the SELECT stock_name || '(' || || stock_code ')' the FROM t_stock_trans_dtl; MySQL in: the SELECT CONCAT (stock_name, '(', stock_code, ')' ) the FROM t_stock_trans_dtl; splicing operator only supports a character field; how splicing numeric field? Will be converted to numeric character, then splicing
4, using an alias
CONCAT the SELECT (stock_name, '(', stock_code, ')' ) the FROM t_stock_trans_dtl; Just check out the splicing field, header name is not easy to read, there may be an alias; used AS keyword expression an alias: the SELECT CONCAT (stock_name, '(', stock_code, ')' ) AS Stock the FROM t_stock_trans_dtl; the SELECT CONCAT (stock_name, '(', stock_code, ')') AS 'stock name and code' the FROM t_stock_trans_dtl; suggestion: whether Chinese alias are there spaces are enclosed in quotation marks
Second, create a new output field (case when)
1、case when
For example: how the number of buy, to distinguish between large single, single, small single? [ 3000, + ∞) large single ( 300,3000 ) single ( 0,300 ] Studio the SELECT Volume, the CASE the WHEN Volume > = 3000 THEN 'big one' the WHEN Volume > Volume 300 the AND <3000 THEN 'single' the WHEN Volume <= 300 THEN 'Studio' the END AS 'single type of' the FROM t_stock_trans_dtl the WHERE opt_typ = 'bUY' ; ## the else keyword the SELECT Volume, the CASE the WHEN Volume > = 3000 THEN 'big one' the WHEN Volume > the AND 300 volume <3000 THEN ''Small single' the END the FROM t_stock_trans_dtl the WHERE opt_typ = 'Buy';
2, CASE WHEN Syntax 1
### CASE WHEN Syntax. 1: the CASE expression 1 THEN the WHEN conditions resulting expression. 1 the WHEN 2 THEN result of the expression of the conditional expression 2 ... WHEN result of the expression of the conditional expression N N THEN [ELSE ELSE Results Expression] the END no ELSE partial, if all conditional expressions are not satisfied, it returns NULL. Each should have a CASE WHEN ELSE branch! ! ! For example: For purchase transactions, the number of transactions is a positive number, and sell transactions, the number of transactions is negative. This leads to the transaction amount (transaction price * number of transactions) Returns the value of both positive and negative. How do the conversion according to the type of transaction, the transaction amount of all returns a positive number? Buy trade price * number of transactions sold 0- trading price * number of transactions the SELECT opt_typ,. Price, Volume, the CASE the WHEN opt_typ = 'buy' THEN. Price * Volume the WHEN opt_typ = 'sell' THEN-0. Price * Volume ELSE 0 END FROM t_stock_trans_dtl; ##另一种写法 SELECT opt_typ,price,volume, CASE opt_typ WHEN '买入' THEN price*volume WHEN '卖出' THEN 0-price*volume ELSE 0 END FROM t_stock_trans_dtl;
3, CASE WHEN Syntax 2
### CASE WHEN Syntax 2:
the CASE conditional expression
WHEN expression results match expression. 1 1 THEN
WHEN 2 THEN results match expression Expression 2
...
WHEN resulting expression matching expression N THEN N
[expression the ELSE the ELSE Results formula]
the END
results are applicable to the conditional expression, it is an exhaustive list.
But not for the result of the conditional expression is a range of situations.
4, the nested CASE WHEN
CASE WHEN outer conditional expression 1 THEN (CASE WHEN inner layer of the inner layer of the conditional expression 1 THEN expression results. 1
...
the WHEN inner inner conditional expression result expression M M THEN
ELSE ELSE inner resulting expression
END )
...
the WHEN Conditional expression outer layer resulting expression N N THEN
ELSE ELSE outer expression results
END
two types of syntax can be nested, nested, but not recommended.
We recommend up to two nested parentheses format and use the SQL statement.