View Full Version : Co ai la "CHIEN GIA" SQL-Oracle khong giup minh voi!
thaisalem
24-01-2005, 10:09
Co ai giup minh giai bai tap nay nhe: Trong Oracle 8i
dung SQL, chon ra 5 nguoi co luong cao nhat trong bang
emp(emp_no,emp_dep,emp_sal).
Dùng lệnh (có thể chạy hơi chậm):
select * from (
select emp_no,emp_sal from emp order by emp_sal desc)
where rownum<6
thaisalem
26-01-2005, 09:12
Nhung ma minh da thu, voi server cua minh xai Oracle 8.0 ma van khogn thuc hien lenh nay duoc....
Tớ thấy có lẽ là thế này:
select *
from (select row_number() over(order by emp_sal desc) as rownum, emp_no, emp_dep, emp_sal from emp)
where rownum<6
Bạn chỉ ra giùm tớ xem trong cái table đó thì field nào dùng store mức lương vậy? emp(emp_no,emp_dep,emp_sal)
yedc2001
08-02-2005, 17:10
Co ai giup minh giai bai tap nay nhe: Trong Oracle 8i
dung SQL, chon ra 5 nguoi co luong cao nhat trong bang
emp(emp_no,emp_dep,emp_sal).
Ban thu viet bang PL/SQL nhu sau (nho coi lai structure table emp(emp_no, emp_dep, emp_sal ) phai giong table resultat (id_emp number(4), no_dep number(4), sal number(4))) :
CREATE TABLE resultat (id_emp number(4), no_dep number(4), sal number(4))
/
DECLARE
CURSOR C1 SELECT emp_no, emp_dep, emp_sal FROM emp
ORDER BY emp_sal DESC;
v_emp_no emp.emp_no%TYPE;
v_emp_dep emp.emp_dep%TYPE;
v_emp_sal emp.emp_sal %TYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO v_emp_no, v_emp_dep, v_emp_sal ;
IF C1%ROWCOUNT < 6;
THEN INSERT INTO resultat(id_emp, no_dep, sal)
VALUES (v_emp_no, v_emp_dep, v_emp_sal );
END IF;
END LOOP;
END;
/
SELECT * FROM resultat
/
SELECT top 5 emp_no,emp_dep,emp_sal FROM emp ORDER BY emp_sal DESC
He he, cho cái câu lệnh này vào mà chạy, dùng rownum thì không chính xác đâu, trong Oracle không có cái từ khóa top 5 đâu
select * from emp a
where 5 > (select count(*) from emp b where a.sal < b.sal);
buon_vi_dep_2003
23-10-2006, 17:43
Select Employee_ID,last_name,first_name,salary
From (Select * From employees order By Salary desc)
Where rownum<6;
Conn = Hr nha ban!
therebex
23-10-2006, 23:55
select * from emp
order by emp_sal
where count(*) = 5
Dùng Top-N Analysis thôi:
SELECT ROWNUM as Rank, ename, sal
FROM (SELECT ename, sal FROM emp
ORDER BY sal DESC)
WHERE ROWNUM <=5;
He he, cho cái câu lệnh này vào mà chạy, dùng rownum thì không chính xác đâu, trong Oracle không có cái từ khóa top 5 đâu
select * from emp a
where 5 > (select count(*) from emp b where a.sal < b.sal);
Hay quá. Cách này dùng subquery quá hay.
buon_vi_dep_2003
10-05-2007, 17:08
các câu sau đúngChọn ra 5 người lương cao nhất
Select Employee_ID,last_name,first_name,salary
From (Select * From employees order By Salary desc)
Where rownum<=5;
select * from (
select employee_id,salary from employees order by salary desc)
where rownum<6;
select * from employees a
where 5 > (select count(*) from employees b where a.salary < b.salary);
Chủ đề này, mình nhớ cách đây khoảng 2 năm, trên oravn.com đã bàn tới, phần tranh luận của anh Nhsang và 1 member nữa. Rất tiếc, các bài phân tích của họ bây giờ đã... :(
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.