Help in admin query!!!
how can i get table names exist in my schema HR by query
if there is such documentation from where i can get information about such kind of queries
please help me...........
if there is such documentation from where i can get
information about such kind of queries
please help me...........Go to http://tahiti.oracle.com pick version, browse Administration manual and/or Reference, which contains all the info about data dictionary views.
Another possibility is to get infio from the dictionary itself:
SELECT * FROM dict WHERE table_name like '%TAB%';
And see what view you'll need
Gints Plivna
http://www.gplivna.eu
Similar Messages
-
Hi, How I can get desired result. I am joining two tables but if date timeline does't exist in #two table then should show hyphen (-). Please help on this query. Thanks.
create table #one (code_p char(4), code_h char(2), code_date datetime)
insert into #one values ('DEHG','2','2010-01-01')
insert into #one values ('DEHG','2','2011-01-01')
insert into #one values ('DEHC','2','2009-01-01')
insert into #one values ('DEHG','2','2012-01-01')
create table #two (code_p char(4), code_h char(2), code_date datetime)
insert into #two values ('DEHG','2','2010-01-01')
insert into #two values ('DEHC','2','2009-01-01')
select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
p.code_p code_p_two, p.code_h code_h_two, p.code_date code_date_two from #one p join #two a on p.code_p = a.code_p
--Result from the above query
code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
DEHG 2 2010-01-01 DEHG 2 2010-01-01
DEHG 2 2011-01-01 DEHG 2 2010-01-01
DEHC 2 2009-01-01 DEHG 2 2009-01-01
DEHC 2 2012-01-01 DEHG 2 2009-01-01
--Desired result
code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
DEHG 2 2010-01-01 DEHG 2 2010-01-01
DEHG 2 2011-01-01 DEHG 2 -
DEHC 2 2009-01-01 DEHG 2 2009-01-01
DEHC 2 2012-01-01 DEHG 2 -Try this:
select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
p.code_p code_p_two, p.code_h code_h_two, ISNULL(CONVERT(varchar(50),a.code_date,121),'-') code_date_two
from #one p
left join #two a on p.code_p = a.code_p and p.code_date=a.code_date
If this post answers your query, please click "Mark As Answer" or "Vote as Helpful". -
Hello all,
Total newbie to this pl/sql stuff. So, deseperately need help in my query.
BOOKING_ID BOOKING_STATUS BOOKING_DATE BOOKING_TIME BOOKING_DATE_TIME
1234567 CANCELLED 20090301 37252 5/1/2010 10:20
1234567 CANCELLED 20090301 44229 5/1/2010 12:17
1234567 BOOKED 20090301 39462 5/1/2010 10:57
1234567 CANCELLED 20090301 43549 5/1/2010 12:05
9671111 BOOKED 20090301 68124 5/1/2010 12:57
9671111 CANCELLED 20090301 45001 5/1/2010 12:05
How do I write my query such that I would get the following results:
BOOKING_ID BOOKING_STATUS BOOKING_DATE BOOKING_TIME BOOKING_DATE_TIME
9671111 BOOKED 20090301 68124 2/4/2010 12:17
Basically, I am looking at the latest BOOKING_TIME and making sure the BOOKING_STATUS=BOOKED, if not, don't even bother bring back the result. Hence, you see that BOOKING_ID=1234567 is not required since at the latest BOOKING_TIME=44229, the BOOKING_STATUS=CANCELLED.
Any help is greatly appreciated.
Thank you in advance for your help.
Stanley HoHi, Stanley,
Welcome to the forum!
Whenever you have a question, please post your sample data in a form that people can actually use. CREATE TABLE and INSERT statements are perfect.
For example:
CREATE TABLE booking
( booking_id NUMBER (8)
, booking_status VARCHAR2 (10)
, booking_date_time DATE
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 10:20', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 12:17', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'BOOKED', TO_DATE ('5/1/2010 10:57', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (9671111, 'BOOKED', TO_DATE ('5/1/2010 12:57', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (9671111, 'CANCELLED', TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));What you want is called a Top-N Query .
Here's one way to do it:
WITH got_rnum AS
SELECT booking.*
, ROW_NUMBER () OVER ( PARTITION BY booking_id
ORDER BY booking_date_time DESC
) AS rnum
FROM booking
SELECT booking_id
, booking_status
, TO_CHAR (booking_date_time, 'YYYYMMDD') AS booking_date
, TO_CHAR (booking_date_time, 'SSSSS') AS booking_time
, TO_CHAR (booking_date_time, 'MM/DD/YYYY HH24:MI') AS booking_date_time
FROM got_rnum
WHERE rnum = 1
AND booking_status = 'BOOKED'
;Notice that you don't need PL/SQL to do this; plain old SQL is good enough.
Of course, if you're using PL/SQL for other reasons, you can use a query like this within PL/SQL.
Dates (including time of day) should always be stored in DATE columns.
If you have a DATE column, like booking_date_time, then there's no need for redundant date and time columns.
You can always display just the year-month-day, or just the time, in any format, as I did above.
The output from the query above, with the data above, is:
BOOKING_ID BOOKING_ST BOOKING_ BOOKI BOOKING_DATE_TIM
9671111 BOOKED 20100501 46620 05/01/2010 12:57I realize the booking_date and booking_time columns aren't quite what you posted. If they are not derivable from booking_date_time, then you probably do need separate columns for them, and those columns can easily be added to the query above.
Edited by: Frank Kulash on Feb 5, 2010 4:41 PM
KEEP (DENSE_RANK ...) , like Max used below, is a great tool to have in your kit. The problem with it is that you have to repeat a lot of stuff for every column, so the more columns you have in your output, the more tedious it gets. ROW_NUMBER sclaes much better, and is adaptable to more situations. I suggest you master ROW_NUMBER first, and look into KEEP (DENSE_RANK ...) later. -
I need help with SQL query (if it can be accomplished with query at all).
I'm going to create a table with structure similar to:
Article_Name varchar2(30), Author_Name varchar2(30), Position varchar2(2). Position field is basicly position of an article author in the author list, e.g. if there is one author, his/her position is 0, if 2, then 1st author is 0, second is 1, etc.
Article_Name Author_Name Position
Outer Space Smith 0
Outer Space Blake 1
How can I automate creation of Position, based on number of authors on the fly? Let's say I have original table without Position, but I want to create a new table that will have this information.
RegardsIf you have an existing table whose structure doesn't tell you what position the author is in, what's the algorithm you'd use to determine who was the first author, the second author, etc? If you issue a select query on a table without providing an "order by" clause, Oracle makes no guarantees about the order in which it retrieves rows.
As an aside, why would you store position number in a varchar2 field? If it's a number, it ought to be stored as a number.
Justin -
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De. -
Hi,
I have a client requirement to show a report on the device availability. The report should show the output as
Node Availability%
Formula for Availability = (Total No. of Failed/Total No. rows) * 100
My Oracle Table has the following data
NODE SUMMARY
172.16.10.55 Default Interface Ping fail for 172.16.10.55: ICMP timeout
172.16.10.55 Default Interface Ping restore for 172.16.10.55
172.16.10.55 Default Chassis Ping restore for 172.16.10.55
172.16.10.55 Default Chassis Ping fail for 172.16.10.55: ICMP timeout
172.16.10.55 Default Chassis Ping restore for 172.16.10.55
172.16.10.55 Default Chassis Ping fail for 172.16.10.55: ICMP timeout
172.16.10.55 Default Chassis Ping fail for 172.16.10.55: ICMP timeout
172.16.10.55 Default Interface Ping restore for 172.16.10.55
172.16.10.55 Default Interface Ping fail for 172.16.10.55: ICMP timeout
172.16.10.56 Default Chassis Ping restore for 172.16.10.56
172.16.10.56 Default Interface Ping fail for 172.16.10.56: ICMP timeout
172.16.10.56 Default Chassis Ping fail for 172.16.10.56: ICMP timeout
172.16.10.56 Default Chassis Ping restore for 172.16.10.56
172.16.10.56 Default Chassis Ping fail for 172.16.10.56: ICMP timeout
172.16.10.56 Default Chassis Ping restore for 172.16.10.56
172.16.10.56 Default Chassis Ping restore for 172.16.10.56
172.16.10.56 Default Interface Ping fail for 172.16.10.56: ICMP timeout
In the above table the Summary column has the details like 'Ping fail' , 'Ping restore' for each Node. So, for each Node I have to compute the Total Ping Fail / (Total Ping Fail + Ping Restore) * 100 to compute the availability %.
My output should be like the below
Node Availability%
172.16.10.55 55.55
172.16.10.56 54
Can someone please help me with query.
I appreciate your help in advance.
Thanks.
Regards,
RaviShankar.My Oracle Table has the following dataThat's great, but if you want maximum response to your question, then post CREATE TABLE + INSERT INTO statements.
I currently do not have the time to turn your data into them.
And always post the database version you're using.
http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
Also use the {noformat}{noformat} tag in order to post examples that benefit from staying formatted and thus readable when posted on the forum.
Simply put the tag before and after your examples.
For example, when you type:
{noformat}select *
from dual;{noformat}
it will appear as:select *
from dual; -
Need helping in writing query for finding percentage of duration
Can any one please help in writing query for this.
The table is like this :-
ID Region Month Duration
I1 R1 Jan 80
I2 R2 Jan 70
I3 R1 Jan 70
I4 R3 Jan 40
I5 R1 Feb 80
I6 R2 Feb 30
I7 R3 Mar 100
I want to write a query to find
% of duration for each and every region against each and every month.
Please help in solving this query. I am in urgent need of this.
Thanks in advance.I also have to do in MS Access 2003You also have to ask into an other forum since here it's an Oracle forum, to try to find Oracle solution.
Nicolas. -
Can we Definine Input Help in a Query
Hi
Is it possible to define input help or search help in a query the same way as it is done in R3?? I will like to load specific value from a psa.
RegardsHi Jacques,
Providing Customized Input help in query directly is not possible. Instead we can provide Input help in WAD layouts for a query with the help of another Query.
This can be made available for the users to choose with using controls such as Drop Down, Check boxes, Buttons and so on..
Hope this helps.
Regards.
Shafi. -
I need help with the query
Here is what I need
For a particular comm record if there is no Salary record where comm:Date = Salary:Date, then
• Find maximum dated Salary record as of comm:Date.
• Clone this record and set Salary:Date = comm:Date
• Set Salary:rate = comm:rate
Like wise for a particular Salary record If there is no comm record where Salary:Date = comm:Date then
• Find maximum effective dated comm record as of Salary:Date
• Apply Rate 2 amount from this maximum effective dated record to Salary record i.e. Set Salary:rate = comm:rate
Example
Salary Table :
ID Sal_Date Rate Hours
1 07/01/2011 400.00 40
2 02/15/2011 200.00 40
3 01/01/2011 160.00 40
Sal_comm Table:
Sal_Date comm_Rate
1 07/01/2011 10.00
4 03/01/2011 7.50
3 01/01/2011 4.00
I need to merge comm_Rate column in Salary table, since there is no salary record as off 03/01/2011, I need to find the maximum dated salary record as of 03/01/2011
i.e. the record dated 02/15/2011. Now I need to clone that salary record, set the SAL_date as 03/01/2011 and update Rate2 amount. So the record set will be like:
Sal_Date:
id sal_Date Rate Hours comm_Rate
1 07/01/2011 400.00 40 10.00
4 03/01/2011 200.00 40 7.50
2 02/15/2011 200.00 40 4.00
3 01/01/2011 160.00 40 4.00So you need all used dates as the "driving" dataset. And you need the according data for each of these.
WITH salary_table as
(select 1 id,to_date('07/01/2011','MM/DD/YYYY')sal_date,400 rate,40 hours from dual union all
select 2 id,to_date('02/15/2011','MM/DD/YYYY')sal_date,200 rate,40 hours from dual union all
select 3 id,to_date('01/01/2011','MM/DD/YYYY')sal_date,160 rate,40 hours from dual),
sal_comm as
(select 1 id,to_date('07/01/2011','MM/DD/YYYY')sal_date,10 comm_Rate from dual union all
select 4 id,to_date('03/01/2011','MM/DD/YYYY')sal_date,7.5 comm_Rate from dual union all
select 3 id,to_date('01/01/2011','MM/DD/YYYY')sal_date,4 comm_Rate from dual)
select to_char(all_dates.sal_date,'MM/DD/YYYY') sal_date,sal.rate,sal.hours,com.comm_rate
from (select sal_date from salary_table
union
select sal_date from sal_comm) all_dates
inner join (select s1.*,lead(sal_date-1,1,to_date('31/12/9999','DD/MM/YYYY')) over (order by sal_date) next_sal_date
from salary_table s1) sal
on (all_dates.sal_date between sal.sal_date and sal.next_sal_date)
inner join (select s1.*,lead(sal_date-1,1,to_date('31/12/9999','DD/MM/YYYY')) over (order by sal_date) next_sal_date
from sal_comm s1) com
on (all_dates.sal_date between com.sal_date and com.next_sal_date)
order by all_dates.sal_date desc;
SAL_DATE RATE HOURS COMM_RATE
07/01/2011 400 40 10
03/01/2011 200 40 7.5
02/15/2011 200 40 4
01/01/2011 160 40 4
-
Need urgent help with the query - Beginer
Hello - I need help with a query to populate data in a table.Here is the scenario.
Source1
MnthID BranchCod CustID SegCode FXStatus ProfStatus Profit
200712 B1 C1 20 Y Y 100
Source2
MnthID BranchCod CustID ProdCode ProdIndex
200712 B1 C1 12 1
200712 B1 C2 12 0
Destination
MnthID BranchCod SegCode ProdCode CountSegCust CountProdCust ProfitProdCust
Condition and Calculations:
1)Source1 customer are base customers.If Source2 has customers who is not in source1 then that customer's record should not be fetched.
2)SegCode, FX Status, ProfStatus is one variable in destination table. [ SegCode = SegCode+ FXStatus (if FXStatus = Y)+ ProfStatus (if FXStatus = Y) ]
3)CountSegCust = CountCustID Groupby MnthID,BranchCod,SegCode Only.
4)CountProdCust = CountCustID Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
5)ProfitProdCust = Sum of Profit of Customers Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
Apologies for bad formatting.
Thanks in advance!!A total guess indeed.
It's not clear whether some aggregation can be done (summing counts of grouped data might cause some customers being counted more than once)
insert into destination
select mnthid,branchcod,segcode,prodcode,countsegcust,countprodcust,profitprodcust
from (select s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end segcode,
s2.prodcode,
count(s1.custid) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
order by null
) countsegcust,
count(case proindex when 1
then custid
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) countprodcust,
sum(case proindex when 1
then profit
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) profitprodcust,
row_number() over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) the_row
from source1 s1,source2 s2
where s1.mnthid = s2.mnthid
and s1.branchcod = s2.branchcod
and s1.custid = s2.custid
where the_row = 1Regards
Etbin -
Need help writing a query for following scenario
Hi all, I need some help writing a query for the following case:
One Table : My_Table
Row Count: App 5000
Columns of Interest: AA and BB
Scenario: AA contains some names of which BB contains the corresponding ID. Some
names are appearing more than once with different IDs. For example,
AA BB
Dummy 10
Me 20
Me 30
Me 40
You 70
Me 50
Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
I would appreciate any input. ThanksIs it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
WITH t AS (
SELECT 'me' aa, 10 bb FROM dual
UNION ALL
SELECT 'me' aa, 20 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
SELECT DISTINCT aa, bb
FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
WHERE cnt > 1; -
Can anyone help me with Query ?
Hi Experts,
Can anyone help me with Query? the query need to retrieve below info based on set of books ID.
Needed columns:
1 SOB
2 Legal Entities
3 Fixed Asset Org Name
4 Applications/Responsibilities
5 Operating Units
6 Inventory Org
7 COA
8 Assigned Responsibilities
ThanksDuplicate post -- Can you help on below Query ?
Please post only once! -
Need Help on below Query.
Hi All,
Need Help on below Query.
Consider,
"test9" Table Data in COLUMN "Name" AS
Name
=====
'a'
'b'
'c'
'd'
'e'
I am writing a query as :
SELECT * FROM test9 WHERE Name IN ('a','b','c','d','e','f','g')
I want result set as , It should show data as -
'f'
'g'
i.e. data which does not exists in the table and which is give in in clause
Is it possible in a single query.You can put the data that is to be checked for into a table instead or an inline view, for example:
with t as
(select 'a' as c1 from dual
union all
select 'b' from dual
union all
select 'c' from dual
union all
select 'd' from dual
union all
select 'e' from dual)
select c1 from (select 'a' as c1 from dual
union all
select 'b' from dual
union all
select 'c' from dual
union all
select 'd' from dual
union all
select 'e' from dual
union all
select 'f' from dual
union all
select 'g' from dual)
minus
select c1 from t
C
f
g
2 rows selected. -
Can you help on below Query ?
Hi Experts,
Can anyone help me with Query? the query need to retrieve below info based on set of books ID.
Needed columns:
1 SOB
2 Legal Entities
3 Fixed Asset Org Name
4 Applications/Responsibilities
5 Operating Units
6 Inventory Org
7 COA
8 Assigned Responsibilities
ThanksAlthough this is not a complete query, it will give you a start
select haou.name "Name",'Legal Entity' "Type", gsb.name "Set Of Books"
from hr_all_organization_units haou,
hr_organization_information hoi,
gl_sets_of_books gsb
where haou.organization_id = hoi.organization_id
and hoi.org_information_context = 'Legal Entity Accounting'
and hoi.org_information1 = gsb.set_of_books_id
and gsb.set_of_books_id = :sob
UNION
select haou.name "Name",'Operating Unit' "Type", gsb.name "Set Of Books"
from hr_all_organization_units haou,
hr_organization_information hoi,
gl_sets_of_books gsb
where haou.organization_id = hoi.organization_id
and hoi.org_information_context = 'Operating Unit Information'
and hoi.org_information3 = gsb.set_of_books_id
and gsb.set_of_books_id = :sob
UNION
select haou.name "Name",'Inventory Org' "Type", gsb.name "Set Of Books"
from hr_all_organization_units haou,
hr_organization_information hoi,
gl_sets_of_books gsb
where haou.organization_id = hoi.organization_id
and hoi.org_information_context = 'Accounting Information'
and hoi.org_information1 = gsb.set_of_books_id
and gsb.set_of_books_id = :sob
UNION
select fifs.id_flex_structure_name "Name",'Chart Of Account' "Type", gsb.name "Set Of Books"
from fnd_id_flex_structures_vl fifs,
fnd_id_flexs fif,
gl_sets_of_books gsb
where fif.application_id = fifs.application_id
and fif.id_flex_name = 'Accounting Flexfield'
and fifs.id_flex_num = gsb.chart_of_accounts_id
and gsb.set_of_books_id = :sobOther tables which would help you are fnd_application_tl and fnd_responsibility_tl
HTH -
Help build a query !!!
There are two tables. One consists of members with expenses and next with fixed percentile. I need to build a single query based upon following
1. Need to know the number of members that fall under a fixed range of percent like 10%, 40%, 50%. Suppose, total no of members = 50 then
10% -> 5
40% -> 20
50% -> 25
2. Now I need to rank the members as per their expenses. So, if following be the expenses of the members sorted by expenses
A1 100
A2 99
A3 98
A4 97
A5 96
A6 95
A50 50
then
total expense should be found out as
10% [sum of top 5 : From A1 to A5] = 100 + 99 + 98 + 97 + 96 = 490
40% [sum of next 20: From A6 to A26]
50% [sum of next 25: From A27 to A50]
Please help !!!!!
Edited by: josleen on Jan 11, 2010 7:50 AMHi,
There are a number of analytic functions for dealing with percentiles.
Depending on how you want to handle ties, how you count duplicate values, and other factors, PERCENT_RANK might be best for this problem.
You really should post the information Centinul requested whenever you have a question.
If you can phrase your question in terms of commonly available tables, then you don't have to post them.
For example, to group employees from the scott.emp table according to their salaries, and get a total of salaries by group, you could create a groups table and join it to the emp table, like this:
CREATE TABLE groups
( seq_id NUMBER
, label VARCHAR2 (20)
, low_pct NUMBER -- lowest number IN this group
, high_pct NUMBER -- lowest number NOT IN this group, but in the next highest group
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (1, 'Top 10%', 0, 10);
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (2, '10%-50%', 10, 50);
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (3, 'Bottom 50%', 50, 101);
COMMIT;
WITH got_pct AS
SELECT ename
, sal
, 100 * PERCENT_RANK () OVER ( ORDER BY sal DESC
, empno
) AS pct
FROM scott.emp
SELECT g.label
, SUM (p.sal) AS group_total
FROM got_pct p
JOIN groups g ON p.pct >= g.low_pct
AND p.pct < g.high_pct
GROUP BY g.label
ORDER BY MIN (g.seq_id)
;Output:
LABEL GROUP_TOTAL
Top 10% 8000
10%-50% 12875
Bottom 50% 8150If you need help understanding a query with sub-queries, it helps to run the sub-query by iteself.
In this case:
WITH got_pct AS
SELECT ename
, sal
, 100 * PERCENT_RANK () OVER ( ORDER BY sal DESC
, empno
) AS pct
FROM scott.emp
SELECT *
FROM got_pct
;produces this output:
ENAME SAL PCT
KING 5000 0
SCOTT 3000 7.69230769
FORD 3000 15.3846154
JONES 2975 23.0769231
BLAKE 2850 30.7692308
CLARK 2450 38.4615385
ALLEN 1600 46.1538462
TURNER 1500 53.8461538
MILLER 1300 61.5384615
WARD 1250 69.2307692
MARTIN 1250 76.9230769
ADAMS 1100 84.6153846
JAMES 950 92.3076923
SMITH 800 100In case of a tie, PERCENT_RANK assigns the same result to all rows with the same input value. The reason I included empno in the analytic ORDER BY clause was to prevent ties. Otherwise, both SCOTT and FORD (with the exact same sal=3000) would be assigned pct=7.69230769, and the "Top 10%" would then include 3 out of 14 rows (over 21% of the rows), which, outside of Lake Woebegone, shouldn't happen.
Edited by: Frank Kulash on Jan 11, 2010 11:02 AM
Explanation added.
Maybe you are looking for
-
Oracle SOA Suite 10.1.3.4 Polling database table with Database Adapter
We are trying to use the database adapter to poll for new and changed data in a database. To do that we try to use a sequencing table on an external database. But for that we need to specify the data-source name for the external sequencing table. Whe
-
Can u transfer files via airdrop from a iphone 5 to a mac and vice a versa
hi, can u transfer files via air drop from an iphone 5 to a mac air, and mac air to a iphone 5.
-
Hi There- Can anybody advise me on how to get iTunes to recognize my iPad when I hook it up to my computer. Since upgrading to iTunes 12, I can't get my iPad to connect and sync. Thanks for you help in advance. Sincerely- Douglas Ward
-
Compiling simultaneously two classes referencing each other
Hi, When we want to compile 2 classes simultaneously in a package, we give command -- javac package-name/*.java Suppose the two classes are ClassA and ClassB and ClassA has a reference of ClassB. So the compiler will finish compiling ClassB before co
-
Migration assistant built mac makes me validate apps with password, fix?
I built an Imac using Migration Assistant. Now when I run an app it prompts me to put in the password for the account I purchased the app. The error says that this app was purchased with a different account. Then I put in the password and the app wor