iT邦幫忙

0

SQL問題詢問

是這樣的,最近有看到一題跟SQL相關的題目,想請問答案是否為我所寫的這樣

以下是題目

https://ithelp.ithome.com.tw/upload/images/20181019/20110132wgQbqPEZ8p.jpg

這是我的答案

select Class,Name,SUM(Items) as total_tiems
from Table A
Left Join Table B
on Table A.Name=Table B.Name

想請問答案是否為我所寫的這樣,謝謝

小魚 iT邦大師 1 級 ‧ 2018-10-19 19:02:44 檢舉
要知道答案對不對建測試資料出來測試就好了,
電腦說的永遠都比較準...
這考的是sum, group by,把Group by 搞懂應該就好了。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
paicheng0111
iT邦大師 5 級 ‧ 2018-10-19 15:22:20
最佳解答
select 
    [Table A].Class
    , [Table A].Name
    ,SUM(Items) as total_tiems
from [Table A]
Left Join [Table B]
on [Table A].Name=[Table B].Name
GROUP BY [Table A].Class, [Table A].Name
3
純真的人
iT邦大師 1 級 ‧ 2018-10-19 15:11:32

你少 group by Class,A.Name
還有left join的別名只有A和B沒有多Table的字

select Class,A.Name,SUM(Items) as total_tiems
from Table A
Left Join Table B on A.Name = B.Name
group by Class,A.Name

除非是這樣的..表格名有空白

select Class,[Table A].Name,SUM(Items) as total_tiems
from [Table A]
Left Join [Table B] on [Table A].Name = [Table B].Name
group by Class,[Table A].Name
0
Ryan
iT邦新手 4 級 ‧ 2018-10-19 16:52:06

SUM函數後面要寫GROUP BY 欄位名,才知道要用什麼欄位來統計

SELECT A.Class, A.Name, SUM(B.Items) as total_tiems
FROM Table A
LEFT JOIN Table B
ON A.Name = B.Name
GROUP BY A.Class, A.Name

我要發表回答

立即登入回答