这是我的表结构及其下的数据
CREATE TABLE IF NOT EXISTS `nifty_banknifty` (
`instrument` varchar(100) DEFAULT NULL,
`symbol` varchar(100) DEFAULT NULL,
`expiry_date` varchar(100) DEFAULT NULL,
`strike_pr` varchar(100) DEFAULT NULL,
`option_typ` varchar(15) DEFAULT NULL,
`open` decimal(15,2) DEFAULT NULL,
`high` decimal(15,2) DEFAULT NULL,
`low` decimal(15,2) DEFAULT NULL,
`close` decimal(15,2) DEFAULT NULL,
`settle_pr` decimal(15,2) DEFAULT NULL,
`contracts` varchar(70) DEFAULT NULL,
`open_int` varchar(70) DEFAULT NULL,
`change_oi` varchar(70) DEFAULT NULL,
`date_time` date DEFAULT NULL
);
INSERT INTO `nifty_banknifty` (`instrument`, `symbol`, `expiry_date`, `strike_pr`, `option_typ`, `open`, `high`, `low`, `close`, `settle_pr`, `contracts`, `open_int`, `change_oi`, `date_time`) VALUES
('OPTIDX', 'NIFTY', '31-Mar-2016', '6000', 'CE', 1060.55, 1226.50, 1053.25, 1212.75, 1212.75, '684', '601650', '-1650', '2016-03-01'),
('OPTIDX', 'NIFTY', '31-Mar-2016', '7000', 'CE', 156.65, 298.90, 156.65, 289.70, 289.70, '50178', '2553975', '-163125', '2016-03-01'),
('OPTIDX', 'NIFTY', '31-Mar-2016', '6000', 'PE', 3.35, 5.20, 2.30, 2.70, 2.70, '11216', '1196350', '-2700', '2016-03-01'),
('OPTIDX', 'NIFTY', '31-Mar-2016', '7000', 'PE', 124.10, 131.75, 62.10, 66.85, 66.85, '269140', '6069025', '1134400', '2016-03-01');
现在每个执行价格分别有两个执行价格6000和7000以及相应的CE和PE open_int值
(以前不知道Stike价格(早先不知道6000,7000))
请您告诉我如何编写查询以分别为每个执行价格检索每个Stikre价格和CE和PE open_int值
例如输出必须是
Strike_Price CE PE
6000 601650 1196350
7000 2553975 6069025
这是我的小提琴
答案 0 :(得分:1)
您可以使用CASE WHEN... END
SELECT strike_pr ,
MAX(CASE WHEN option_typ = 'CE' THEN open_int END) AS CE ,
MAX(CASE WHEN option_typ = 'PE' THEN open_int END) AS PE
FROM nifty_banknifty GROUP BY strike_pr;