Query to retrieveonly three emplyee details from each department-v 10.2.0.4
Hi all,
Need query to retrieve only 3 employees from each department.
Order can be same as server retrieve.
Eg. If dept 10 has 5 employees, query should return only 3 employee details (emp#, deptno, salary).
If dept 20 has only 1 employee then should return one emp details.
Thanks.
with t as (
select e.*,
row_number() over(partition by deptno order by 1) rn
from emp e
select *
from t
where rn <= 3
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RN
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 1
7839 KING PRESIDENT 17-NOV-81 5000 10 2
7934 MILLER CLERK 7782 23-JAN-82 1300 10 3
7369 SMITH CLERK 7902 17-DEC-80 800 20 1
7876 ADAMS CLERK 7788 23-MAY-87 800 20 2
7902 FORD ANALYST 7566 03-DEC-81 3000 20 3
7499 ALLEN SALESMAN 7698 20-FEB-81 1500 300 30 1
7698 BLAKE MANAGER 7839 01-MAY-81 1500 30 2
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 3
9 rows selected.
SQL> SY.
Similar Messages
-
SQL Select 1 record from each department
assume emp table
1- i want to retrive 1 employee from each department
2- 2 from each
3- total 20 records 1st employee from 1st dept, 2nd from 2nd, 3rd from 3rd, 4th from 1st , 5th from 2nd and so on
Edited by: Fakhr-e-Alam on Aug 10, 2011 3:20 AMIs this homework? Remember to give OTN Forums full credit when you hand in your assignment.
This is how to do the first one: you shoudl be able to figure out the others from here.
SQL> select e.*
2 from emp e
3 where (empno, 1) in
4 ( select x.empno
5 , row_number() over (partition by x.deptno
6 order by x.hiredate )
7 from emp x )
8 order by e.deptno
9 /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 BOEHMER MANAGER 7839 09-JUN-81 2450 10
7369 CLARKE CLERK 7902 17-DEC-80 800 20
7499 VAN WIJK SALESMAN 7698 20-FEB-81 1600 300 30
8101 PSMITH 03-DEC-10 40
8060 VERREYNNE PLUMBER 8061 08-APR-08 4000 50
SQL>In this case, the employees are ordered on the basis of HIREDATE, so the longest serving employees are selected from each department. Other definitions of "first" will require different sorting criteria. You know your own business rules.
Cheers, APC
Edited by: APC on Aug 10, 2011 10:33 AM -
QUERY FOR FINDING SECOND HIGEST SALARY IN EACH DEPARTMENT...?
Hi ,
if anyone know query..please mail to my id ...[email protected]Hi,
(a) Use the analytic DENSE_RANK function (or ROW_NUMBER, depending on how you want to treat ties) to number every row, showing how each one ranks within its deparment.
(b) Then choose the ones that were assigned 2 as their rank.
Step (a) will have to be done in a sub-query and step (b) in the main query, since analytic functions are computed after the WHERE-clause is done. -
How to get two top salaries from each department
select st.staff_name,st.dept_code,
st.staff_sal
from staff_master st
where staff_code in ((select staff_code
from staff_master s
where staff_sal=(select max(staff_sal) from staff_master where dept_code=st.dept_code))
union
select staff_code from staff_master st where staff_sal=
(select max(staff_sal) from staff_master where dept_code=st.dept_code and staff_sal < (select max(staff_sal) from staff_master where dept_code=st.dept_code)))
order by st.dept_code;You can use RANK, DENSE_RANK or even ROW_NUMBER based on your need. Below is an example using DENSE_RANK. I have used EMP table available in SCOTT schema.
SQL> select * from emp order by deptno, sal desc;
EMPNO ENAME JOB MGR HIREDATE SAL COM DEPTNO ID
7782 CLARK MANAGER 7839 09-JUN-13 12450 0 10
7839 KING PRESIDENT 17-NOV-13 5000 0 10
7788 SCOTT ANALYST 7566 19-APR-87 13000 0 20
7369 SMITH CLERK 7902 02-APR-13 12975 0 20
7566 JONES MANAGER 7839 02-APR-13 12975 0 20
7876 ADAMS CLERK 7788 23-MAY-87 11101 0 20
7698 BLAKE MANAGER 7839 01-MAY-13 12850 0 30
7499 ALLEN SALESMAN 7698 20-FEB-13 11600 300 30
7844 TURNER SALESMAN 7698 08-SEP-13 11500 0 30
7521 WARD SALESMAN 7698 22-FEB-13 11250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-13 11250 1400 30
11 rows selected.
SQL> select *
2 from (
3 select e.*
4 , dense_rank() over(partition by deptno order by sal desc) emp_rank
5 from emp e
6 )
7 where emp_rank <= 2;
EMPNO ENAME JOB MGR HIREDATE SAL COM DEPTNO ID EMP_RANK
7782 CLARK MANAGER 7839 09-JUN-13 12450 0 10 1
7839 KING PRESIDENT 17-NOV-13 5000 0 10 2
7788 SCOTT ANALYST 7566 19-APR-87 13000 0 20 1
7369 SMITH CLERK 7902 02-APR-13 12975 0 20 2
7566 JONES MANAGER 7839 02-APR-13 12975 0 20 2
7698 BLAKE MANAGER 7839 01-MAY-13 12850 0 30 1
7499 ALLEN SALESMAN 7698 20-FEB-13 11600 300 30 2
7 rows selected.
SQL> -
SQL query to get transaction detail from DEFERRED_TRAN_ID
Hi,
I'm using Oracle Advance Replication and get the transaction detail from Enterprise Manager Console.
So instead of using the console.
Is there a way any SQL query which get transaction details like SQL query fired, old and new column value etc from DEFERRED_TRAN_ID.
thanksquote:
Originally posted by:
lucapac
I have two tables: tblWorkers and tblSkills. tblWorkers has a
column, Skills, which is populated from a multiple-checkbox form
field with one or more skill_IDs from tblSkills, so each
tblWorkers.Skills consists of a list of one or more comma-delimited
values. For any Skill_ID, I need to generate a listing of all
Workers with the corresponding skill, so I have tried to do
something along the lines of SELECT WorkerName FROM tblWorkers
WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
(ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
got data type mismatches out of the way) return all Workers, not
just those with the desired Skill. There must be an easy way to do
this ... How do people with a bit more CF/SQL experience than I
have do this???
As Kronin implied, we normalize our databases. If you don't
understand that answer, the book "Database Design for Mere Mortals"
is often mentioned on this forum. -
Query SSIS packages details from file system
Hi All,
I have around 500 packages (SQL 2005) deployed in file system & all this packages are running on daily basis via SQL agent. Now I need to migrated all 500 packages into SQL server 2008 R2.
There is no inventory to track which package belogs to which team and any other information.
Now, I need a method to query the pakages connection string details with database respective. Is there any method?
Thanks
RaghuCheck this out, might be helpful for your requirement
Read SSIS Package File(.dtsx) for Information [Connection Managers,Transformations,Variables,Configuration,Expressions,Log Providers]
http://sqlage.blogspot.com/ -
Generating multiple details from a single input row
Post Author: dhay
CA Forum: General
I'm using Crystal Reports Developer XI.
Each input row from the database contains a starting serial number and the number of serial numbers to be generated. The detail section of my report consists of a form containing the serial number. I want to create a form for each serial number that is to be generated. For example, if the database supplies a starting serial number of 11, and the number of serial numbers to be generated is equal to 3, my report would create three forms, the first containing serial number '11', the second form contains serial number '12', and the third, '13'. The next input row would then be read, and multiple forms would be created for that input row. And so on.
So, I want each input row to create as many instances of my report's detail section as is specified by the input field 'number of serial numbers to be generated'.
In my prior experience, each input row generates a single detail section instance on the report. Can I do what I described above in Crystal? Alternatively, is there a way to do this using SQL (possibly generating an intermediate table or view) so that the Crystal Report would receive one input row per serial number (and would thus work in the normal manner)?
Thanks in advance,
Donassume you have a check box at column no 1. and print button
create a hidden item on page zero
create a PL?SQL page process on submit - after computations and validation with condition when button press is print
and put this code
FOR i in 1..APEX_APPLICATION.G_F01.count
LOOP
if i = 1 then
:p0_invoice_selected := APEX_APPLICATION.G_F01(i);
else
:p0_invoice_selected := ::p0_invoice_selected ||','||APEX_APPLICATION.G_F01(i);
end if;
END LOOP;
create page branching to zero page and call the report on the request field
PRINT_REPORT=print_invoice and condition when button press is print
in your invoice print query put the condition
where invoice_id in (:p0_invoice_selected)
in your report template insert a group with invoice_id at the top and select page break.
Now about your page no select the section-page no not the normal page no.
but you have invoice-id as a character then i would suggest you create a type
CREATE OR REPLACE type list_table as table
of varchar2 (500);
add a function
function in_list_table( p_string in varchar2 ) return list_table
as
l_string long default p_string || ',';
l_data list_table := list_table();
l_instr number;
begin
loop
exit when l_string is null;
l_instr := instr( l_string, ',' );
l_data.extend;
l_data(l_data.count) :=
ltrim( rtrim( substr( l_string, 1, l_instr-1 ) ) );
l_string := substr( l_string, l_instr+1 );
end loop;
return l_data;
end;
and call the function in your sql query
invoice_id IN
(SELECT list.COLUMN_VALUE
FROM table(CAST (
in_list_table(:p0_invoice_selected) AS list_table
)) list))
hope this will solve your problem. -
How to merge details from 2 different universe through webi (XIR2)u2026.
I have 2 DPs with one common dimension and 2 uncommon dimensions each...
DP1
EmployeeID
Name
Address
DP2
EmployeeID
Title
BirthDate
I bring both queries in. I merge on EmployeeID. To get the Name, Address, Title and BirthDate to all appear in the same block, I understand that I would have to make either Name/Address or Title/Birthdate to be details of there respective EmployeeID. My question is, what if I make these four columns details from within their respective universe (and not from the report). I can't get these columns to appear in the same block. Keeps getting incompatible object. What's the difference between making details on the report side (as oppose to from the universe)? I want to make it easy for report developers and not to always make uncommon demensions to be details from the report side.
In each of the universe, when I create the details, I specify which dimensions they are associated with. From within the Webi report, once I merge the common dimension (EmployeeID), I would see the Imcompatible Object message only if I try to DRAG the detail objects into the same block. If I first go to the block, go to a column inside the block, right click and create new blank column, double click on blank column to see formula bar, paste detail item into formula bar...that would work....
So in summary, this works after merging EmployeeID dimensions from 2 different universes...
[EmployeeID], [Name], [Address], (create blank column and paste: =[Title] in formula of blank column)
this does not work...
[EmployeeID], [Name], [Address], (drag [Title] into column next to [Address]) {Incompatible Object message appears}
If you create a details from the report side, all would work fine. You can merge all objects into same block without problems. I just don't understand why if a dimension has 10 details (and they are clearly attributes of a dimension) why can't these be done at the universe level and not have users recreate them at the report level.Hi Anthony,
Following information might help you to resolve the issue.
Inserting Detail object into table.
Table already contains a merged dimension based on the Dimension object to which that Detail is associated.
Error: "Cannot Drop Here: Incompatible Objects".
Workflow:
Create WebIntelligence report using Island Resorts Universe
Select <Customer>, <Country of Origin>, <Age>, <Address>
Add second query using: <Customer>, <Sales Person>, <Year>, <Revenue>
Create merged dimension for <Customer> from Query 1 and Query 2
Insert new table using objects: <Merged Customer>, <SAles Person>, <Year>, <Revenue>
Try to insert <Age> or <Address>. Error "Cannot Drop Here: Incompatible Objects".
Solution:
Logged as product defect: ADAPT00625276 Fixed in Service Pack 2.
Workaround:
Create a report level variable, defined as a Detail, which is associated to the <Merged Customer> merged dimension.
Insert this report level variable into the table - no error is generated.
I hope this will help you.
Regards,
Sarbhjeet Kaur -
Campaign Details from Target Group
Hi All,
I have a requirement, in which from the GUID of the Target Group, i need to fetch the details of the Campaigns in which it is assigned (There will be multiple assignement).
I found two Tables for this:
1. CRMD_MKTPL_TGGRP : Field: TARGETGRP_GUID
2. CRMD_MKTTG_TG_H : Fields: SET_H_GUID, SET_GUID, PROF_GUID
My logic was to join these table for getting the Campaign for each Target Group, but unfortunately i didnt find any common data in between the field TARGETGRP_GUID of CRMD_MKTPL_TGGRP and above stated 3 fields of the CRMD_MKTTG_TG_H.
is there any FM available for getting the Campaign details from the Target Group?
Please share your comments.
Regards
DaveHi Dave,
Did you try to use the dynamic query SEGTgAdvQuery?
this is a Dynamic query that searches for Target Groups. The result BOL Object contains a relation SEGTgTgCampaignRel that contains all Marketing Campaigns for that Target Group.
here is how you could call the dynamic query.
data: lr_query_service TYPE REF TO cl_crm_bol_dquery_service,
lr_selection_param TYPE REF TO if_bol_selection_param,
lr_selection_param_buil TYPE REF TO if_bol_selection_param,
lr_tg TYPE REF TO cl_crm_bol_entity,
lr_mkt TYPE REF TO if_bol_entity_col,
lr_query_result TYPE REF TO if_bol_entity_col,
lr_query_service = cl_crm_bol_dquery_service=>get_instance( 'SEGTgAdvQuery' ).
IF lr_query_service IS BOUND.
CALL METHOD lr_query_service->insert_selection_param
EXPORTING
iv_index = 1
iv_attr_name = 'ID'
iv_sign = 'I'
iv_option = 'EQ'
iv_low = '12345' "This is your Target Group ID
RECEIVING
rv_selection_param = lr_selection_param_buil.
lr_query_result = lr_query_service->get_query_result( ).
lr_tg ?= lr_query_result->if_bol_bo_col~get_first( ) .
IF lr_tg IS NOT INITIAL.
lr_mkt ?= lr_tg->get_related_entities( 'SEGTgTgCampaignRel' ).
NOW YOU HAVE THE LIST OF MKT CAMPAIGNS...
ENDIF.
ENDIF.
hope this helps.
Kind regards,
Micha -
How do I transfer my entire iTunes library from my old laptop to my new one? At the moment using Home Sharing only transfers a few songs from each album. Is there another way I can transfer from one laptop to another?
Hello DeanoH1975,
The following article provides several options available for transferring your library to a new computer, and details each process.
iTunes: How to move your music to a new computer
http://support.apple.com/kb/HT4527
Cheers,
Allen -
HT1918 How do I remove my credit card details from my Apple Account?
How do I remove my credit card details from my Apple Account? I do not like the fact that Apple does not provide a clear and simple option to 'Delete a payment card', I definitely do not think this is legal.
I ask this question because I recently received a spam-like message from Apple asking me to log in and update my account. I went in and did that, but I would like to remove credit card details from the account and it is horribly stubborn about holding onto my credit card details.Hi there
Thankyou for the latter responses from ssschmidt and limnos - as it happens I figured this out by myself. Yes, it appears that Apple will not let you delete an account UNTIL YOU HAVE VALIDATED THE CARD DETAILS. That it, until you have given them all of your credit card details - even down to the 3 digit validation number. Only once you have provided this information does the 'Payment type - NONE' appear.
I figure however, that you can just put any 3 digits in, ie, it doesn't need to be your actual 3 pin code, for the system to let you pass to the NONE option.
As I said before, I still think this ***** the big one. Apple should not be insisting on getting customers' bank details in this way - especially since there are these PHISHING emails floating around which look extremely like official Apple communications. It was the receipt of one such email that alerted me to this issue in the first place - thanks Limnos, I didn't respond :-)
This is the first time I've used this 'user to user' set up for a query like this, and I totally think it's ridiculous. This information should be readily available from Apple - no wonder their profit levels are so astromical!
Happy Mondays y'all!
m -
I just recently installed Firefox for the first time. It seems nice and quick. The version is reported as: "10.0.1".
I wanted to save some text from a web page, so navigated to that page, selected the text, and pressed the Control-C combination to COPY the selected text to the buffer. For example, the text I selected looked something like this:
Harry
Ron
Hermione
Hagrid
Albus
NOTE: Each line of text has a small icon to the left of the text.
It is not reasonable to COPY and PASTE each line individually, as there can be hundreds of lines of data. I recall, however, that
doing a COPY and PASTE on this data into Microsoft's Excel will produce cells which have the icons included in the cell, but unfortunately one cannot can't get rid of them! At least I've never found a way to remove them, but that's another issue. :)
Once I'd done the COPY operation I switched to a Notepad window and did a PASTE operation. To my surprise, the text from each line was duplicated. It looked like this:
Harry Harry
Ron Ron
Hermione Hermione
Hagrid Hagrid
Albus Albus
Thinking that there might be something unusual about the web page I looked at the source, but it appeared "normal" -- that is, as expected.
Note: I have done this operation several times before, and have never seen this occur before.
Note: In the actual data some of the lines have quoted text in them. Curiously there is weird behavior on these lines. In some cases the entire line is shown only once. (These occur at the top of the line, and the quoted text is at the beginning of the name.)
When quoted text appears "later" in the name, in some cases the quoted text is duplicated, and in other cases the quoted text is missing altogether! I have also noticed an error with the quoted text, and so will be reporting that to the web site which generates the HTML.
Note that each line of "text" is "anchor text", so if I click on a name the browser navigates to a page for that name.
I believe that the problem is that the COPY operating in Firefox is not simply copying the visible text, but also the ALT=
Below is a sample of what the source HTML looks like:
<a class="lnk" target="_blank" href="http://details.aspx?id=Harry">
<img width="16" height="16" alt="Harry" class="tb_icon" src="http://.../Harry.gif"/>
<span>Harry</span></a>
<br/>
(Because of the true length of the lines in the source HTML, I have stripped out the actual URL of the site.)
To make sure I wasn't imaging this difference I repeated the process within Internet Explorer. In that browser I did not get duplicated data.Try:
*Extended Copy Menu (fix version): https://addons.mozilla.org/firefox/addon/extended-copy-menu-fix-vers/ -
Query is not picking the data from multiprovider
Hi All,
i have defined a query on a multiprovider which is made up of three ods one from sales orders, delivery, and billing but when i run the query ...its not picking up order quantity for some sales orders and also its not picking all the items available for that orders...but for some orders its picking up the order quantity but not delivery quantity...
order quantity is coming from sales orders ods and delivery quantity is coming form delivery ods, billing quantity is coming from billing ods...can somebody help me with a suggestion like what could be ther problem...
any sugestions will be highly appreciated.
with regards
harishdear Harish,
multiprovider works as union,
you have order, delivery, billing, e.g the data like
order
orderno orditem customer product ordqty
ORD001 1 a x 10
ORD001 2 a y 20
ORD002 1 b x 30
delivery
delvno delvitem customer product delvqty
DLV001 1 a x 8
DLV002 1 b x 25
billing
billno billitem customer product billqty
BIL001 1 a x 8
multiprovider
orderno-orditem-delvno-delvitem-billno-billitem-customer- product-ordqty-delvqty-billqty
ORD001 1 blank blank blank blank a x 10 blank blank
ORD001 2 blank blank blank blank a y 20 blank blank
ORD002 1 blank blank blank blank b x 30 blank blank
blank blank DLV001 1 blank blank a x 8
blank blank DLV002 1 blank blank b x blank 25 blank
blank blank blank blank BIL001 1 a x blank blank 8
what can you do is remove order no, order item, delivery no, delivery item, billing no, and billing item from query row area.
hope this helps. -
Delete the payment details from a BPmaster data when there is still an deal
Hi All ,
what happens when i delete the payment details from a BP master data (counter party role in payment details tab ) when there is still an outstanding deal for this BP?
Will this be reflected in the saved deal, so will the deal go into error after the payment details have been removed?
I test this scenario in my dev system with following steps
1. I create a deal with t code FTR_CREATE
2. Delete all payment details from BP Master (counter party role in payment details tab )
3. i settle deal t code FTR_EDIT
4. I run TBB1 to create a payment request
5. then i run F111 and all required details and also payment request system is create payment without any error
all the able steps i did with out any error ... i think system should through error while running F111 t code because in BP master all data is deleted .
so i want to know whether system will through error mesg when we delete payment details from BP MASTER
regardsHi,
The payment details are only a standing instruction which is captured by various create programs for respective financial transaction. Further, it is derived only once at the time of creation each time.
Deleting payment details will not give you any error message for already created transactions, for new transactions, you may have to put it manually at the time of creation else will throw an error message with TBB1.
Regards
Prasad AV -
Hello,
Can anyone guide me how to access the calendar exception details from Project Server using PSI?
I need to extract calendar details of enterprise resources , like exception name, exception type, exception start date and exception end date into my SSRS report hosted in SharePoint 2013 and 2010.
I would be helpful if I can get a sample of this. I have read through many PSI documents in fact still going through ,what PSI does and doesn't , PSI methods etc. from Project Server SDK and MSDN . Moreover, I
am a beginner in .NET programming. I am confused and have lots of questions in my mind, like which PSI service should be used in my report(is it just Calendar.svc), can we pull the details as XML type data source my SSRS report ,are
there any other configuration settings apart from " setting up an event handler by installing an event handler assembly on each Project Server computer in the SharePoint farm, and then configuring the event handler
for the Project Web App instance by using the Project Server Settings page in the General Application Settings of SharePoint Central Administration" (as per prerequisites for PSI in SDK) , how can I implement authentication settings
-(when user with proper SharePoint permission
can accesses the SSRS report )
Kindly bear with me if my questions are not appropriate .
Please do guide me, and .
Thanks in Advance!!
Mridhula
Mridhula.SHi Brendan,
Project server reporting database doesn't contain the calendar info. The only supported way to use the PSI to read the calendar info from Published database.
See this
reply from Amit.
Hope this helps,
Guillaume Rouyre, MBA, MVP, P-Seller |
Maybe you are looking for
-
Error while reversing goods issue using VL09
Hi All , user is getting the error " 4902546971document does not contain any selectable items " when reverse the GI using VL09. Please find the below document flow. Free of Charge 70475917 / 10 . Purchase order 9900099037 / 10 . Delivery Standard
-
PO printing issue in Dot Matrix printer
Hi expert We got unnecessary spaces on the second page while taking a PO print out. printer is dot matrix- EPSON LQ2180. Page Format DINA4 Device type is EPESCP2 Can any body help in this matter?what have i to do remove unnecessary spaces from PO
-
Creating PDFs from a PDF in Snow Leopard
All, With Snow Leopard, I'm unable to create a PDF from a PDF using Acrobat 8 or 9 because it doesn't work and theres no fix, yet. What I do.... someone mails me a PDF, I make comments, fill in info, etc, and need to create another PDF from that one
-
"Lighting Effects" not showing active in menu
I am running CS6 and dont have access to Light Effects in my drop down menu. How do I activate it for use? Thanks
-
I'm Having trouble streaming videos from a website , what do I do ?