HOW TO GET TOP N RECORDS IN SQL , URGENT
Hi,
I want to get the TOP 2 and BOTTOM 2 records (TOP 2 SAL , BOTTOM 2 SAL) from the following query result for each department . How do I get it using a SQL statement ? Thanks
SQL> SELECT A.DNAME, B.ENAME, B.SAL FROM DEPT A, EMP B WHERE A.DEPTNO = B.DEPTNO ORDER BY DNAME, SAL
DNAME------------ENAME--------SAL
ACCOUNTING-------KING--------5000
----------------CLARK--------2450
---------------MILLER--------1300
RESEARCH--------SCOTT--------3000
-----------------FORD--------3000
----------------JONES--------2975
----------------ADAMS--------1100
----------------SMITH---------800
SALES-----------BLAKE--------2850
----------------ALLEN--------1600
---------------TURNER--------1500
-----------------WARD--------1250
---------------MARTIN--------1250
----------------JAMES---------950
14 rows selected.
If Feroz is asking the TOP 2 Salaried employees and BOTTOM 2 Salaried employees
for ***EACH**** DEPARTMENT, then following SQL is one of the many ways(proves good with performance).Also brings top2+ bottom2 in one shot.SELECT dname, ename, sal FROM
(SELECT d.DNAME, e.ENAME, e.SAL,
dense_rank() over(PARTITION BY e.DEPTNO ORDER BY e.SAL) dr
FROM EMP e, DEPT d
WHERE e.DEPTNO = d.DEPTNO)
WHERE dr <= 2
UNION
SELECT dname, ename, sal FROM
(SELECT d.DNAME, e.ENAME, e.SAL,
dense_rank() over(PARTITION BY e.DEPTNO ORDER BY e.SAL DESC) dr
FROM EMP e, DEPT d
WHERE e.DEPTNO = d.DEPTNO)
WHERE dr <= 2
/Thx,
SriDHAR
Similar Messages
-
How to Get Top 10 records using SQL select statement.
:-) Hiee E'body,
I want to generate a sql report in which I only want the top 10 records based on the numeric value of a column.
For Example,
To generate a Location Wise, Employee Wise Years of Service Report.
Here I need the Top 10 Employees according to their No. of Years of Service for all the existing locations using a single query.
eg.
Location Emp No. YOS
India - 22 30
212 28
819 24 ...
US 123 40
312 33
90 33
144 30 ...
UK 77 20
79 20
331 18
109 16 ...
Every Location should display their respective Top 10 Employees
regarding their No. of Years of Service.
Please let me the know the solution to this.
Have a nice day.
Thanking You,
Vivek Kapoor.For example if the table contained (India rows only shown) :
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
India 928 13
India 929 13
India 930 12
do you want to see
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
or
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
India 928 13
India 929 13
Also if the India rows were
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
India 928 30
India 929 30
do you want to see
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
or
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
India 928 30
India 929 30
Please clarify.
Thanks,
Partha -
How to get top 10 records for each option in table prompt?
Hi,
I have created one report in which my requirement is to get top 10 highest salaries for each departments. I have created one table prompt which contains the names of all departments. On the salary column I have applied one filter i.e. TOP 10. Currently I am having 3 departments. I want to show the top 10 salaries for each department, but I am getting top 3 from first, 4 from second and 3 from third.They are calculating top 10 salaries based on all departments, not on individual department. How can I get top 10 salaries for each department?Hi,
Use TopN function in your column formula.
Ex: TOPN("Sales ,5 BY department)
Thanks,
Satya -
How to get top 3 record from aggregated column
Hello,
i have a simple query which return all student in class with total marks. now i want to select top 3 student whose score is maximum. simple top three position. how can i use rank function here? or any other way to do that
select st.sti_roll_no ,st.sti_name , sum(rd.rd_obt_marks) as mycol from rd_result_detail rd , rm_result_master rm, sti_student_info st
where rm.rm_result_id = rd.rd_result_id
and st.sti_roll_no= rd.rd_student_id
--and rd.rd_student_id = 'MBP10293'
and rm.rm_semester = 3
and rm_session = 2009
and rm_batch= 3
and rm.rm_exam_type ='FINAL TERM'
and rm.rm_class_id = 'MBA'
group by st.sti_name, st.sti_roll_no
order by st.sti_roll_no;Not sure!!!!!!!!!!!!!
with t as
(select st.sti_roll_no ,st.sti_name , sum(rd.rd_obt_marks) as mycol from rd_result_detail rd , rm_result_master rm, sti_student_info st
where rm.rm_result_id = rd.rd_result_id
and st.sti_roll_no= rd.rd_student_id
--and rd.rd_student_id = 'MBP10293'
and rm.rm_semester = 3
and rm_session = 2009
and rm_batch= 3
and rm.rm_exam_type ='FINAL TERM'
and rm.rm_class_id = 'MBA'
group by st.sti_name, st.sti_roll_no
order by st.sti_roll_no)
select sti_roll_no ,sti_name,mycol,dense_rank()over(order by mycol desc) rnk
from t
--where rnk<=3
SQL> ed
Wrote file afiedt.buf
1 select e.* from (select empno,ename,sal,dense_rank()over(order by sal desc) rnk
2* from emp)e where rnk<=3
SQL> /
EMPNO ENAME SAL RNK
7839 KING 5000 1
7788 SCOTT 3000 2
7902 FORD 3000 2
7566 JONES 2975 3 -
How to get the previous record value in the current record plz help me...
In my sql how to get the previous record value...
in table i m having the field called Date i want find the difference b/w 2nd record date value with first record date... plz any one help me to know this i m waiting for ur reply....
Thanx in Advance
with regards
kotreshFirst of this not hte mysql or database forum so don;t repeate again.
to get diff between two date in mysql use date_format() to convert them to date if they r not date type
then use - (minus)to get diff. -
How to get a total record count before grouping?
I need to group a report on a formula that does roughly the following:
[record count] / ( [total record count] / 20 )
What this acheives is to label each record with a number of 1 - 20 which I want to group on. Getting this figure is the easy part, what is not working is the fact that I cannot group from a formula that is calculated after grouping. I overcame a portion of this by using "Whilereadingrecords" (rather than "count", running totals, or while printing records) to acheive a record count.
I can't figure out how to get a total record count done before grouping. Is there a way to do this with "WhileReadingRecords"?? Is this even possible?
Thanks
JohnHi John,
The order of how Crystal does things dictates the order of which features you can use. Crystal has a two pass method. In the first pass it does things like passing the query, grouping, summarizing. In the second pass it does formulas, formatting, etc.
Unfortunately Crystal does the Grouping before summarizing so what you want to do can't be done in Crystal. The best way to get around this to either create a SQL Command or view/stored procedure that will do the summarizing for you. Then in the report you can use it.
Hope this helps,
Brian -
How to get master data records that do not have transaction data in a query
Hi,
How to get master data records that do not have transaction data in a query output. Can we create a query or any other way to get the master data records that do not have transaction data?Hi,
Create a multiprovider which includes transactional data target and master data info object. Make sure that identification for this master data info object is ticked on both the provider.
Create report on this multiprovider , keep the master data info object in rows , and now you should able to see all the values which are there in master data info object irrespective of transaction happened or not .
Next you may create condition showing only zero keyfigure values , ie. master data without any transaction.
Hope that helps.
Regards
Mr Kapadia -
How i get number of record in repet frame?
Hi,
How i get number of record in repet frame?
Regards
JomarJomar,
you can use Summary Columns in the Data Modell. Create them on group higher that your wished group (so on the pane for an first group of a query), use as Source the primary column and as reset at the group, where the summary column is located.
If you hide some instances via format triggers, that you could use a counter inside the format triggers to count the rows.
Regards
Rainer -
Hierarchical query - How to get all parent records - Duplicate post
Hi,
In Oracle, START WITH, CONNECT BY commands will give all the direct and indirect child records. Other way round, is they are command which gives all the parent records till the root? Please let me know. I am working on Oracle 9i Release 2.
Thanks a lot for your help.
Edited by: skv on Nov 21, 2008 11:05 AMDuplicate post.
Hierarchical query - How to get all parent records
Please edit this post heading to duplicate post.
Regards.
Satyaki De. -
How to get Listener Information using PL/SQL code
How to get Listener Information using PL/SQL code
user2075318 wrote:
How to get Listener Information using PL/SQL codeThis approach (somewhat of a hack) can be used - but it does not really provide meaningful data at application layer.
SQL> create or replace function TnsPing( ipAddress varchar2, port number default 1521 ) return varchar2 is
2 type THexArray is table of varchar2(2);
3 --// tnsping packet (should be 10g and 11g listener compatible)
4 TNS_PING_PACKET constant THexArray := new THexArray(
5 '00', '57', '00', '00', '01', '00', '00', '00',
6 '01', '39', '01', '2C', '00', '00', '08', '00',
7 '7F', 'FF', '7F', '08', '00', '00', '01', '00',
8 '00', '1D', '00', '3A', '00', '00', '00', '00',
9 '00', '00', '00', '00', '00', '00', '00', '00',
10 '00', '00', '00', '00', '00', '00', '00', '00',
11 '00', '00', '00', '00', '00', '00', '00', '00',
12 '00', '00', '28', '43', '4F', '4E', '4E', '45',
13 '43', '54', '5F', '44', '41', '54', '41', '3D',
14 '28', '43', '4F', '4D', '4D', '41', '4E', '44',
15 '3D', '70', '69', '6E', '67', '29', '29'
16 );
17
18 socket UTL_TCP.connection;
19 txBytes number;
20 rxBytes number;
21 rawBuf raw(1024);
22 resp varchar2(1024);
23 begin
24 socket := UTL_TCP.open_connection(
25 remote_host => ipAddress,
26 remote_port => port,
27 tx_timeout => 10
28 );
29
30 --// convert hex array into a raw buffer
31 for i in 1..TNS_PING_PACKET.Count loop
32 rawBuf := rawBuf || HexToRaw( TNS_PING_PACKET(i) );
33 end loop;
34
35 --// send packet
36 txBytes := UTL_TCP.write_raw( socket, rawBuf, TNS_PING_PACKET.Count );
37
38 --// read response
39 rxBytes := UTL_TCP.read_raw( socket, rawBuf, 1024 );
40
41 UTL_TCP.close_connection( socket );
42
43 --// convert response to varchar2
44 resp := UTL_RAW.Cast_To_Varchar2( rawBuf );
45
46 --// strip the header from the response and return the text only
47 return( substr(resp,13) );
48 end;
49 /
Function created.
SQL>
SQL> select tnsping( '10.251.93.30' ) as TNSPING from dual;
TNSPING
(DESCRIPTION=(TMP=)(VSNNUM=169869568)(ERR=0)(ALIAS=LISTENER))
SQL> select tnsping( '10.251.95.69' ) as TNSPING from dual;
TNSPING
(DESCRIPTION=(TMP=)(VSNNUM=0)(ERR=0)(ALIAS=LISTENER))
SQL> -
How to order/get top 10 records of Business Object
Hi Guys,
I need some help here.
Im trying to find out how should I go about ordering my return results by .findByMultipleParameters.
Is there a simple method to also get eg. top 30 records after sorting the results?
There are some new class - OrderBy, Paging in 7.11 (EhP1 SP02) but I'm not on this version yet.
Thanks all for your help in advance.
Thanks.Hello Bertina!
I think you talk about it:
List<QueryFilter> list = new ArrayList<QueryFilter>();
list.add(QueryFilterFactory.createFilter("products.name", Condition.EQ, "Pen"));
list.add(QueryFilterFactory.createFilter("products.code", Condition.EQ, 30));
OrderBy orderBy = new OrderBy("products.name");
Paging paging = new Paging(10);
myService.findByMultipleParameters(lista, orderBy , paging, "");
A sample code, that is work fine for you.
Regards, Ronaldo.
Edited by: Ronaldo Rampelotti on Jun 18, 2009 10:36 PM -
How to get th displaye record count through SQL*Plus without result
set lines 155
set pages 100
set autoprint on
variable cv refcursor
set serveroutput on size 1000000
set timing on
set feedback on
set echo on
exec proc_name (input1, input2, :cv);how to get the record count without resultset display in the sql*plus promt ...?
plz help me....This is my earilier code
set lines 155
set pages 100
set autoprint on
variable cv refcursor
set serveroutput on size 1000000
set timing on
set feedback on
set echo on
exec proc_name (input1, input2, :cv);
Then i have tried to execute like this
declare
disp SYS_REFCURSOR;
cv SYS_REFCURSOR;
cnt number :=0;
begin
proc_name (input1, input2, :cv);
FOR disp in cv --here cv is the set of record set
LOOP
--FETCH cv INTO disp;
EXIT WHEN cv%NOTFOUND;
cnt := cnt + 1;
END LOOP;
dbms_output.put_line(cnt);
dbms_output.put_line(cv%rowcount);
CLOSE cv;
end;
getting error...
LOOP
ERROR at line 8:
ORA-06550: line 8, column 2:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
. ( % ; for
The symbol "; was inserted before "LOOP" to continue.
ORA-06550: line 13, column 2:
PLS-00103: Encountered the symbol "DBMS_OUTPUT"
ORA-06550: line 13, column 27:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( , * % & - + / at mod rem <an identifier>
<a double-quoted delimited-identifier> <an exponent (**)> as
from into || bulk
I have set of executable procedure script for exec procedure1(input1, input2 :cv); , exec procedure1(input1, input2 :cv);,.... like that. But i want only the record count, while we execute all these scripts in the sql promt...How to do that one.. ? -
Hi All,
I need to get TOP sql query in 11g Database.
I dont know about PT.
User will carry on some testing activity on the Database, i need to get the top sql from the database.
There is no access to AWR, ADDM and Entriprise console.
I need to get the output of the queries by executing some sql.
Can some body help me in this regards.
Thanking You
Pramodh>
Whether it is possible to get the full set of sql, i tried executing this it is gives the report part by part.
>
I am not sure what the problem is.
You get the top 5 SQL statements because of the "<6" part of the code. Would be top 10 if "<11" and so on.
Maybe you have got to format the output a little in SQL*Plus to get it better readable - I leave that up to you :-)
Kind regards
Uwe
http://uhesse.wordpress.com -
How to get top SQLs in a time period
HI, How can identify top SQL over a period of 1-month. Top SQL tab only displays top SQLs in last 24 hours in grid control.
NeerajAWR is your answer. Find the snapshots from Month old to current or what ever time you want and create a preserved snapshot set. and then create a report.
That will give you the top 10 sql with in that range.
AWR can be found under Adminstration tab.
Naren -
How to get first 10 records from the database using JSP
i want ot get first 10 records from the database and then after clicking the next button in the page,it must show the next precceding 10 records from the database.i am getting the first 10 records .but how to post to the same page to get another preceeding 10 record.
Search the forums - this has been asked a lot. I usually recommend experimenting with tops and order bys until you're satisfied.
Kind regards,
Levi
Maybe you are looking for
-
Error Code 12 When Transport BW Request
Dear All, I am getting a return code 12 when I execute a BW transport request from DEV to QA. When the transport starts, it actually calls a job , RDDEXECL to run. The job fails with the error : 01.08.2006 14:05:05 Job started 01.08.2006 14:05:05 S
-
Why am I getting an error code 410 when updating to Android 5.1?
My Samsung Galaxy S5 has tried twice now to upgrade from 5.0 to 5.1 (G900VVRU1BOC4). Both times I get a message that the update failed. When I check the update log it says the update failed with error code 410. What can I do to install this upgrad
-
Hi, as i said in the Subject i can't find my product.. Pavilion Dv6-6c50se my S/N: [Edited for Personal Information] my P/N: A7P18EA#ABV now what? i really need to change my Fan and i can't get the Part Number of it.. Please Help. This question was
-
ORACLE.FDK.MaximumConcurrentRequestsPerUserReached
Hi Could you please help us I have create a BPEL polling adapter which polls on database table. As soon as a record is inserted into this table, the BPEL process will kick of and creates a folder in OCS using Webservices API. Polling will run every 5
-
What is l_s_range in the Exit?
Hi, I am new to BW reporting. I saw the coding in user exit 'EXIT_SAPLRRS0_001' L_S_RANGE-LOW = '06', something like that. My question is, is this 'L_S_RANGE' a structure? how do I know what fields are there? I tried to see this in SE84 under struc