45. 德州扑克起手牌-同花
数据分析师玩德州扑克的同学应该不在少数。
涉及快速的概率计算、人性洞察、尔虞我诈,这不跟数据分析师工作很像吗哈哈?
现在就来通过起手牌来练习、巩固字符串相关函数吧!
SQL德州两不误,完美。
同花,如A♥️K♥、7♦️8♦、Q♠K♠
任务:找到所有手对组合(注意是组合,不是排列),并计算概率。
考点
left 、right、like、substr都可以
hand_permutations,
起手牌的排列组合表
id | card1 | card2 |
---|---|---|
1 | 2♠ | 3♠ |
2 | 2♠ | 4♠ |
…… | …… | …… |
2648 | A♣ | 9♣ |
2649 | A♣ | 10♣ |
2650 | A♣ | J♣ |
2651 | A♣ | Q♣ |
2652 | A♣ | K♣ |
小数点后保留3位。
cnt | ttl_cnt | p |
---|---|---|
312 | 1326 | 0.235 |
点击下方空白区域即可查看参考答案
select
sum(case when right(card1,1)=right(card2,1) then 1 else 0 end)/2 as cnt /*同花组合数*/
, count(1)/2 as ttl_cnt /*总组合数*/
,cast(sum(case when right(card1,1)=right(card2,1) then 1 else 0 end)/count(1) AS DECIMAL(4,3)) as p /*概率*/
from hand_permutations
MySQL 8.0
xxxxxxxxxx
2
select * from hand_permutations limit 5;