iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
自我挑戰組

Oracle資料庫系列 第 16

[Day16]彙總函數實作

  1. 在OE帳戶的orders_item資料表中,找出單價最高的產品。
SELECT MAX(unit_price)FROM order_items;

https://ithelp.ithome.com.tw/upload/images/20211001/201409158V4DLfYktx.png

  1. 在HR帳戶的employees資料表中,查詢各部門員工的平均薪水(顯示至小數一位數,其餘四捨五入)。輸出結果依序顯示部門代碼和平均薪水,並降冪排序部門平均薪水。
SELECT department_id"部門代號", ROUND(AVG(salary),1)"平均薪水"
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC;

https://ithelp.ithome.com.tw/upload/images/20211001/201409156OJeKDxHkc.png

  1. 在OE的orders_items資料表中,查詢每張訂單中的最高訂購金額。
SELECT order_id, MAX(unit_price*quantity)"最高訂購金額"
FROM order_items
GROUP BY order_id;

https://ithelp.ithome.com.tw/upload/images/20211001/20140915SRUv7QnQ6c.png

  1. 在HR中的employees和department資料表中,將部門代號30以後的資料依照代碼排序,並顯示每一個部門每個職員每個月的薪水總額。
SELECT department_id, job_id, SUM(salary)
FROM employees
WHERE department_id>30
GROUP BY department_id, job_id
ORDER BY department_id;

https://ithelp.ithome.com.tw/upload/images/20211001/20140915xnmd4WQVLj.png

  1. 在OE中的product_information和inventroies資料表中,查詢product_information表中,product_status為可被訂購的quantity_on_hand(是的一個欄位)。
    說明:
    1.product_information:product_id, product_information
    inventories:product_id, quantity_on_hand
    2.因為產品可被訂購的在手量是在inventroies裡,所以和product_information資料表合併時,需使用左外部合併。
    3.在SELECT子句內,彙總函數以外的欄位就是分組欄位,所以最後面加上:GROUP BY pi.product_id, pi.product_status。
SELECT pi.product_id, pi.product_status, sum(i.quantity_on_hand)"在手量"
FROM product_information pi
LEFT OUTER JOIN inventories i
ON (pi.product_id=i.product_id)
WHERE(pi.product_status='orderable')
GROUP BY pi.product_id, pi.product_status;

https://ithelp.ithome.com.tw/upload/images/20211001/20140915zalmJRrMMn.png


上一篇
[Day15]彙總函數(分組函數)
下一篇
[Day17]非相關子查詢
系列文
Oracle資料庫30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言