Re: [SQL ] 將值計算再新增到資料表

看板Database作者 (科科笑)時間11年前 (2014/07/02 15:03), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《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://www.ptt.cc/bbs/Database/M.1404284639.A.B34.html

07/02 15:13, , 1F
所以你不是要一個實體欄位去存放Expected_Count嗎?
07/02 15:13, 1F

07/02 15:14, , 2F
如果只是要顯示出來,直接將movie跟新table join即可
07/02 15:14, 2F
文章代碼(AID): #1JiwxViq (Database)
討論串 (同標題文章)
文章代碼(AID): #1JiwxViq (Database)