MSSQL Over (Partition by ... Order by ...) 範例

建立Table

  1. CREATE TABLE sales (
  2. department VARCHAR(50),
  3. employee VARCHAR(50),
  4. sale_date DATE,
  5. amount DECIMAL(10, 2)
  6. );
  7.  
  8. INSERT INTO sales VALUES
  9. ('部門A', '員工1', '2023-01-01', 100.00),
  10. ('部門A', '員工1', '2023-01-02', 150.00),
  11. ('部門A', '員工2', '2023-01-01', 200.00),
  12. ('部門B', '員工3', '2023-01-01', 50.00),
  13. ('部門B', '員工3', '2023-01-02', 75.00),
  14. ('部門B', '員工4', '2023-01-01', 300.00);

資料結構:

departmentemployeesale_dateamount
部門A員工12023/1/1100
部門A員工12023/1/2150
部門A員工22023/1/1200
部門B員工32023/1/150
部門B員工32023/1/275
部門B員工42023/1/1300

 

在分區中計算行號:

  1. SELECT
  2. department,
  3. employee,
  4. sale_date,
  5. ROW_NUMBER() OVER (PARTITION BY department ORDER BY sale_date) AS row_num
  6. FROM
  7. sales;
  8.  

資料結果:

departmentemployeesale_daterow_num
部門A員工12023/1/11
部門A員工22023/1/12
部門A員工12023/1/23
部門B員工32023/1/11
部門B員工42023/1/12
部門B員工32023/1/23

 

在分區中計算排名:

  1. SELECT
  2. department,
  3. employee,
  4. sale_date,
  5. RANK() OVER (PARTITION BY department ORDER BY sale_date) AS rank
  6. FROM
  7. sales
  8.  

資料結果:

departmentemployeesale_daterank
部門A員工12023/1/11
部門A員工22023/1/11
部門A員工12023/1/23
部門B員工32023/1/11
部門B員工42023/1/11
部門B員工32023/1/23

 

在分區中計算累積總和:

SELECT
    department,
    employee,
    sale_date,
    amount,
    SUM(amount) OVER (PARTITION BY department, employee ORDER BY sale_date) AS cumulative_amount
FROM
    sales;

資料結果:

departmentemployeesale_dateamountcumulative_amount
部門A員工12023/1/1100100
部門A員工12023/1/2150250
部門A員工22023/1/1200200
部門B員工32023/1/15050
部門B員工32023/1/275125
部門B員工42023/1/1300300

 

在分區中計算平均薪水:

  1. SELECT
  2. department,
  3. employee,
  4. sale_date,
  5. amount,
  6. AVG(amount) OVER (PARTITION BY department) AS avg_amount
  7. FROM
  8. sales;
  9.  

資料結果:

departmentemployeesale_dateamountavg_amount
部門A員工12023/1/1100150
部門A員工12023/1/21501900/5/29
部門A員工22023/1/12001900/5/29
部門B員工32023/1/1501900/5/20
部門B員工32023/1/2751900/5/20
部門B員工42023/1/13001900/5/20

 

在分區中計算百分位數:

  1. SELECT
  2. department,
  3. employee,
  4. sale_date,
  5. amount,
  6. PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY amount) OVER (PARTITION BY department) AS median_amount
  7. FROM
  8. sales;
  9.  

資料結果:

departmentemployeesale_dateamountmedian_amount
部門A員工12023/1/1100150
部門A員工12023/1/2150150
部門A員工22023/1/1200150
部門B員工32023/1/15075
部門B員工32023/1/27575
部門B員工42023/1/130075

 

在分區中計算前後數值:

  1. SELECT
  2. department,
  3. employee,
  4. sale_date,
  5. amount,
  6. LEAD(amount) OVER (PARTITION BY department, employee ORDER BY sale_date) AS next_amount,
  7. LAG(amount) OVER (PARTITION BY department, employee ORDER BY sale_date) AS prev_amount
  8. FROM
  9. sales;
  10.  

資料結果:

departmentemployeesale_dateamountnext_amountprev_amount
部門A員工12023/1/1100150NULL
部門A員工12023/1/2150NULL100
部門A員工22023/1/1200NULLNULL
部門B員工32023/1/15075NULL
部門B員工32023/1/275NULL50
部門B員工42023/1/1300NULLNULL

留言

這個網誌中的熱門文章

ORA-12514: TNS: 監聽器目前不知道連線描述區中要求的服務

Oracle 工作排程 DBMS_JOB 筆記

Oracle 例外控制(Exception Control)