iT邦幫忙

0

SQL語法如何橫向加總?

https://ithelp.ithome.com.tw/upload/images/20181109/20106936LBrsqYIySD.png

如圖,每個學生有三個成績,我要如何在查詢之後,多出個SUM欄位並且自動加總三個成績呢?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

5
allenlwh
iT邦高手 1 級 ‧ 2018-11-09 10:21:13
最佳解答
select student_id,student_name,chinese,english,math, 
    (chinese+english+math) as total 
    from your_table
看更多先前的回應...收起先前的回應...

非常感謝你/images/emoticon/emoticon07.gif

石頭 iT邦高手 1 級 ‧ 2018-11-10 08:35:47 檢舉

備註 如果那三個欄位可為空
建議使用 coalesce(chinese,0)+coalesce(english,0)+ coalesce(math,0)
不然只要有一個欄是空值 計算結果就是空值

好的~非常感謝你~~

longan iT邦新手 5 級 ‧ 2019-04-13 15:43:25 檢舉

請問一下 該如何查詢學生的最高成績及科目呢?

我要發表回答

立即登入回答