PDA

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).

minhpn
25-01-2005, 14:20
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....

quick
27-01-2005, 13:13
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

Asus
29-01-2005, 00:50
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
/

hithere
18-02-2005, 17:09
SELECT top 5 emp_no,emp_dep,emp_sal FROM emp ORDER BY emp_sal DESC

quanlh
24-02-2005, 11:02
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

trungnd
29-10-2006, 22:50
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;

Cyber
09-05-2007, 16:17
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);

kendo
10-05-2007, 20:12
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ờ đã... :(