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.
Similar Messages
-
Query For Finding Revised PO Amout in Purchasing in Each Revisions
Can anybody please tell me how
can i write a query for Extracting
individual PO amounts in each revisions of a
PO from PO_LINES_ARCHIEVE_ALL..
ie FOR Each revisions of a PO What was the total PO Amount
Regards
BinuHi Geoff,
You can use the ROW_NUMBER and CASE Functions to acheive this. Here with , i have give u a small idea with an ex:
Qry :
B.ItemCode,SUM(B.PO1)[PO1],SUM(B.POQty1)[POQty1],SUM(B.PO2)[PO2],SUM(B.POQty2)[POQty2] From
select Itemcode
CASE when Sno=1 Then DocNum End [PO1]
CASE When Sno=1 Then Qty End [POQty1]
CASE when Sno=2 Then DocNum End [PO2]
CASE When Sno=2 Then Qty End [POQty2]
From(
Row_Number() Over (Order By Opor.Docnum) [Sno], Opor.DocNum,POr1.ItemCode,sum(POr1.Quantity)[Qty] from OPOR join Por1 on Opor.DocEntry=por1.DocEntry
ItemCode='20002' group by oPor.DocNum,por1.ItemCode
A where A.Sno<=2)B Group By B.ItemCode
Result :-
ItemCode PO1 POQty1 PO2 POQty2
20002 18 1.000000 19 10.000000
Thanks,
Karthikeyan.P -
Write a query for finding STDDEV for OLAP Cube
Can anybody post a sample query which will find the standard deviation of sales for each month in the sample GLOBAL OLAP schema for Oracle 11g.
The OLAP option automatically generates a set of relational views on cubes, dimensions, and hierarchies in Oracle 11g. So how can i write a query for finding the Standard deviation on these views.The easiest way to do this is to do this directly within the AW using a custom calculated measure. In AWM11g you can use the 11g custom measure wrapper to execute the STDDEV function:
olap_dml_expression('function',data type)
For example
olap_dml_expression('STDDEV(cube_name, time_dimension_name)',number)
You can then simply expose this calculated measure as another column in your SQL View and then there is no need to use the SQL equivalent. This means the calculation is performed inside the AW (which is as close to the source data as you can get) ensuring performance of the whole query remains high.
If necessary you can take this a step further, if required, and wrap the STDDEV calculation within an OLAP DML program that would allow you to manipulate the status of time within the calculation. For example, you may want the STDDEV calc to only take into account the last 12 time periods rather than all time periods currently in status.
Hope this helps
Keith Laker
Oracle EMEA Consulting
OLAP Blog: http://oracleOLAP.blogspot.com/
OLAP Wiki: http://wiki.oracle.com/page/Oracle+OLAP+Option
DM Blog: http://oracledmt.blogspot.com/
OWB Blog : http://blogs.oracle.com/warehousebuilder/
OWB Wiki : http://wiki.oracle.com/page/Oracle+Warehouse+Builder
DW on OTN : http://www.oracle.com/technology/products/bi/db/11g/index.html -
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. -
Query to find employee annual salary or pay rate for a pay period (Bi-Week)
Hi Guru's,
need your help for finding annual salary or pay rate for pay period for an employee.
I have below query returning some thousand line which I cant understand. Can someone correct the query please.
SELECT ppp.proposed_salary_n salary From per_pay_proposals ppp,apps.per_all_people_f papf,apps.per_all_assignments_f paaf,apps.per_all_people_f papf1 WHERE papf.person_id = paaf.person_id AND paaf.primary_flag = 'Y' AND paaf.assignment_type = 'E' AND paaf.supervisor_id = papf1.person_id AND papf1.current_employee_flag = 'Y'AND papf.business_group_id = paaf.business_group_id AND SYSDATE BETWEEN papf.effective_start_date and papf.effective_end_date AND SYSDATE BETWEEN paaf.effective_start_date AND paaf.effective_end_date AND SYSDATE BETWEEN papf1.effective_start_date AND papf1.effective_end_date AND papf.employee_number='1234';
SALARY
17.4346
16.0846
17.4346
13.78
13.78
15.07
13.78
13.78
13.78
3305.59
14.859
SALARY
5507.25
2731.01
2690.51
13.78
13.35
13.35
1960
4192
17
4927
2525.02
SALARY
2652
13.35
15.07
2686.67
2964
13.78
17.4635
20.4981
16.0846
13.78
17.4635
SALARY
2666.68
13.78
15.07
13.78
16.0846
17.4635
4353.99
4562.51
17.4346
16.0846
6727.41
SALARY
2780.99
20
17.4346
16.0846
2970
2315.01
17.4635
2629.85
14.5705
5635
17.4346
Thanks
SandeepI used table hrpy_rgdir to get sequence number for the given pernr, payroll area and dates.
Using the sequence number, I am calling Function PYXX_READ_PAYROLL_RESULT to extract the payroll results.
SELECT * INTO CORRESPONDING FIELDS OF TABLE it_hrpy_rgdir
FROM hrpy_rgdir
WHERE srtza = 'A'
AND paydt IN sopaydt
AND abkrs IN soabkrs
AND pernr IN sopernr.
LOOP AT it_hrpy_rgdir.
PERFORM get_rt USING it_hrpy_rgdir-pernr it_hrpy_rgdir-seqnr.
ENDLOOP.
*& Form get_rt
* text
* -->PERNR text
* -->SEQNR text
FORM get_rt USING pernr seqnr.
CLEAR: it_payroll_result.
CALL FUNCTION 'PYXX_READ_PAYROLL_RESULT'
EXPORTING
clusterid = 'RQ'
employeenumber = pernr
sequencenumber = seqnr
CHANGING
payroll_result = it_payroll_result
EXCEPTIONS
illegal_isocode_or_clusterid = 1
error_generating_import = 2
import_mismatch_error = 3
subpool_dir_full = 4
no_read_authority = 5
no_record_found = 6
versions_do_not_match = 7
error_reading_archive = 8
error_reading_relid = 9
OTHERS = 10.
ENDFORM.
For any period, the function module just returns sy-subrc = 6 [no_record_found].
We are on Australian Payroll. Could you please let me know what I might be missing or is there an alternative way of getting the information ?
Thank you -
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. -
Savitzky golay filter for finding second derivative
Hello friends,
I want to find second derivative using Savitzky golay filter for the data as shown in image.
Three different spacings are used.
Region AB and EF= ∆f1
Region BC and DE=∆f2
Region CD=∆f3
Can some one help me to understand to to decide side points and polynomial order for my input data.?
Thanks in advance.The polynomial order must be at least one larger than the highest derivative. So a third order polynomial is required for a second deriviative. In general it is better to keep the polynomial order as low as possible while getting a good fit to the data.
The number of side points is somewhat arbitrary and depends on how much smoothing is needed. Often some experimentation and judgement is required.
Have you considered resampling the data to create a new dataset with uniform sampling throughout? That might be easier than trying to match derivatives at the transition points.There are several resampling VIs available.
How many data points do you have in each segment?
Lynn -
GREP query for find/change operation
Hi everyone,
In the following line, there's a comma after the first name. This type of formatting is repeated hundreds of times on a number of pages so I need to use a GREP find/change query to remove the comma after the first name and replace it with a space.
Firstname,Lastname,State
I wondered if something could tell me what the GREP code would be?
Appreciate any assistance.function(){return A.apply(null,[this].concat($A(arguments)))}
.. it's also doing it for the second comma
That's impossible. You must have entered something else than Scott's code. -
Query For Finding Yearly Opening and Closing Balance for All the Items
Hi Experts,
I am working on Query Based Report for finding the Yearly Opening and Closing Stock for all the Items
i will give yearwise selection and I want opening and closing stock in between that years
Warm Regards,
Sandip Kokate
Edited by: Sandipk on May 20, 2011 1:58 PMHi,
Declare @SDate DateTime
Declare @EDate DateTime
Declare @Whse nvarchar(10)
Set @SDate= (SELECT min(F_RefDate) FROM OFPR T1 where T1.[Name] ='[1%]' )
Set @EDate= (SELECT max(T_RefDate) FROM OFPR T1 where T1.[Name] ='[%1]' )
Set @Whse=(Select Max(s2.Warehouse) from OINM S2 Where S2.Warehouse = '[%2]')
BEGIN
Select @Whse as 'Warehouse', a.Itemcode, max(a.Dscription) as ItemName,
sum(a.OpeningBalance) as OpeningBalance, sum(a.INq) as 'IN', sum(a.OUT) as OUT,
((sum(a.OpeningBalance) + sum(a.INq)) - Sum(a.OUT)) as Closing ,
(Select i.InvntryUom from OITM i where i.ItemCode=a.Itemcode) as UOM
from( Select N1.Warehouse, N1.Itemcode, N1.Dscription, (sum(N1.inqty)-sum(n1.outqty))
as OpeningBalance, 0 as INq, 0 as OUT From dbo.OINM N1
Where N1.DocDate < @SDate and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,
N1.Dscription Union All select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance,
sum(N1.inqty) , 0 as OUT From dbo.OINM N1 Where N1.DocDate >= @SDate and N1.DocDate <= @EDate
and N1.Inqty >0 and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,N1.Dscription
Union All select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance, 0 , sum(N1.outqty) as OUT
From dbo.OINM N1 Where N1.DocDate >= @SDate and N1.DocDate <=@EDate and N1.OutQty > 0
and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,N1.Dscription) a, dbo.OITM I1
where a.ItemCode=I1.ItemCode
Group By a.Itemcode Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0 Order By a.Itemcode
END
I hope this will work for you.
In above query you can also user OFPR.Code, OFPR, Category OFPR.Indicator instead of OFPR.Name.
Regards
Vaibhav Anharwadkar
Edited by: Vaibhav Ancharwadkar on May 24, 2011 9:23 AM -
Query for finding data during special holidays
Hi, i have a table for special holidays that looks like this:
PROFILE_DAY
VAR_DATE
REGULAR_HOLIDAY
1/1/2013
H_WEEK_THURSDAY
3/28/2013
H_WEEK_FRIDAY
3/29/2013
REGULAR_HOLIDAY
12/24/2013
REGULAR_HOLIDAY
12/25/2013
REGULAR_HOLIDAY
12/31/2013
And another table (LOAD_PROFILE_TEST), which contains LOAD_PROF1 values from (TIME_EQ) 0 to 24 at intervals 0.25 for (PROFILE_DAY) MONDAY to SUNDAY including REGULAR_HOLIDAY, H_WEEK_THURSDAY and H_WEEK_FRIDAY. All in all, this table contains 970 records (97 records between 0 to 24 with interval of 0.25 per PROFILE_DAY, with 10 distinct PROFILE_DAY).
TIME_EQ
PROFILE_DAY
LOAD_PROF1
LOAD_PROF2
0
REGULAR_HOLIDAY
11.47
0.25
REGULAR_HOLIDAY
11.27
0.5
REGULAR_HOLIDAY
11.3
0.75
REGULAR_HOLIDAY
11.08
0
MONDAY
11.27
0.25
MONDAY
11.33
0.5
MONDAY
11.18
Now, I have this query to update value of LOAD_PROF2 of the said table whenever parameters V_DATE_OUT & V_DATE_IN is entered:
UPDATE LOAD_PROFILE_TEST
SET LOAD_PROF2 = LOAD_PROF1 + :LOAD_DIFF
WHERE UPPER(PROFILE_DAY) IN (select UPPER(to_char(:V_DATE_OUT + (level-1), 'fmDAY'))
from dual
connect by level <= :V_DATE_IN - :V_DATE_OUT + 1
where :LOAD_DIFF is a certain pre-determined value.
This query works fine if i am trying to update regular days from MONDAY to SUNDAY. What i would like to do is to determine if the two parameter dates, V_DATE_OUT & V_DATE_IN would fall under any of the holidays on the first table LOAD_PROFILE_TEST, then update only those rows. For example, V_DATE_OUT = 12/02/2013, Monday and V_DATE_IN = 12/06/2013, Friday. The query above would update the values of LOAD_PROF2 for PROFILE_DAY - MONDAY to FRIDAY, corresponding to dates 12/02/2013 and 12/06/2013. If however, V_DATE_OUT = 12/23/2013, Monday and V_DATE_IN = 12/27/2013, Friday, this should update the rows corresponding to PROFILE_DAY - MONDAY for 12/23/2013, THURSDAY for 12/26/2013, FRIDAY for 12/27/2013, and REGULAR_HOLIDAY for the dates 12/24/2013 and 12/25/2013 since these two are included in the first table (table of holidays). This same scenario will work the same way when V_DATE_OUT and/or V_DATE_IN fall in the dates 3/28/2013 and 3/29/2013. All other dates not included in the table for special holidays will be treated according to the day they fall on (Monday thru Sunday). I hope my point is clear. Thank you in advance.Thanks for your reply. Firstly, I am using Forms [32 Bit] Version 10.1.2.0.2 (Production). I will try to explain a little further but I don't know if i can give the exact details of the tables since this would be voluminous. First i have a table REG_HOLIDAYS, whcih contain the following:
PROFILE_DAY
VAR_DATE
REGULAR_HOLIDAY
1/1/2013
H_WEEK_THURSDAY
3/28/2013
H_WEEK_FRIDAY
3/29/2013
REGULAR_HOLIDAY
12/24/2013
REGULAR_HOLIDAY
12/31/2013
REGULAR_HOLIDAY
12/25/2013
Now i have another table LOAD_PROFILE_TEST which i will simplify just to show what i wanted to do:
CREATE
TABLE LOAD_PROFILE_TEST
TIME_EQ
NUMBER (5, 2),
PROFILE_DAY
VARCHAR2 (15 BYTE),
LOAD_PROF1
NUMBER (6, 2),
LOAD_PROF2
NUMBER (6, 2)
Here are the sample values of this table:
TIME_EQ
PROFILE_DAY
LOAD_PROF1
LOAD_PROF2
0
Monday
2
0
Tuesday
2.1
0
Wednesday
2.3
0
Thursday
2.5
0
Friday
2.2
0
Saturday
2.4
0
Sunday
2.3
0
Regular_holiday
1.9
0.25
Monday
2.1
0.25
Tuesday
2.1
0.25
Wednesday
2.4
0.25
Thursday
2.2
0.25
Friday
2.5
0.25
Saturday
2.3
0.25
Sunday
2.3
0.25
Regular_holiday
2.5
However, in the actual table, TIME_EQ will start with 0 until 24 with interval of 0.25 (i.e 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, ...23.5, 23.75, 24). So for a PROFILE_DAY of 'Monday', there will be 97 rows corresponding to TIME_EQ of 0 to 24. The same is true for Tuesday, Wednesday until Sunday including Regular_holiday. All in all, this table would contain 776 rows. LOAD_PROF1 values are random values initally inputted with PROFILE_DAY and TIME_EQ.
The first goal is to UPDATE this table (LOAD_PROFILE_TEST) by updating the column LOAD_PROF2 by adding a certain parameter value, :LOAD_DIFF, say 0.1. Now, i will have two scenarios to show what i would like to happen. First, i have two date parameters :V_DATE_OUT and :V_DATE_IN.
First Case, V_DATE_OUT = 12/15/2013 (Monday); V_DATE_IN = 12/22/2013 (Sunday)
I will have this query to update said table:
UPDATE LOAD_PROFILE_TEST
SET LOAD_PROF2 = LOAD_PROF1 + :LOAD_DIFF
WHERE UPPER(PROFILE_DAY) IN (select UPPER(
to_char(
:V_DATE_OUT + (level-1),
'fmDAY'))
from dual
connect by level <=
:V_DATE_IN - :V_DATE_OUT + 1);
The output of this query would be:
TIME_EQ
PROFILE_DAY
LOAD_PROF1
LOAD_PROF2
0
Monday
2
2.1
0
Tuesday
2.1
2.2
0
Wednesday
2.3
2.4
0
Thursday
2.5
2.6
0
Friday
2.2
2.3
0
Saturday
2.4
2.5
0
Sunday
2.3
2.4
0
Regular_holiday
1.9
0.25
Monday
2.1
2.2
0.25
Tuesday
2.1
2.2
0.25
Wednesday
2.4
2.5
0.25
Thursday
2.2
2.3
0.25
Friday
2.5
2.6
0.25
Saturday
2.3
2.4
0.25
Sunday
2.3
2.4
0.25
Regular_holiday
2.5
Since, 12/15/2013 up to 12/22/2013 is from Monday to Sunday without having a particular day included in the first table, REG_HOLIDAYS, therefore all the rows with Monday to Sunday are updated.
Second Case, V_DATE_OUT = 12/23/2013 (Monday); V_DATE_IN = 12/29/2013 (Sunday)
Take note that 12/24 and 12/25 are included in the first table, REG_HOLIDAYS, therefore i need a query so that this would be my output afterwards:
TIME_EQ
PROFILE_DAY
LOAD_PROF1
LOAD_PROF2
0
Monday
2
2.1
0
Tuesday
2.1
0
Wednesday
2.3
0
Thursday
2.5
2.6
0
Friday
2.2
2.3
0
Saturday
2.4
2.5
0
Sunday
2.3
2.4
0
Regular_holiday
1.9
2.0
0.25
Monday
2.1
2.2
0.25
Tuesday
2.1
0.25
Wednesday
2.4
0.25
Thursday
2.2
2.3
0.25
Friday
2.5
2.6
0.25
Saturday
2.3
2.4
0.25
Sunday
2.3
2.4
0.25
Regular_holiday
2.5
2.6
As can be seen, since 12/23/2013 up to 12/29/2013 is from Monday to Sunday, rows with Monday upto Sunday should be updated, HOWEVER, since 12/24 and 12/25 which falls on a Tuesday and a Wednesday also were declared as holidays (included in the REG_HOLIDAYS table), instead of updating rows with PROFILE_DAY of Tuesday and Wednesday, the query should instead update rows with Regular_holiday as PROFILE_DAY and leave as is the rows with Tuesday and Wednesday. Additional note, days declared in the REG_HOLIDAYS table with PROFILE_DAY of Regular_holidays are treated as DISTINCT as in the case of 12/24 and 12/25.
The query you gave me gives the correct value if the involved days between V_DATE_OUT and V_DATE_IN is from Monday to Sunday only, otherwise it always gives Regular_holiday only regardless of the other dates queried. As in the first case above, it would give Monday to Sunday (correct) but for the second case, it will only give Regular_holiday, the other days (Monday, Thursday, Friday, Saturday and Sunday) was not outputted.
I hope this became clearer since i will still be needing this for another query which i will inquire again after resolving this issue. Thanks a lot for your help. -
Recursive query for finding parents and children against two tables
Hi I have two tables where the data is stored hierarchially.
I have found using connect_by, level and other oracle functions doing its job when the data is in one table.
Can I traverse against two tables by using the same oracle functions.
My Table A can go 2 levels deep in my hierarchy and level 3 and so forth has to be done against a different table.
Appreciate your insight..
Thanks in advanceThe number of levels that I can traverse in the first table is dynamic (sometimes 2 levels and sometimes 3 levels and actually no level limit) and then has to dynamically start looking at second table for finding any more levels in the other table.
I was able to implement them seperately and union them.
I was more looking for a comprehensive solution to reduce the number of lines that I have to maintain...
Thanks in advance
Edited by: user566193 on Jan 15, 2010 1:58 PM -
Query for Finding the Daily Cumulative Production Total
Hi Experts,
I want Query based Report for finding Daily Cumulative i.e Running Production Total
Suppose Yesterdays Production for Item A0001 is 20 and Todays Prodction is 20 then it will show 50 but it shout be datewise selection.
Warm Regards,
Sandip KokateHello
Report Scenario as follow
Item Code Item name Todays Producion Yesterdays Production Total Productio
A001 Computer 20 20 40
A002 Mouse 10 05 15
A003 CPU 15 05 20
This is Exact Report
Regards,
Sandip Kokate -
Query to find Concurrent requests executed on each db node in RAC from in last 24 Hours
Hi
Could you please help me in getting the query to find total number of concurrent requests executed on each db node in Two DB Node RAC with PCP ( Two CM Nodes) in last 24 hours.c2670397-8171-480e-b9f8-8874e77ee0b4 wrote:
Hi
Could you please help me in getting the query to find total number of concurrent requests executed on each db node in Two DB Node RAC with PCP ( Two CM Nodes) in last 24 hours.
Query FND_CONCURRENT_REQUESTS table (ACTUAL_START_DATE and ACTUAL_COMPLETION_DATE columns) -- http://etrm.oracle.com/pls/et1211d9/etrm_pnav.show_object?c_name=FND_CONCURRENT_REQUESTS&c_owner=APPLSYS&c_type=TABLE
https://community.oracle.com/message/10780262#10780262
https://community.oracle.com/message/10589420#10589420
Thanks,
Hussein -
3rd largest salary in each department.
Hi all,
I need to find the third largest salary in each dept in the emp table.
Can anyone give me the query.
regards,
premSQL> select empno, ename, deptno, funny_requirement_salary from
2 (select empno, ename, deptno, rank() over (partition by deptno order by sal DESC) rn, sal funny_requirement_salary from emp)
3 where rn = 3
4 /
EMPNO ENAME DEPTNO FUNNY_REQUIREMENT_SALARY
7934 MILLER 10 1300
7566 JONES 20 2975
7844 TURNER 30 1500 -
Query to find out two counts on each column
Hi
can you help for the below query.
I wanted to find out two count values on each column, ie above or equal 5 count and below 5 count.
Table:
Q1 Q2 Q3 (Q1, Q2, Q3 are columns in a table.)
1 3 5
6 7 4
8 5 1
2 8 5
Query should display, like below.
Expected Result:
colname >=5 count <5 count
Q1 2 2
Q1 3 1
Q3 2 2
Thanks in advance.The query itself can be simplified as follows:
SQL> with t as (
2 select level lvl from dual
3 connect by level < 4)
4 select colname
5 , sum(case when val >= 5 then 1 else 0 end) "count >= 5"
6 , sum(case when val < 5 then 1 else 0 end) "count < 5"
7 from (
8 select decode (lvl, 1, 'Q1', 2, 'Q2', 'Q3') colname
9 , decode (lvl, 1, q1, 2, q2, q3) val
10 , lvl
11 from (select q1, q2, q3
12 from q),
13 t)
14 group by colname, lvl
15 order by lvl;Lines 8-10 is what pivots the cartesian join (CJ) of q & t. It helps to explain it by breaking down each part.
The CJ produces (I added the lvl into the output below for explanation purposes):
SQL> with t as (
2 select level lvl from dual
3 connect by level < 4)
4 select q1, q2, q3, lvl
5 from q,
6 t;
Q1 Q2 Q3 LVL
1 3 5 1 <-- line a1
6 7 4 1 <-- line a2
8 5 1 1
2 8 5 1
1 3 5 2 <-- line a5
6 7 4 2
8 5 1 2
2 8 5 2
1 3 5 3
6 7 4 3
8 5 1 3
2 8 5 3
12 rows selected.and the CJ from above now allows us to pivot the results using the LVL column as the "decoding" key.
SQL> with t as (
2 select level lvl from dual
3 connect by level < 4)
4 select decode (lvl, 1, 'Q1', 2, 'Q2', 'Q3') colname
5 , decode (lvl, 1, q1, 2, q2, q3) val
6 , lvl
7 from (select q1, q2, q3
8 from q),
9 t;
COLNAME VAL LVL
Q1 1 1 <-- line b1
Q1 6 1 <-- line b2
Q1 8 1
Q1 2 1
Q2 3 2 <-- line b5
Q2 7 2
Q2 5 2
Q2 8 2
Q3 5 3
Q3 4 3
Q3 1 3
Q3 5 3
12 rows selected.Line b1:
The decodes hit line a1 of the CJ and since lvl = 1, we get colname=Q1 and the corresponding Q1 row value (val=1) of line a1 and finally the value of 1 for lvl.
Line b2:
For decode of line a2 of the CJ, lvl = 1 again and we get colname=Q1 and the corresponding Q1 row value (val=6) of line a2 and finally the value of 1 for lvl.
Line b5:
For decode of line a5 of the CJ, lvl = 2 and we get colname=Q2 and the corresponding Q2 row value (val=3) of line a5 and finally the value of 2 for lvl.
The other rows of the pivoted result set above are obtained similarly and from this result we can now perform the aggregation for the final result.
The WITH query is a called subquery factoring. It allows you to reference the subquery multiple places in the query.
HTH
Maybe you are looking for
-
Song updating problems!!!!! PLEASE HELP!
I just restored my video ipod and not it is saying that my ipod has a corrupt file and it won't let me update my playlist! Please help! Should i send it in?
-
Is it possible to print one document to two printers?
I need to print one document to two printers at the same time. Can I accomplish this with printer pools in OSX Server? I've found several 3rd party vendors for Windows, but nothing for the Mac. Any help would be great! Thanks! Josh
-
Next-generation plug-in 1.6 : ClassNotFoundException and not caching JARs
Hi, Since the introduction of the next-generation plug-in in Java 1.6.0_10, the applet I have used and developed for many years don't load anymore. The situation is this : the Jars of this applet are embedded in a Lotus Notes database as Java resourc
-
A question about backend of creating the app with Xcode
Hi, I'm a new iphone developer who just started to learn how to create iphone apps. I just started to learn about view-based applications. When I select view-based applications, XCode will automatically generate some files for me. For example, if a c
-
How can I see times in 15 minute increments in day and week views in lightning?
I have many events that are less than an hour in length. Is thre any way to have lightning show times on the left side in 15 minute intervals instead of 60?