Ordering a Group By query

I finally have a query that's properly doing the GROUP BY. Now I want to take those contents and apply a sort to them. Unfortunately, when I tried to add an ORDER BY, it said that it wasn't a GROUP BY something. So, I tried to do an outside SELECT from those contents and order those, but that went awry. So, I need help.
Here's the query that I'm trying to sort.
SELECT g.proposal_id,g.end_date,g.title,
g.funding_agency,g.status,g.id,
p.id,p.fname,p.lname,p.link,p.status AS people_status,
SUM(g.funding) AS project_funding,
MIN(g.start_date) AS start_date
FROM grants_contracts g, people p
WHERE g.id = p.id
AND g.end_date >= SYSDATE
AND g.status = 'current'
GROUP BY g.proposal_id,g.end_date,g.title,
g.funding_agency,g.status,g.id,
p.id,p.fname,p.lname,p.link,p.status
thanks!

It looks like you made a variable of each column selection. I only wanted to try one, so I variabled g.title into g_title, and also included g.title, just to make the code that relies on it work for now. Still, I received "column ambigously identified".
select * from ( <!------>
                                   SELECT g.proposal_id,g.title g_title,g.title,
                                             g.funding_agency,g.status,g.id,
                                             p.id,p.fname,p.lname,p.link,p.status AS people_status,SUM(g.funding) AS project_funding,
                                             MIN(g.start_date) AS start_date
                                        FROM grants_contracts g, people p
                                        WHERE g.id = p.id
                                             AND g.end_date >= SYSDATE
                                             AND g.status = 'current'
                                   GROUP BY g.proposal_id,g.title,
                                                  g.funding_agency,g.status,g.id,
                                                  p.id,p.fname,p.lname,p.link,p.status
                                        order by g_title
I left off the semicolon because when I added it, I received "illegal character".

Similar Messages

  • Group by order by in same query

    Hi,
    I am wondering whether it is possible to use group by and order by in same query
    For example if i have a table like the one below
    Col1 Col2 col3
    C 36 2
    A 25 5
    B 12 8
    A 25 6
    B 12 9
    C 36 1
    A 25 7
    I need a result like below
    A 25 5
    A 25 6
    A 25 7
    B 12 8
    B 12 9
    C 36 1
    C 36 2
    can the select statement with group by and order by solve this? How?

    Yes you can - though I am not sure that it is what you want. In your example you do not need to group the data, you just need to order by the first column, then the third. If required, you could also throw in the second column;
    SQL> with t as  ( 
       select 'C' col1, 36 col2, 2 col3 from dual union all 
       select 'A', 25 ,5 from dual union all 
       select 'B', 12 ,8 from dual union all 
       select 'A', 25 ,6 from dual union all 
       select 'B', 12 ,9 from dual union all 
       select 'C', 36 ,1 from dual union all   
       select 'A', 25 ,7 from dual)  
    select col1,col2,col3  
    from t
    order by col1,col3
    COL1       COL2       COL3
    A            25          5
    A            25          6
    A            25          7
    B            12          8
    B            12          9
    C            36          1
    C            36          2

  • How I check in group by query the group change

    How I check in group by query the group change
    Hi master
    Sir I have master detail table
    This is my query
    select rownum,chartofacc.accid,title,nvl(drbal,0),nvl(crbal,0),
    (select case when nvl(sum(debit),0)-nvl(sum(credit),0)>0 then
    nvl(sum(debit),0)-nvl(sum(credit),0)
    else
    0
    end mfadrttt
    from voudetail where voudetail.accid=chartofacc.accid) as mfadr,
    (select case when nvl(sum(credit),0)-nvl(sum(debit),0)>0 then
    nvl(sum(credit),0)-nvl(sum(debit),0)
    else
    0
    end mfacrttt
    from voudetail where voudetail.accid=chartofacc.accid) as mfacr
    ,nvl(debit,0),nvl(credit,0),voumaster.entdate,voumaster.vno from chartofacc ,accbal,voudetail,voumaster where chartofacc.accid=accbal.accid(+) and chartofacc.accid=voudetail.accid(+) and voumaster.vno=voudetail.vno order by chartofacc.accid,voumaster.entdate,voudetail.VNO;
    Sir I need add opbal from master section to debit in detail section when new group start only after adding I use that column for accumulative total or running balance
    If I get any method when group change system give me any key or indication then I use
    Please give me idea in both field oracle sql and oracle report 6i
    Thank
    aamir

    Hi,
    Please send tables structures and sample data from that tables. And, of course what should be the output. :) Just sending your query won't help us to find a solution.
    Peter D.

  • "median count" in a single group by query

    Hallo following problem:
    i use a single group by query to analyze a data table.
    like "select avg(parameter1), sum(parameter2), count(case when...end) from datatable where ....".
    Following problem: I want a median of a count without rewriting the query completely, accessing the table several times or something.
    Easy example - in Detail:
    Create table and fill with example data
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> CREATE TABLE datatable
      2  (
      3    SLOT NUMBER
      4  , DATA NUMBER
      5  );
    Table created.
    SQL> INSERT INTO datatable VALUES (1,1);
    1 row created.
    SQL> INSERT INTO datatable VALUES (1,2);
    1 row created.
    SQL> INSERT INTO datatable VALUES (1,3);
    1 row created.
    SQL> INSERT INTO datatable VALUES (1,4);
    1 row created.
    SQL> INSERT INTO datatable VALUES (1,5);
    1 row created.
    SQL> INSERT INTO datatable VALUES (2,1);
    1 row created.
    SQL> INSERT INTO datatable VALUES (3,1);
    1 row created.
    SQL> INSERT INTO datatable VALUES (3,2);
    1 row created.
    SQL> INSERT INTO datatable VALUES (3,3);
    1 row created.
    SQL> INSERT INTO datatable VALUES (3,4);
    1 row created.
    SQL> INSERT INTO datatable VALUES (3,5);
    1 row created.
    SQL> INSERT INTO datatable VALUES (4,1);
    1 row created.
    SQL> INSERT INTO datatable VALUES (4,2);
    1 row created.
    SQL> INSERT INTO datatable VALUES (4,3);
    1 row created.
    SQL> INSERT INTO datatable VALUES (4,4);
    1 row created.
    SQL> INSERT INTO datatable VALUES (4,5);
    1 row created.
    SQL> INSERT INTO datatable VALUES (5,1);
    1 row created.
    SQL> INSERT INTO datatable VALUES (5,2);
    1 row created.
    SQL> INSERT INTO datatable VALUES (5,3);
    1 row created.
    SQL> INSERT INTO datatable VALUES (5,4);
    1 row created.
    SQL> INSERT INTO datatable VALUES (5,5);
    1 row created.In the table there are several items (here slots) with a for each slot unique data-value.
    I want to have the median count of data values, to filter out slots with more or less values.
    this worked, until there where only slots with less data:
    SQL> SELECT FLOOR(COUNT(DISTINCT SLOT||DATA)/COUNT(DISTINCT DATA)) FROM datatabl
    e;
    FLOOR(COUNT(DISTINCTSLOT||DATA)/COUNT(DISTINCTDATA))
                                                       4but when there is a slot having more data - it won't work, the very simple and stupid calculation will give a too little value
    SQL>
    SQL> INSERT INTO datatable VALUES (4,6);
    1 row created.
    SQL>
    SQL> SELECT FLOOR(COUNT(DISTINCT SLOT||DATA)/COUNT(DISTINCT DATA)) FROM datatabl
    e;
    FLOOR(COUNT(DISTINCTSLOT||DATA)/COUNT(DISTINCTDATA))
                                                       3so what i would need is this:
    SQL>
    SQL> SELECT MEDIAN(count(DISTINCT SLOT) over (partition by DATA)) FROM datatable
    SELECT MEDIAN(count(DISTINCT SLOT) over (partition by DATA)) FROM datatable
    ERROR at line 1:
    ORA-30483: window  functions are not allowed hereIn detail:
    the count delivers the distinct count of slots for each data (possible duplicated entrys should not be counted)
    And I want the median, which should be 4.
    SQL> SELECT count(DISTINCT SLOT) over (partition by DATA) FROM datatable;
    COUNT(DISTINCTSLOT)OVER(PARTITIONBYDATA)
                                           5
                                           5
                                           5
                                           5
                                           5
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
                                           4
    COUNT(DISTINCTSLOT)OVER(PARTITIONBYDATA)
                                           1
    22 rows selected.Is it -anyhow- possible to do this without rebuilding the whole query?
    Thanks a lot

    ok, an example:
      CREATE TABLE "DATATABLE"
       (     "TOOL" CHAR(5 BYTE),
         "SLOT" NUMBER,
         "LOTID" CHAR(4 BYTE),
         "STEP" VARCHAR2(44 BYTE),
         "ENDTIME" DATE,
         "PARAMETER1" NUMBER,
         "PARAMETER6" NUMBER
    Insert into DATATABLE  values ('TOOL1',-1,'LOT1','STEP1',to_timestamp('02-FEB-12 03.09.42 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',-1,'LOT1','STEP2',to_timestamp('02-FEB-12 03.18.47 PM','DD-MON-RR HH.MI.SS AM'),42,0);
    Insert into DATATABLE  values ('TOOL1',-1,'LOT1','STEP3',to_timestamp('02-FEB-12 03.09.44 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',-1,'LOT1','STEP4',to_timestamp('02-FEB-12 02.20.38 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',-1,'LOT1','STEP5',to_timestamp('02-FEB-12 02.20.35 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT1','STEP1',to_timestamp('02-FEB-12 01.51.28 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT1','STEP2',to_timestamp('02-FEB-12 01.52.40 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT1','STEP3',to_timestamp('02-FEB-12 01.54.20 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT1','STEP4',to_timestamp('02-FEB-12 01.55.32 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT1','STEP5',to_timestamp('02-FEB-12 01.56.36 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',2,'LOT1','STEP1',to_timestamp('02-FEB-12 01.52.41 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT1','STEP1',to_timestamp('02-FEB-12 02.00.29 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT1','STEP2',to_timestamp('02-FEB-12 02.01.43 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT1','STEP3',to_timestamp('02-FEB-12 02.03.23 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT1','STEP4',to_timestamp('02-FEB-12 02.04.34 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT1','STEP5',to_timestamp('02-FEB-12 02.05.40 PM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT1','STEP1',to_timestamp('02-FEB-12 02.02.11 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT1','STEP2',to_timestamp('02-FEB-12 02.03.26 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT1','STEP3',to_timestamp('02-FEB-12 02.05.07 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT1','STEP4',to_timestamp('02-FEB-12 02.06.19 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT1','STEP5',to_timestamp('02-FEB-12 02.07.27 PM','DD-MON-RR HH.MI.SS AM'),20,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT1','STEP1',to_timestamp('02-FEB-12 02.03.54 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT1','STEP2',to_timestamp('02-FEB-12 02.05.08 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT1','STEP3',to_timestamp('02-FEB-12 02.06.49 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT1','STEP4',to_timestamp('02-FEB-12 02.08.01 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT1','STEP5',to_timestamp('02-FEB-12 02.14.26 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT1','STEP1',to_timestamp('02-FEB-12 02.05.35 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT1','STEP2',to_timestamp('02-FEB-12 02.06.50 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT1','STEP3',to_timestamp('02-FEB-12 02.15.14 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT1','STEP4',to_timestamp('02-FEB-12 02.16.26 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT1','STEP5',to_timestamp('02-FEB-12 02.17.31 PM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',7,'LOT1','STEP1',to_timestamp('02-FEB-12 02.06.42 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT1','STEP1',to_timestamp('02-FEB-12 02.14.14 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT1','STEP2',to_timestamp('02-FEB-12 02.15.29 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT1','STEP3',to_timestamp('02-FEB-12 02.17.09 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT1','STEP4',to_timestamp('02-FEB-12 02.18.22 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT1','STEP5',to_timestamp('02-FEB-12 02.23.31 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT1','STEP1',to_timestamp('02-FEB-12 02.58.14 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT1','STEP2',to_timestamp('02-FEB-12 02.15.32 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT1','STEP3',to_timestamp('02-FEB-12 02.59.30 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT1','STEP4',to_timestamp('02-FEB-12 03.01.15 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT1','STEP5',to_timestamp('02-FEB-12 03.07.52 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT1','STEP1',to_timestamp('02-FEB-12 02.23.20 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT1','STEP2',to_timestamp('02-FEB-12 02.24.39 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT1','STEP3',to_timestamp('02-FEB-12 02.26.19 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT1','STEP4',to_timestamp('02-FEB-12 02.27.30 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT1','STEP5',to_timestamp('02-FEB-12 02.28.34 PM','DD-MON-RR HH.MI.SS AM'),17,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT1','STEP1',to_timestamp('02-FEB-12 02.59.37 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT1','STEP2',to_timestamp('02-FEB-12 03.10.51 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT1','STEP3',to_timestamp('02-FEB-12 02.24.52 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT1','STEP4',to_timestamp('02-FEB-12 03.12.03 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT1','STEP5',to_timestamp('02-FEB-12 03.13.41 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT1','STEP1',to_timestamp('02-FEB-12 02.32.30 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT1','STEP2',to_timestamp('02-FEB-12 02.33.43 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT1','STEP3',to_timestamp('02-FEB-12 02.35.24 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT1','STEP4',to_timestamp('02-FEB-12 02.36.35 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT1','STEP5',to_timestamp('02-FEB-12 02.37.41 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT1','STEP1',to_timestamp('02-FEB-12 02.34.11 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT1','STEP2',to_timestamp('02-FEB-12 02.35.26 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT1','STEP3',to_timestamp('02-FEB-12 02.37.06 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT1','STEP4',to_timestamp('02-FEB-12 02.38.19 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT1','STEP5',to_timestamp('02-FEB-12 02.39.29 PM','DD-MON-RR HH.MI.SS AM'),17,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT1','STEP1',to_timestamp('02-FEB-12 02.35.54 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT1','STEP2',to_timestamp('02-FEB-12 02.37.08 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT1','STEP3',to_timestamp('02-FEB-12 02.38.48 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT1','STEP4',to_timestamp('02-FEB-12 02.40.01 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT1','STEP5',to_timestamp('02-FEB-12 02.41.13 PM','DD-MON-RR HH.MI.SS AM'),13,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT1','STEP1',to_timestamp('02-FEB-12 02.37.37 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT1','STEP2',to_timestamp('02-FEB-12 02.38.52 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT1','STEP3',to_timestamp('02-FEB-12 02.40.33 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT1','STEP4',to_timestamp('02-FEB-12 02.41.46 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT1','STEP5',to_timestamp('02-FEB-12 02.42.58 PM','DD-MON-RR HH.MI.SS AM'),17,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT1','STEP1',to_timestamp('02-FEB-12 02.39.20 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT1','STEP2',to_timestamp('02-FEB-12 02.40.34 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT1','STEP3',to_timestamp('02-FEB-12 02.42.16 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT1','STEP4',to_timestamp('02-FEB-12 02.43.29 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT1','STEP5',to_timestamp('02-FEB-12 02.44.42 PM','DD-MON-RR HH.MI.SS AM'),15,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT1','STEP1',to_timestamp('02-FEB-12 02.41.04 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT1','STEP2',to_timestamp('02-FEB-12 02.42.19 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT1','STEP3',to_timestamp('02-FEB-12 02.43.59 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT1','STEP4',to_timestamp('02-FEB-12 02.45.13 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT1','STEP5',to_timestamp('02-FEB-12 02.46.26 PM','DD-MON-RR HH.MI.SS AM'),13,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT1','STEP1',to_timestamp('02-FEB-12 02.42.49 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT1','STEP2',to_timestamp('02-FEB-12 02.44.03 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT1','STEP3',to_timestamp('02-FEB-12 02.45.44 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT1','STEP4',to_timestamp('02-FEB-12 02.47.01 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT1','STEP5',to_timestamp('02-FEB-12 02.48.10 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT1','STEP1',to_timestamp('02-FEB-12 02.44.33 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT1','STEP2',to_timestamp('02-FEB-12 02.45.47 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT1','STEP3',to_timestamp('02-FEB-12 02.47.31 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT1','STEP4',to_timestamp('02-FEB-12 02.48.45 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT1','STEP5',to_timestamp('02-FEB-12 02.49.58 PM','DD-MON-RR HH.MI.SS AM'),14,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT1','STEP1',to_timestamp('02-FEB-12 02.46.17 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT1','STEP2',to_timestamp('02-FEB-12 02.47.31 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT1','STEP3',to_timestamp('02-FEB-12 02.49.16 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT1','STEP4',to_timestamp('02-FEB-12 02.50.31 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT1','STEP5',to_timestamp('02-FEB-12 02.51.42 PM','DD-MON-RR HH.MI.SS AM'),14,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT1','STEP1',to_timestamp('02-FEB-12 02.48.01 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT1','STEP2',to_timestamp('02-FEB-12 02.49.16 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT1','STEP3',to_timestamp('02-FEB-12 02.51.02 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT1','STEP4',to_timestamp('02-FEB-12 02.52.16 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT1','STEP5',to_timestamp('02-FEB-12 02.55.04 PM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT1','STEP1',to_timestamp('02-FEB-12 02.49.48 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT1','STEP2',to_timestamp('02-FEB-12 02.51.02 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT1','STEP3',to_timestamp('02-FEB-12 02.52.47 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT1','STEP4',to_timestamp('02-FEB-12 02.55.39 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT1','STEP5',to_timestamp('02-FEB-12 02.56.45 PM','DD-MON-RR HH.MI.SS AM'),15,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT1','STEP1',to_timestamp('02-FEB-12 02.51.33 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT1','STEP2',to_timestamp('02-FEB-12 02.52.48 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT1','STEP3',to_timestamp('02-FEB-12 02.56.11 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT1','STEP4',to_timestamp('02-FEB-12 02.57.23 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT1','STEP5',to_timestamp('02-FEB-12 02.58.29 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT1','STEP1',to_timestamp('02-FEB-12 02.53.18 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT1','STEP2',to_timestamp('02-FEB-12 02.56.00 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT1','STEP3',to_timestamp('02-FEB-12 02.57.53 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT1','STEP4',to_timestamp('02-FEB-12 02.59.05 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT1','STEP5',to_timestamp('02-FEB-12 03.00.12 PM','DD-MON-RR HH.MI.SS AM'),17,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT1','STEP1',to_timestamp('02-FEB-12 02.56.29 PM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT1','STEP2',to_timestamp('02-FEB-12 02.57.44 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT1','STEP3',to_timestamp('02-FEB-12 02.59.35 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT1','STEP4',to_timestamp('02-FEB-12 03.00.46 PM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT1','STEP5',to_timestamp('02-FEB-12 03.07.19 PM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',-1,'LOT2','STEP1',to_timestamp('24-FEB-12 03.19.26 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT2','STEP1',to_timestamp('24-FEB-12 03.00.47 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT2','STEP2',to_timestamp('24-FEB-12 03.02.56 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',1,'LOT2','STEP3',to_timestamp('24-FEB-12 03.03.56 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',2,'LOT2','STEP1',to_timestamp('24-FEB-12 03.02.22 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',2,'LOT2','STEP2',to_timestamp('24-FEB-12 03.05.06 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',2,'LOT2','STEP3',to_timestamp('24-FEB-12 03.06.06 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT2','STEP1',to_timestamp('24-FEB-12 03.03.56 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT2','STEP2',to_timestamp('24-FEB-12 03.07.15 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',3,'LOT2','STEP3',to_timestamp('24-FEB-12 03.13.53 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT2','STEP1',to_timestamp('24-FEB-12 03.05.54 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT2','STEP2',to_timestamp('24-FEB-12 03.15.03 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',4,'LOT2','STEP3',to_timestamp('24-FEB-12 03.16.04 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT2','STEP1',to_timestamp('24-FEB-12 03.05.58 AM','DD-MON-RR HH.MI.SS AM'),0,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT2','STEP2',to_timestamp('24-FEB-12 03.25.35 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT2','STEP3',to_timestamp('24-FEB-12 03.29.56 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',5,'LOT2','STEP4',to_timestamp('24-FEB-12 03.30.57 AM','DD-MON-RR HH.MI.SS AM'),16,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT2','STEP1',to_timestamp('24-FEB-12 03.14.18 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT2','STEP2',to_timestamp('24-FEB-12 03.17.13 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',6,'LOT2','STEP3',to_timestamp('24-FEB-12 03.19.55 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',7,'LOT2','STEP1',to_timestamp('24-FEB-12 03.15.51 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',7,'LOT2','STEP2',to_timestamp('24-FEB-12 03.21.18 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',7,'LOT2','STEP3',to_timestamp('24-FEB-12 03.22.19 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT2','STEP1',to_timestamp('24-FEB-12 03.17.50 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT2','STEP2',to_timestamp('24-FEB-12 03.23.28 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',8,'LOT2','STEP3',to_timestamp('24-FEB-12 03.24.29 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT2','STEP1',to_timestamp('24-FEB-12 03.21.42 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT2','STEP2',to_timestamp('24-FEB-12 03.25.37 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',9,'LOT2','STEP3',to_timestamp('24-FEB-12 03.26.38 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT2','STEP1',to_timestamp('24-FEB-12 03.23.25 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT2','STEP2',to_timestamp('24-FEB-12 03.27.47 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',10,'LOT2','STEP3',to_timestamp('24-FEB-12 03.28.47 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT2','STEP1',to_timestamp('24-FEB-12 03.27.44 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT2','STEP2',to_timestamp('24-FEB-12 03.32.06 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',11,'LOT2','STEP3',to_timestamp('24-FEB-12 03.33.07 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT2','STEP1',to_timestamp('24-FEB-12 03.29.54 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT2','STEP2',to_timestamp('24-FEB-12 03.34.16 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',12,'LOT2','STEP3',to_timestamp('24-FEB-12 03.35.17 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT2','STEP1',to_timestamp('24-FEB-12 03.32.04 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT2','STEP2',to_timestamp('24-FEB-12 03.36.26 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',13,'LOT2','STEP3',to_timestamp('24-FEB-12 03.37.27 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT2','STEP1',to_timestamp('24-FEB-12 03.34.14 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT2','STEP2',to_timestamp('24-FEB-12 03.38.36 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',14,'LOT2','STEP3',to_timestamp('24-FEB-12 03.39.37 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT2','STEP1',to_timestamp('24-FEB-12 03.36.24 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT2','STEP2',to_timestamp('24-FEB-12 03.40.46 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',15,'LOT2','STEP3',to_timestamp('24-FEB-12 03.41.46 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT2','STEP1',to_timestamp('24-FEB-12 03.38.34 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT2','STEP2',to_timestamp('24-FEB-12 03.42.55 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',16,'LOT2','STEP3',to_timestamp('24-FEB-12 03.43.56 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT2','STEP1',to_timestamp('24-FEB-12 03.40.43 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT2','STEP2',to_timestamp('24-FEB-12 03.45.05 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',17,'LOT2','STEP3',to_timestamp('24-FEB-12 03.46.06 AM','DD-MON-RR HH.MI.SS AM'),17,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT2','STEP1',to_timestamp('24-FEB-12 03.42.53 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT2','STEP2',to_timestamp('24-FEB-12 03.47.15 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',18,'LOT2','STEP3',to_timestamp('24-FEB-12 03.48.16 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT2','STEP1',to_timestamp('24-FEB-12 03.45.03 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT2','STEP2',to_timestamp('24-FEB-12 03.49.25 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',19,'LOT2','STEP3',to_timestamp('24-FEB-12 03.50.26 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT2','STEP1',to_timestamp('24-FEB-12 03.47.13 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT2','STEP2',to_timestamp('24-FEB-12 03.51.34 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',20,'LOT2','STEP3',to_timestamp('24-FEB-12 03.52.35 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT2','STEP1',to_timestamp('24-FEB-12 03.49.22 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT2','STEP2',to_timestamp('24-FEB-12 03.53.44 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',21,'LOT2','STEP3',to_timestamp('24-FEB-12 03.54.45 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT2','STEP1',to_timestamp('24-FEB-12 03.51.32 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT2','STEP2',to_timestamp('24-FEB-12 03.55.54 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',22,'LOT2','STEP3',to_timestamp('24-FEB-12 03.56.55 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT2','STEP1',to_timestamp('24-FEB-12 03.53.42 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT2','STEP2',to_timestamp('24-FEB-12 03.58.03 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',23,'LOT2','STEP3',to_timestamp('24-FEB-12 03.59.04 AM','DD-MON-RR HH.MI.SS AM'),19,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT2','STEP1',to_timestamp('24-FEB-12 03.55.51 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT2','STEP2',to_timestamp('24-FEB-12 04.00.14 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',24,'LOT2','STEP3',to_timestamp('24-FEB-12 04.01.15 AM','DD-MON-RR HH.MI.SS AM'),18,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT2','STEP1',to_timestamp('24-FEB-12 03.58.01 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT2','STEP2',to_timestamp('24-FEB-12 04.02.23 AM','DD-MON-RR HH.MI.SS AM'),1,0);
    Insert into DATATABLE  values ('TOOL1',25,'LOT2','STEP3',to_timestamp('24-FEB-12 04.03.24 AM','DD-MON-RR HH.MI.SS AM'),18,0);(code generated by sqldeveloper)
    In the example:
    LOT1 has two slot, with only 1 step instead of 5 --> these slots should not be counted --> result 23
    LOT2 has one slot, with 4 instead of 3 --> this slot is ok and should be counted --> result 25
    as previously mentioned, i want to count all slots, which got at least the median amount of steps [edit: slot -1 should also not be counted]
    LOT1:
        SELECT
          sum(CASE WHEN SLOT != -1 THEN parameter1 END) parameter1,
          min(CASE WHEN SLOT != -1 THEN EndTime END) first_time,
          max(CASE WHEN SLOT != -1 THEN EndTime END) last_time,
          sum(CASE WHEN SLOT != -1 THEN parameter6 END) parameter6,
          CASE WHEN count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT END) > 0 AND ROUND(count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT||STEP END)/count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT END)) > 0 THEN FLOOR(count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT||STEP END)/ROUND(count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT||STEP END)/count(DISTINCT CASE WHEN SLOT != -1 THEN SLOT END))) ELSE NULL END num_slots,
          sum(CASE WHEN SLOT = -1 THEN parameter1 END) minusone_parameter1,
          min(CASE WHEN SLOT = -1 THEN EndTime END) minusone_first_time,
          max(CASE WHEN SLOT = -1 THEN EndTime END) minusone_last_time,
          sum(CASE WHEN SLOT = -1 THEN parameter6 END) minusone_parameter6,
          count(CASE WHEN SLOT = -1 THEN 1 END) num_minusone
        FROM
          datatable
      WHERE
        tool = 'TOOL1'
        AND lotid = 'LOT1';
    PARAMETER1             FIRST_TIME                LAST_TIME                 PARAMETER6             *NUM_SLOTS*              MINUSONE_PARAMETER1    MINUSONE_FIRST_TIME       MINUSONE_LAST_TIME        MINUSONE_PARAMETER6    NUM_MINUSONE          
    423                    02.02.12 13:51:28         02.02.12 15:13:41         0                      *23*                     46                     02.02.12 14:20:35         02.02.12 15:18:47         0                      5                      this is the desired result
    same query with Lot2:
    PARAMETER1             FIRST_TIME                LAST_TIME                 PARAMETER6             *NUM_SLOTS*              MINUSONE_PARAMETER1    MINUSONE_FIRST_TIME       MINUSONE_LAST_TIME        MINUSONE_PARAMETER6    NUM_MINUSONE          
    506                    24.02.12 03:00:47         24.02.12 04:03:24         0                      *25*                     1                      24.02.12 03:19:26         24.02.12 03:19:26         0                      1                      so right now i get the desired results with this ugly query... but with combinations of several different conditions - e.g. several slots with more steps and a few with less... - it may not work.
    Edited by: Paneologist on Feb 24, 2012 7:42 AM

  • Help in group by query

    Hi,
    Please help me in the following query.
    SELECT
    a.bcast_id ,
    a.bcast_file_nm ,
    a.clickthru_url_cd ,
    a.list_id ,
    a.prs_id ,
    a.resp_cd ,
    a.run_seq ,
    COUNT(a.resp_cd) ,
    c.src_grp_cd ,
    b.site_cd
    FROM
    gca_cbx_response_test a ,
    gca_program_run_hist b ,
    campaign_master c
    WHERE
    a.list_id=b.list_id
    AND
    a.run_seq=b.campaign_run_seq
    AND
    b.campaign_cd=c.campaign_cd
    GROUP BY
    a.bcast_id ,
    a.bcast_file_nm ,
    a.clickthru_url_cd ,
    a.list_id ,
    a.prs_id ,
    a.resp_cd ,
    a.run_seq ,
    c.src_grp_cd ,
    b.site_cd
    HAVING
    a.resp_ts
    BETWEEN
    to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS')
    AND
    to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS');
    But the following group by query is throwing error that a.resp_ts is not a group by function.
    But i don't want to add a.resp_ts in group by clause.
    Please help me in this query.
    Thanks,
    Dilip

    How about:
    SELECT a.bcast_id ,
           a.bcast_file_nm ,
           a.clickthru_url_cd ,
           a.list_id ,
           a.prs_id ,
           a.resp_cd ,
           a.run_seq ,
           COUNT(a.resp_cd) ,
           c.src_grp_cd ,
           b.site_cd
    FROM   gca_cbx_response_test a ,
           gca_program_run_hist b ,
           campaign_master c
    WHERE  a.list_id=b.list_id
    AND    a.run_seq=b.campaign_run_seq
    AND    b.campaign_cd=c.campaign_cd
    AND    a.resp_ts BETWEEN to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS')
                     AND to_date('04-AUG-2005 00:01:32','DD-MON-YYYY HH24:MI:SS');
    GROUP BY a.bcast_id ,
             a.bcast_file_nm ,
             a.clickthru_url_cd ,
             a.list_id ,
             a.prs_id ,
             a.resp_cd ,
             a.run_seq ,
             c.src_grp_cd ,
             b.site_cd;

  • ORDER BY/GROUP BY in XSL/T

    SCENARIO 1:
    Hello all!
         I have got an XML file which contains a list of products. I transform this file into an HTML page using an XSL stylesheet. The XML file looks like the following:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <products>
         <product id="40117" date="20030228">
              <issuer code="A-08244998">MECALUX, Inc.</issuer>
              <description>This is product 4117 description</description>
         </product>
         <product id="31007" date="20030228">
              <issuer code="A-19728222">REDBERRY TECH, Inc.</issuer>
              <description>This is product 31007 description</description>
         </product>
         <product id="4844" date="20030227">
              <issuer code="A-94847402">JYL SOFT, SA.</issuer>
              <description>This is product 4844 description</description>
         </product>
         <product id="3711" date="20030227">
              <issuer code="A-74185296">SOFTEC, Inc.</issuer>
              <description>This is product 3711 description</description>
         </product>
         <product id="566" date="20030226">
              <issuer code="A-08244998">MECALUX, Inc.</issuer>
              <description>This is product 566 description</description>
         </product>
         <product id="41621" date="20030226">
              <issuer code="A-74185296">SOFTEC, Inc.</issuer>
              <description>This is product 41621 description</description>
         </product>
    </products>
    I would like to order the file by date and then by product id. So the list I'll generate in HTML would have a heading with the date of the products (from most recent to oldest) and then another subheading product-id / Issuer name ordered by ascending product id, like this:
    ---------------- DATE: 2/28/2003 ------------------------
    Product-ID     Issuer Name
    31007 REDBERRY TECH, Inc.
    40117 MECALUX, Inc.
    ---------------- DATE: 2/27/2003 ------------------------
    Product-ID     Issuer Name
    3711 SOFTEC, Inc.
    4844 JYL SOFT, SA.
    ---------------- DATE: 2/26/2003 ------------------------
    Product-ID     Issuer Name
    566 MECALUX, Inc.
    41621 SOFTEC, Inc.
    To sort the data from within the XSL stylesheet I'd use:
         <xsl:apply-templates select="product">
              <xsl:sort select="@date" data-type="number" order="descending"/>
              <xsl:sort select="@id" data-type="number" order="ascending"/>
         </xsl:apply-templates>
    I'd like to write only once the date heading for each product with the same date. Template "product" would output the information of the current product, formatting it as an HTML table row.
    MY QUESTION: How can my stylesheet "remember" the date of the last product processed to know whether I should output a new heading with the date of the current product? The idea is to code the "IF" statement and the use of variable "last_date" but I don't know how in XSL/T.
    Any help would be highly appreciated.
    Thanks in advance.
         last_date = ""
         WHILE there are products DO
              product = GET_NEXT_PRODUCT
              IF product.date != last_date THEN
                   WRITE_DATE_HEADING( product.date )
                   last_date = product.date
              END-IF
              WRITE_PRODUCT_INFO( product )
         END-WHILE
    SCENARIO 2:
    Let's supose the following XML file. I'd like to generate an HTML page
    with a list of products ORDERED BY product description and GROUPED BY
    category name.
    The listing would look like the following:
         Category 1
              product-description [product-id], isuer name
         Category 2
              product-description [product-id], isuer name
         Category 3
              No products at stock
         Category 4
              product-description [product-id], isuer name
    MY QUESTION: The XML file may not be ordered by any criteria. How could I order and group the data in this case? Would it be simpler/easier if the XML file were ordered (or semi-ordered) in any way?
    The XML file would look like the following (note that there are no
    products of category 3):
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <stock>
         <categories>
              <category id="C1">Category 1</category>
              <category id="C2">Category 2</category>
              <category id="C3">Category 3</category>
              <category id="C4">Category 4</category>
         </categories>
         <products>
              <product id="40117" date="20030228" category="C4">
                   <issuer code="A-08244998">MECALUX, Inc.</issuer>
                   <description>This is product 4117 description</description>
              </product>
              <product id="31007" date="20030228" category="C4">
                   <issuer code="A-19728222">REDBERRY TECH, Inc.</issuer>
                   <description>This is product 31007 description</description>
              </product>
              <product id="4844" date="20030227" category="C2">
                   <issuer code="A-94847402">JYL SOFT, SA.</issuer>
                   <description>This is product 4844 description</description>
              </product>
              <product id="3711" date="20030227" category="C4">
                   <issuer code="A-74185296">SOFTEC, Inc.</issuer>
                   <description>This is product 3711 description</description>
              </product>
              <product id="566" date="20030226" category="C2">
                   <issuer code="A-08244998">MECALUX, Inc.</issuer>
                   <description>This is product 566 description</description>
              </product>
              <product id="41621" date="20030226" category="C1">
                   <issuer code="A-74185296">SOFTEC, Inc.</issuer>
                   <description>This is product 41621 description</description>
              </product>
         </products>
    </stock>

    Scenario 1
    How can my stylesheet "remember" the date of the
    last product processedYou can't do it this way, a stylesheet doesn't support the concept of global mutable variables.
    But you can apply a test on this axis "preceding-sibling::product" to see if exists a previous product with the same date.

  • Parameter for ORDER BY in DAX query not respected

    I have an SSRS table fed by a parameterized DAX query (utilizing the methods in
    thesearticles). I have all of my parameters working just fine except for those feeding my closing ORDER BY statement (I am offloading the sorting
    to the Tabular for several reasons, including a large tested performance gain over sorting in SSRS). I have defined the ORDER BY as follows:
    ORDER BY @Order1, @Order2, @Order3, @Order4
    I cannot get even the first parameter to work. I can pass arbitrary strings with no impact on the report returned, though if I hard code the ORDER BY in the query, the ordering is respected, so I know this is a problem with the parameter.
    Running a Profiler trace on the server when I fire the SSRS report returns the following as the parameter value for @Order1:
    <Parameter>
    <Name>Order1</Name>
    <Value xsi:type="xsd:string">Dimuser[UserID-Name]</Value>
    This is exactly what I want to replace @Order1, and when I hard code that exact string into my query I get the behavior I want.
    I have played with \ escaping the brackets to no avail.
    Any insight is greatly appreciated.

    I have discovered a workable solution to my own problem.
    The parameter is passed and interpreted as a quoted string, rather than as a field name.
    I altered my query to follow this general format:
    ORDER BY
    SWITCH( TRUE()
    , @Order1 = "User", DimUser[UserID-Name]
    , SWITCH( TRUE()
    , @Order2 = "User", DimUser[UserID-Name]
    In this way I am comparing two quoted strings for equality and providing an unquoted field identifier that Tabular recognizes and can order by.

  • Alert is not working with a group by query

    Hello,
    I have a group by query. Using that query have created an alarm, but the alarm is not working, though if I execute manually the query, the query is returning records.
    I have update the alarm and set another query which has no group by clause. This way the alarm works.
    So, my question is the following, do you know if this behaviour is because some SAPs limitation, that is, that alarms with queries containing the group by clause cant work?
    Thank you very much for your help!

    Hi Adrian,
    Actually, the query I have post was a simplification of the real one.
    The real one is the following, and there I think it is not possible to rewrite it without the group by>
    SELECT T1.ItemCode , SUM(T1.OpenQty) , SUM(T2.OnHand) , SUM(T1.OpenQty)- SUM(T2.OnHand)
    FROM RDR1 T1 inner JOIN OITW T2 on T1.ItemCode = T2.ItemCode
    WHERE T2.WhsCode = '01'
    GROUP BY T1.ItemCode
    HAVING SUM(T1.OpenQty)- SUM(T2.OnHand) > 0
    Anyway, thank you very much for your idea.

  • Order Type Group - CRM 7.0 B2B Ecommerce with ERP Sales Order Scenario

    Hello Experts
    Scenario: CRM 7.0 with ERP Sales Order
    How can we use ORDER TYPE GROUP in CRM 7.0 B2B E-commerce with ERP sales order?
    With CRM as a back end this ORDER TYPE GROUP functionality is available as OOTB, but with ECC as a back end is this available?
    As per out understanding, we can use more than one TRANSACTION TYPE by seperating the TRANSACTION TYPES by COMMA in shopadmin? Is it a right understanding?
    Kindly let us know if we have ORDER TYPE GROUP functionality in CRM 7.0 B2B E-commerce with ERP sales order?
    Thanks and Regards
    DJ

    Hi DJ,
    In fact the Order Type Group is only available when you are connected to CRM Backend. For ERP Webshops, you only have the option to add the Order Types separated by coma.
    In addition, be aware that B2B only supports Sales Document with category "C - Sales Orders" on ERP backends, for example:Credit Memo Requests won't work. For more information, I created Note 1567713 that explain the restrictions.
    Kind Regards,
    Diego Felix.

  • Get the group by query faster

    Hi,
    I have one group by query which has joins of 2 tables and also the joining columns has indexes.
    But the query takes quite time to get the output.
    Is there any way to get the output of group by clause faster.
    The query is given below
    SELECT a.description,a.type_of_item,a.item_type_id ,b.city_code,COUNT(1)
    FROM PPBS_ITEM_MASTER a,PPBS_INV_SIM_SERIAL b
    WHERE b.item_type_id = a.item_type_id
    AND b.status='AA'
    GROUP BY a.description,a.type_of_item,a.item_type_id ,b.city_code
    Kumar

    Did you do some explain plan and possibly tkprof on the query to see what it is doing?
    what is the explan plan?
    tkprof output?
    what optimizer is in use?
    are stats gathered? if yes, how?
    how many rows are in base tables?
    how many rows do you get from the query?
    how many rows are matching the condition b.status = 'AA' ?
    you can replace that COUNT(1) with a COUNT(*) without any loss of performance.

  • Purchase Order with Goods Receipts Query

    Hi Guys,
    Am still getting to grips with JOINS, I am trying to get a query completed that will show all Purchase Orders that have outstanding items on them, but I would like to also show any corresponding Goods Reciept Notes for that Purchase Order, showing the individual lines with outstanding balances. Here is what I have so far:
    SELECT DISTINCT T0.[CardCode] as [BP Code], T0.[CardName] as [BP Name], T0.[DocNum] as [PO Number], T0.[DocDate]as [PO Date], T1.[ItemCode] as [Stock Item], T1.[Quantity], T1.[OpenQty], T2.[DocNum] as [Goods Receipt No], T2.[DocDate]as [GR Date], T3.[ItemCode] as {Stock Item], T3.[Quantity], T3.[OpenQty] as [Left to Deliver]
    FROM OPOR T0  INNER JOIN POR1 T1 ON T0.DocEntry = T1.DocEntry, OPDN T2 INNER JOIN PDN1 T3 ON T2.DocEntry = T3.DocEntry
    WHERE T0.[DocDate]  >=[%0] AND  T0.[DocDate] <=[%1] AND  T0.[CardName] =[%2]
    I do only want to show the BP once for a given PO (hence my attempt at the DISTINCT command). Could someone be so kind as to point me in the right direction as I will want to turn this into a Crystal Report as well.
    Many thanks in advance
    Sean Martin

    I have double checked your query logic. It can not hold true. Here is the right logic:
    SELECT T0.[CardCode] as [BP Code], T0.[CardName] as [BP Name],
    T0.[DocNum] as [PO Number], T1.[ItemCode] as [Stock Item],
    T1.[Quantity], T1.[OpenQty],
    SUM(isnull(T3.[Quantity],0)) as 'Received Qty',
    T1.[Quantity]-SUM(isnull(T3.[Quantity],0)) as [Left to Deliver]
    FROM dbo.OPOR T0 
    INNER JOIN dbo.POR1 T1 ON T0.DocEntry = T1.DocEntry
    LEFT JOIN dbo.PDN1 T3 ON T3.BaseEntry = T0.DocEntry AND T3.BaseLine = T1.Linenum
    LEFT JOIN dbo.OPDN T2 ON T2.DocEntry = T3.DocEntry
    WHERE T0.[DocDate]  >=[%0] AND T0.[DocDate] <=[%1] AND T0.[CardName] =[%2]
    GROUP BY T0.[CardCode], T0.[CardName], T0.[DocNum], T1.[ItemCode],
    T1.[Quantity], T1.[OpenQty]
    The column 'Left to Deliver' should be identical to 'Remaining Open Qty'
    Thanks,
    Gordon

  • Sort order for group by functions

    I have table TAB1(notenum number, linenum number, linetext varchar2(100)).
    Each note can have multiple lines of text. Eg data
    NOTENUM     LINENUM     LINETEXT
    1     1     THIS IS LINE1.
    1     2     THIS IS LINE2.
    2     1     THIS IS LINE1 OF NOTE2.
    2     2     THIS IS LINE2 OF NOTE2.
    I need the text of lines of the most recent note (max notenum)
    <Note>THIS IS LINE1 OF NOTE2. THIS IS LINE2 OF NOTE2</Note>.
    I have a query like this
    SELECT XMLAGG(XMLPARSE(CONTENT LINETEXT WELLFORMED))
    FROM TAB1
    GROUP BY NOTENUM
    ORDER BY NOTENUM DESC
    Above query would return the text of all lines concatenated for each note and the first row would be the one with highest notenum value.
    My problem is that concatenation of the LINETEXT should happen in ascending order of LINENUM. Since LINENUM is not in GROUP BY clause I cannot specify it in ORDER BY.
    How can I specify to XMLAGG (for that matter any group by function) to concatenate text in a specific order.
    I hope I was able to explain my problem clear enough for somebody to help.
    thanks for all the help
    Message was edited by:
    user448703
    Message was edited by:
    user448703

    Never mind. I just found out that XMLAGG function has the option to specify ORDER BY. I think that will work.

  • Oracle group by query

    create a matrix query to display a job, the salary for that job based on department number, and the total salary for that job, for departments 20,50,80,90 giving each column an appropriate heading. this is the excise query available in oracle in ebook. I have solved it by using inline view.. given below.. Is ther any other way to solve the same quey.. Please help..
    SELECT job_id,
    CASE
    WHEN department_id=20
    THEN salary
    ELSE NULL
    END AS dept20,
    CASE WHEN department_id=50 THEN salary ELSE NULL
    END AS dept50,
    CASE WHEN department_id=80 THEN salary ELSE NULL
    END AS dept80,
    CASE WHEN department_id=90 THEN salary ELSE NULL
    END AS dept90,
    salary
    FROM
    (SELECT job_id,
    SUM(salary) salary,
    department_id
    FROM employees
    WHERE department_id IN (20,50,80,90)
    GROUP BY job_id,
    department_id
    ) order by job_id;

    Hi,
    you can try something like this:
    *not tested
    select distinct job_id,
    (select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 20) sum_sal_20,
    (select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 50) sum_sal_50,
    (select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 80) sum_sal_80,
    (select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 90) sum_sal_90
    from employees e
    where department_id IN (20,50,80,90)
    group by job_id, department_idRegards
    Imran

  • Need help on group by query problem.

    Hi,
    I've a table like..
         CREATE TABLE DPT(DEPTNO NUMBER(4),HIREDATE DATE);
         INSERT INTO DPT VALUES(10,SYSDATE);
         INSERT INTO DPT VALUES(10,SYSDATE+1);
         INSERT INTO DPT VALUES(10,SYSDATE+2);
         INSERT INTO DPT VALUES(10,SYSDATE+3);
         INSERT INTO DPT VALUES(10,SYSDATE+4);
         INSERT INTO DPT VALUES(10,SYSDATE+5);
         INSERT INTO DPT VALUES(20,SYSDATE);
         INSERT INTO DPT VALUES(20,SYSDATE+1);
         INSERT INTO DPT VALUES(20,SYSDATE+2);
         INSERT INTO DPT VALUES(20,SYSDATE+3);
         INSERT INTO DPT VALUES(30,SYSDATE);
         INSERT INTO DPT VALUES(30,SYSDATE+1);
         INSERT INTO DPT VALUES(30,SYSDATE+2);
         INSERT INTO DPT VALUES(30,SYSDATE+3);
         INSERT INTO DPT VALUES(30,SYSDATE+4);
    SELECT DEPTNO,HIREDATE FROM DPT;
    DEPTNO     HIREDATE
    10     30-OCT-09
    10     31-OCT-09
    10     1-NOV-09
    10     2-NOV-09     
    10     3-NOV-09
    10     4-NOV-09
    20     30-OCT-09
    20     31-OCT-09
    20     1-NOV-09
    END SO ON..
    Actually DPT table having 11 lacs records and Every deptno has got different hiredates.
    I want to retrieve the deptno with minimum hiredate i.e deptno having first date.
    So i've written a query like this using group by clause , When i use the group by clause
    it is taking almost 15 sec to give the result.
    SELECT DEPTNO,MIN(HIREDATE) FROM DPT GROUP BY DEPTNO;
    DEPTNO     HIREDATE
    10     30-OCT-09
    20     30-OCT-09
    30     30-OCT-09
    So is there any otherway of achieving the above result without using group by hence it is taking much time.
    Am using Oracle 10g, TOAD 8.6.1 version
    I've done the analyzing of the table & there are Indexes on both the columns.
    Explan result Below:
    PLAN_TABLE_OUTPUT
    Plan hash value: 682192801
    | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 231K| 2938K| | 3804 (4)| 00:00:46 |
    | 1 | SORT GROUP BY | | 231K| 2938K| 32M| 3804 (4)| 00:00:46 |
    | 2 | INDEX FAST FULL SCAN| DPT_NDX1 | 1180K| 14M| | 1019 (1)| 00:00:13 |
    Please give the other ways of writing the same query for improving the performance.
    Regards,
    Arjun.

    Hi Karhick,
    Thanks for the reply. It is giving expected result,but performance not improved.Please find the below Eplan
    PLAN_TABLE_OUTPUT
    Plan hash value: 1880836313
    | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1180K| 32M| | 6714 (2)| 00:01:21 |
    |* 1 | VIEW | | 1180K| 32M| | 6714 (2)| 00:01:21 |
    |* 2 | WINDOW SORT PUSHED RANK| | 1180K| 14M| 54M| 6714 (2)| 00:01:21 |
    | 3 | INDEX FAST FULL SCAN | DPT_NDX1 | 1180K| 14M| | 1019 (1)| 00:00:13 |
    Predicate Information (identified by operation id):
    1 - filter("RNO"=1)
    2 - filter(ROW_NUMBER() OVER ( PARTITION BY "DEPTNO" ORDER BY "HIREDATE")<=1)
    What i've to do now. Please let me know.
    Regards,
    Arjun.

  • Get more column in Group by query....

    select np.netting_profile, max(np.effective_date) from netting_profile np
    where np.profile_run_time_date=to_timestamp('2010-11-18 18:00:00','yyyy-MM-dd HH24:MI:SS') and
    np.effective_date <= to_timestamp('2010-11-26 00:00:00','yyyy-MM-dd HH24:MI:SS')
    GROUP BY np.netting_profile
    In above query i need to get Profile_version. but i dont want to GROUP BY profile_version.....
    Multi profile_version are there so i am getting duplicate value.......
    Table structure
    Netting_Profile Profile_Version Effective_Date Profile_run_time_date
    P1 V1 2010-11-18 00:00:00 2010-11-18 18:00:00
    P1 V2 2010-11-26 00:00:00 2010-11-18 18:00:00
    P1 V3 2010-11-28 00:00:00 2010-11-18 18:00:00
    C1 V1 2010-11-20 00:00:00 2010-11-18 18:00:00
    C2 V2 2010-11-26 00:00:00 2010-11-18 18:00:00
    Expected result
    Netting_Profile Profile_Version Max(Effective_Date)
    P1 V2 2010-11-26 00:00:00
    C1 V2 2010-11-26 00:00:00
    here not able to get Profile_Version
    if required what is the solution.........plz help me..........
    Edited by: user1141807 on Nov 28, 2010 11:27 PM

    SQL> SELECT deptno,max(sal)
      2  FROM emp
      3  GROUP BY deptno;
        DEPTNO   MAX(SAL)
            30       2850
            20       3000
            10       5000
    SQL>
    SQL> --Now I want to show ename column. Example of co-related subquery.
    SQL> --Search documentation for more understanding and example.
    SQL> --http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/queries007.htm#i2067858
    SQL> SELECT ename,deptno,sal
      2  FROM emp e1
      3  WHERE (deptno,sal) IN
      4       (SELECT deptno,max(sal)
      5        FROM emp e2
      6        WHERE e1.deptno=e2.deptno
      7        GROUP BY deptno
      8        );
    ENAME          DEPTNO        SAL
    BLAKE              30       2850
    SCOTT              20       3000
    FORD               20       3000
    KING               10       5000
    SQL> http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/queries007.htm#i2067858
    SQL> --Use Of analytical function.
    SQL> SELECT ename,deptno,sal
      2  FROM (SELECT ename,deptno,sal
      3        ,RANK() OVER ( PARTITION BY deptno ORDER BY sal desc) rn
      4         FROM emp
      5         )
      6  WHERE rn<2;
    ENAME          DEPTNO        SAL
    KING               10       5000
    SCOTT              20       3000
    FORD               20       3000
    BLAKE              30       2850
    SQL> http://download.oracle.com/docs/cd/E11882_01/server.112/e16579/analysis.htm#i1006229

Maybe you are looking for