作为IT工程师,掌握Oracle SQL的高级用法可以显著提升数据分析能力和业务洞察力。以下是一些关键的高级技术和方法:
SELECT employee_id, department_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as dept_rank,
PERCENT_RANK() OVER (PARTITION BY department_id ORDER BY salary) as percentile
FROM employees;
SELECT date, sales,
AVG(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as moving_avg,
SUM(sales) OVER (ORDER BY date ROWS UNBOUNDED PRECEDING) as running_total
FROM daily_sales;
WITH org_hierarchy AS (
-- 基础查询:获取顶级管理者
SELECT employee_id, first_name, last_name, manager_id, 1 as level
FROM employees
WHERE manager_id IS NULL
UNION ALL
-- 递归部分:获取下属员工
SELECT e.employee_id, e.first_name, e.last_name, e.manager_id, h.level + 1
FROM employees e
JOIN org_hierarchy h ON e.manager_id = h.employee_id
)
SELECT LPAD(' ', 2*(level-1)) || first_name || ' ' || last_name as employee_name, level
FROM org_hierarchy
ORDER BY level, employee_name;
SELECT *
FROM (
SELECT department_id, job_id, salary
FROM employees
)
PIVOT (
AVG(salary) FOR job_id IN ('IT_PROG' as IT, 'SA_REP' as Sales, 'FI_ACCOUNT' as Finance)
)
ORDER BY department_id;
SELECT *
FROM quarterly_sales
UNPIVOT (
sales_amount FOR quarter IN (q1 AS 'Q1', q2 AS 'Q2', q3 AS 'Q3', q4 AS 'Q4')
);
SELECT *
FROM stock_prices
MATCH_RECOGNIZE (
PARTITION BY stock_symbol
ORDER BY trade_date
MEASURES
FIRST(A.trade_date) AS start_date,
LAST(C.trade_date) AS end_date,
AVG(A.price) AS start_price,
AVG(C.price) AS end_price
ONE ROW PER MATCH
AFTER MATCH SKIP TO LAST C
PATTERN (A B* C)
DEFINE
B AS B.price > PREV(B.price),
C AS C.price > PREV(C.price)
)
ORDER BY stock_symbol, start_date;
-- 从JSON提取数据
SELECT json_data,
JSON_VALUE(json_data, '$.customer.name') as customer_name,
JSON_VALUE(json_data, '$.order.total' RETURNING NUMBER) as order_total
FROM orders_json;
-- 创建JSON数据
SELECT JSON_OBJECT(
'employeeId' VALUE employee_id,
'name' VALUE first_name || ' ' || last_name,
'department' VALUE department_id
) as employee_json
FROM employees;
SELECT department_id, job_id, SUM(salary) as total_salary
FROM employees
GROUP BY ROLLUP(department_id, job_id);
SELECT region, product_category, quarter, SUM(sales) as total_sales
FROM sales_data
GROUP BY CUBE(region, product_category, quarter);
SELECT region, product_category, quarter, SUM(sales) as total_sales
FROM sales_data
GROUP BY GROUPING SETS (
(region, product_category),
(region, quarter),
(product_category, quarter),
()
);
CREATE MATERIALIZED VIEW mv_monthly_sales
REFRESH COMPLETE ON DEMAND
ENABLE QUERY REWRITE
AS
SELECT
TRUNC(sale_date, 'MONTH') as month,
product_id,
SUM(amount) as total_sales,
COUNT(*) as transaction_count
FROM sales
GROUP BY TRUNC(sale_date, 'MONTH'), product_id;
-- 分区裁剪
SELECT * FROM sales PARTITION(sales_q1_2023);
-- 分区连接
SELECT /*+ LEADING(d) USE_NL(s) */
d.department_name, SUM(s.amount)
FROM sales s
JOIN departments d ON s.department_id = d.department_id
WHERE s.sale_date BETWEEN TO_DATE('01-JAN-2023', 'DD-MON-YYYY')
AND TO_DATE('31-MAR-2023', 'DD-MON-YYYY')
GROUP BY d.department_name;
SELECT
customer_id,
PREDICTION(credit_risk_model USING *) as predicted_risk,
PREDICTION_PROBABILITY(credit_risk_model, 'LOW' USING *) as low_risk_prob
FROM customer_data;
SELECT
customer_id,
CLUSTER_ID(customer_segments USING *) as segment_id,
CLUSTER_DETAILS(customer_segments, CLUSTER_ID(customer_segments USING *) USING *) as segment_details
FROM customer_profiles;
掌握这些高级Oracle SQL技术可以帮助您从数据中提取更有价值的业务洞察,优化查询性能,并解决复杂的数据分析问题。