作者KerKerLaugh (科科笑)
看板Database
标题Re: [SQL ] 将值计算再新增到资料表
时间Wed Jul 2 15:03:55 2014
※ 引述《KerKerLaugh (科科笑)》之铭言:
: 取likes栏位数字的第一个字元,分组计算1~9各有几笔
: SELECT substring( likes, 1, 1 ) as Leading_Digits, Count( * ) as Actual_Count
: FROM movies
: GROUP BY substring( likes, 1, 1 )
: 结果
: Leading_Digits Actual_Count
: 1 30
: 2 12
: 3 10
: 4 13
: 5 8
: 6 4
: 7 8
: 8 10
: 9 6
: 总笔数是101笔
: 我想在Actual_Count右边再新增一栏名为Expected_Count
: Expected_Count 9行的值为101 * 0.301
: 101 * 0.176
: 101 * 0.125
: 101 * 0.097
: 101 * 0.079
: 101 * 0.067
: 101 * 0.058
: 101 * 0.051
: 101 * 0.046 的结果
: (变数) (固定值)
: 请问这要怎麽新增
: 另有一张test的资料表包含
: Leading_Digits Probability
: 1 0.301
: 2 0.176
: 3 0.123
: 4 0.097
: 5 0.079
: 6 0.067
: 7 0.058
: 8 0.051
: 9 0.046
我要的结果是这样
Leading_Digits Actual_Count Expected_Count
1 30
2 12
3 10
4 13
5 8
6 4
7 8
8 10
9 6
用查询我只能作出前两列
第3列要显示的是 (资料表的总笔数)乘以(上面test资料表的9个Probability)
不知道有没有办法一次写出这样的语法
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.123.176.231
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Database/M.1404284639.A.B34.html
1F:→ bohei:所以你不是要一个实体栏位去存放Expected_Count吗? 07/02 15:13
2F:→ bohei:如果只是要显示出来,直接将movie跟新table join即可 07/02 15:14