我有一个简单的表格,用于存储一堆股票的市值.我正在尝试查看是否可以创建将返回如下内容的查询:
cap_type type_count
micro 4
small 6
large 1
mega 2
我不确定如何调整此查询以计算不同选择案例组中的cap_types数量.这是我到目前为止的内容:
SELECT CASE
WHEN market_cap < 10 THEN 'micro'
WHEN market_cap >= 10 < 50 THEN 'small'
WHEN market_cap >= 50 < 100 THEN 'large'
ELSE 'mega'
END AS cap_type
FROM stocks
GROUP BY CASE
WHEN market_cap < 10 THEN 'micro'
WHEN market_cap >= 10 < 50 THEN 'small'
WHEN market_cap >= 50 < 100 THEN 'large'
ELSE 'mega'
END
ORDER BY cap_type ASC
以下是一些示例数据:
CREATE TABLE `stocks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`stock` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
`market_cap` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO temp (`stock`,`market_cap`) VALUES ('MSFT', 40);
INSERT INTO temp (`stock`,`market_cap`) VALUES ('MINI', 4);
INSERT INTO temp (`stock`,`market_cap`) VALUES ('GOOG', 50);
INSERT INTO temp (`stock`,`market_cap`) VALUES ('F', 90);