Ask for help with the query group by

Hello
I have the following table with values structure
CREATE TABLE DUMMY
  (
    SR_NUMBER          VARCHAR2(100 CHAR),
    ASSIGNMENT_GROUP   VARCHAR2(100 CHAR),
    REASSIGNMENT_COUNT VARCHAR2(100 CHAR),
    CREATED DATE,
    CLOSED DATE,
    TARGET_DATE DATE
 )
 
 insert into dummy values('AAA','A','1','02-OCT-11','25-OCT-11','09-OCT-11');
 insert into dummy values('ABC','A','1','03-SEP-12','26-SEP-11','10-SEP-11');
 insert into dummy values('AVB','A','1','02-NOV-13','25-NOV-13','09-NOV-13');
 insert into dummy values('AFT','B','1','02-OCT-11','25-OCT-11','09-OCT-11');
 insert into dummy values('ACS','B','1','02-JAN-12','25-JAN-12','09-JAN-12');
 insert into dummy values('AVC','B','1','02-OCT-13','25-OCT-13','09-OCT-13');
 insert into dummy values('AAD','B','1','02-MAR-14','25-MAR-14','09-MAR-14');
 insert into dummy values('AAA','C','1','02-OCT-11','25-OCT-11','09-OCT-11');
insert into dummy values('AAA','D','1','02-JUN-11','25-JUN-11','09-JUN-11');
insert into dummy values('AAA','E','1','02-APR-12','25-APR-12','09-APR-12');
insert into dummy values('AAA','A','1','02-FEB-13','25-FEB-13','09-FEB-13');

?
I have the following requirement, the output should be:

Number of ticket (sr_number)
% of tickets inside the DL
Number of tickets inside the DL
Average cycle time (cycle time = closing date - date of creation)
Total cycle time (cycle time = closing date - date of creation)
Number of reallocations (sum)


DL - (deadline) formula is, closed date < = target_date

This should be displayed, grouped by year, then month, then the assignment group. The values must be in descending order (dates) is not sure that group in operation here.
I am able to write the code base of the foregoing, but group from the year, month, and group assignment is pretty confusing to me.


Can someone give me a voucher code...

This may not give you exactly what you want, but if all goes well, it will give you something to work with:

WITH tickets AS (
  SELECT TO_CHAR(created, 'YYYY') created_year,
         TO_CHAR(created, 'MM') created_month,
         assignment_group,
         COUNT(sr_number) ticket_count,
         AVG(closed - created) avg_cycle_time,
         SUM(closed - created) tot_cycle_time,
         COUNT(reassignment_count) reassign_count
   FROM dummy
   GROUP BY TO_CHAR(created, 'YYYY'),
            TO_CHAR(created, 'MM'),
            assignment_group
),
dl_tickets AS (
  SELECT TO_CHAR(created, 'YYYY') created_year,
         TO_CHAR(created, 'MM') created_month,
         assignment_group,
         COUNT(sr_number) ticket_in_dl_count
    FROM dummy
    WHERE closed <= target_date
    GROUP BY TO_CHAR(created, 'YYYY'),
          TO_CHAR(created, 'MM'),
          assignment_group
)
SELECT t.created_year,
       t.created_month,
       t.assignment_group,
       t.ticket_count,
       NVL(dl.ticket_in_dl_count, 0) ticket_in_dl_count,
       NVL(dl.ticket_in_dl_count/t.ticket_count, 0) * 100 pct_ticket_in_dl_count,
       t.avg_cycle_time,
       t.tot_cycle_time,
       t.reassign_count
  FROM tickets t,
       dl_tickets dl
  WHERE t.created_year = dl.created_year (+)
  AND t.created_month = dl.created_month (+)
  ORDER BY created_year DESC, created_month DESC

Published by: user1983440 on May 27, 2013 15:47 - added reassign_count

Published by: user1983440 on May 27, 2013 15:54 - added ORDER BY

Tags: Database

Similar Questions

  • What is the correct way to ask for help with the forum, rather than product, questions?

    I can't find the right way to ask a question about the forums themselves.

    PAGulley said

    I can't find the right part of the forum to ask questions about the forum itself and its features. I wonder what the right place and the way is to get help with the forum system itself. I can find the place to ask questions about specific Mozilla products very well.

    Look up where it says contributor tools and select Contributor Forums. Make sure that you scroll to the top or you'll get a bar with different links instead,

    The forum is https://support.mozilla.org/forums where you question would be better in the SUMO community discussions.

  • ask for help with the 3210, can not see the hp software, can't sweep

    Can someone help me with this, I have a 2.66 ghz processor mac intel core 2 duo, the osx running version 10.6.7 and scanner no longer works after update to latest software to 10.6.7.  I tried to download the software of hp hp upgrade, and then when it works not tried to download apple software update for hp.  Nothing works.  I can't activate a scan of the computer, the software icons have disappeared.  And I can't activate the printer, just shows 'no scan options '.  The computer allows printing, I can find a printer icon with the option of mac system preferences.  very frustrating hours pass, no solution, thank you very much, david.

    I was able to solve the problem by talking to a person capable of mac online, a person named kelly in oregon, basically, we did a software update (she waited w / me through 15 minutes of downloading), and mac program "image capture" is now effective for me for scanning documents.  I think I can have earlier been confused by the fact that the screenshot is a new program, I had been looking for hp scanning software and finder contains more than all the entries of "hp".  Image capture seems to work.  (FYI, we had briefly discussed to insert the original disc, but chose to not not to this since it does not say that it's usable in mac os 10.6 and since we had the opportunity to see if the system works after installing the downloaded software updates).

  • Help with the query GROUP BY?

    Why the habit of this work? When I run the code with the first where clause it works fine.
    select diffrating, count (diffrating) 
    from IT220_HOLIDAYDETAILS
    where diffrating = 'A'
    GROUP BY diffrating
    It returns the result that there is a diffirating marked "A".

    However, when I try to add other clauses he says "no data found"?
    select diffrating, count (diffrating) 
    from IT220_HOLIDAYDETAILS
    where diffrating = 'A'
    and diffrating = 'B'
    and diffrating = 'C'
    and diffrating = 'D'
    and diffrating = 'E'
    GROUP BY diffrating
    How can this be when he came 'A' data in the first place? Surely, it should return 'A' as a negative because it worked before?

    What I want is to count all results for each diffrating and return them. Any reason, why it does not work?

    Thanks in advance!

    It returns nothing because you try to select a diffrating whose value has, b, c, d and e at the same time.

    select diffrating, count (diffrating)
    from IT220_HOLIDAYDETAILS
    GROUP BY diffrating
    

    is not already display what you want?

  • Can you tell me how I can ask for help with the following question of Indesign: I am a new user of Indesign and creative Member Cloud.  I was using the fall of the window/Pages to the bottom of the window to view my pages alphabetically or by page number

    I am a new user of Indesign and creative Member Cloud.  I was using the fall of the window/Pages to the bottom of the window to view my pages alphabetically or by page number and also to determine whether or not my JPEGs have been sufficiently high resolution of the printer.  I don't know how I did it, but somehow the window has changed so that now, there not alphabetical/digital/hi-res information.  It shows just the pages in a two pages in numerical order.  How to restore the window so that I'll be able to view the information I did previously.

    Your help will be appreciated,

    Don Unwin

    [Personal information]

    All I had to do was to click on the links and information in the drop-down list.

  • Dialog box that says ' Creative Cloud Installer wants to make changes. Type your password for this purpose"- I type my password for Adobe ID (creative cloud), but it will not accept it. who should I go for help with this query?

    Dialog box that says ' Creative Cloud Installer wants to make changes. Type your password for this purpose"- I type my password for Adobe ID (creative cloud), but it will not accept it. who should I go for help with this query?

    It does not ask the cloud your computer admin password password!

  • Help with the query to select only one record from the result set in double

    Hello

    Please help with the query. Version of Oracle database we use is 10g R2.

    I have a vision that is duplicated IDS, but they are used across the different functions. See below examples of data. Please help me with a query to select only one record (based on ID regardless of the area) from the bottom of the result set of duplicate records. For what is the point of view is there unique records, given the combination of the fields ID, Org, DF, dry, Sub-Sec

    ID
    Org
    DF
    Sec Sub-Sec

    (163)CQCPDMCPDMHD(163)PCENGENGENG(163)CQASICASICIS8888TSTACTACTAC(163)TSHEHESW6789CQINFOINFOFOS6789PCSECSYSSECSYSINFO16789TSSECSYSSECSYSINFO29009PCBMSBMSBMS1

    My result set must eliminate the duplicate identifiers regardless of whoever we choose of the result set. (I mean without distinction Org, DF, s, Sub-s). My expected result set should be.

    ID
    DSB

    DF
    SEC
    Sub-Sec
    (163)CQCPDMCPDMHD8888TSTACTACTAC6789CQINFOINFOFOS9009PCBMSBMSBMS1


    Thank you

    Orton

    Hello

    This sounds like a job for ROW_NUMBER:

    WITH got_r_num AS

    (

    SELECT id, DSB, df, s, sub_sec org

    ROW_NUMBER () OVER (PARTITION BY ID.

    ORDER BY org

    ) AS r_num

    OF view_x

    )

    SELECT id, DSB, df, sub_sec s,

    OF got_r_num

    WHERE r_num = 1

    ;

    He is a Top - N query example, where you choose the elements of N (N = 1 in this case) from the top of an ordered list.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) to your sample data and the results desired from these data.  (I know that you said that you were a view selection.  Just for this thread, pretending it is a picture and post simple CREATE TABLE and INSERT statements to simulate your point of view).
    Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.  (I didn't quite understand the explanation above.  I don't know why you want to

    ID ORG DF DRY SUB_SEC

    --      ---     --      ---     -------

    1234 CQ DPRK DPRK HD

    and is not

    1234 IS CQ ASIC, ASIC

    or

    TS 1234 IT IT SW

    or

    1234 CQ ASIC ASIC HD

    )
    If you change the query at all, post your modified version.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • need help with the query, thx

    Hello
    I need a little help with the query I have to write;
    the table has 4 columns:
    col1               col2         col3       col4
    emp_name     empl_id    salary      year
    
    content of data:
    
    col1               col2         col3       col4
    smith             12           1200      1999
    smith             12           1340      2000
    smith             12           1500      2001
    jones             13           1550      1999 
    jones             13           1600      2000
    aron              14           1200      2002
    what I am asking is the following result: salary according to the latest available year
    i.e.
    smith         12         1500        2001
    jones         13         1600        2000
    aron          14         1200        2002
    ID appreciate some guidance on how to achieve
    Thank you
    Rgds
    select *from
    (select col1, col2,col3, col4,row_number() over(partition by col1 order by col4 desc)  rn  from 
    ) where rn=1
    
  • Help with the query

    Hi, can someone help me with the query. I try to display comm based on calculation below

    SAL < 5000 then 10%

    5000 - 10000 then 15%

    > 10000 then 20%

    This is the query

    DECLARE

    v_comm number (7.2);

    v_Name emp.ename%type;

    v_empno emp.empno%type;

    v_sal emp.sal%type;

    BEGIN

    SELECT ename, empno, sal IN

    v_Name, v_empno, v_sal

    EMP;

    v_comm: = (IF v_sal < v_sal*.10 then 5000;)

    ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;

    Of ANOTHER v_sal > 10001 THEN v_sal*.20;

    END IF ;);

    dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);

    END; /

    also tried

    DECLARE

    v_comm number (7.2);

    v_Name emp.ename%type;

    v_empno emp.empno%type;

    v_sal emp.sal%type;

    BEGIN

    SELECT ename, empno, sal IN

    v_Name, v_empno, v_sal

    EMP;

    IF v_sal < v_comm then 5000: = v_sal*.10;

    ELSIF v_sal between 5001 AND 10000 THEN v_comm: = v_sal*.15;

    Of ANOTHER v_sal > 10001 THEN v_comm: = v_sal*.20;

    END IF ;);

    dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);

    END;

    /

    Hello

    Look at the syntax of the IF in the PL/SQL manual.  Comapre to the instruction BOX and also the expression BOX.

    Perhaps, instead of an IF statement

    v_comm: = (IF v_sal<5000 then="">

    ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;

    ELSE v_sal > 10001 THEN v_sal*.20;

    END IF ;);

    you wanted to write an expression BOX, like this:

    v_comm: = BOX

    WHEN v_sal< 5000                ="" then="" v_sal="" *="">

    WHEN v_sal BETWEEN 5001 AND 10000 THEN v_sal *.15

    WHEN v_sal > 10001 THEN v_sal *.20

    END;

    Or perhaps cela:

    v_comm: = v_sal * CASE

    WHEN v_sal<=  5000="" then="">

    WHEN v_sal<= 10000="" then="">

    OF AUTRE.20

    END;

    You can also use a CASE expression in the SELECT statement.

  • Need help with the query. Help, please

    Hey everyone, need your help.  Thank you in advance.  In my view, there is function Pivot.  Just do not know how to use this function.  I have the query that works.  The result is:

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 dental plan pre-tax amount 29,65

    11-111-1111 Vlad 16505 01/04/2013 dental pre-tax 5 August 13 Plan level EE + SP

    11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Option TOP dental plan

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax dental care plan pay the value

    11-111-1111 16505 Vlad 01/04/2013 dental pre-tax 5 August 13 Plan period Type

    11-111-1111 Vlad 16505 01/04/2013 amount pre-tax medical Plan of 5 August 13 149

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Medical Plan level EE + SP

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan medical Option MED

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 plan pre-tax pay value

    11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Medical Plan period Type

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan PPO medical Plan

    11-111-1111 Vlad 16505 01/04/2013 5 August 13 Vision Plan amount 5.94 pre-tax

    But I need the result to be

    Amount of SSN ID name item level Option PayValue period Type

    11-111-1111 Vlad 16505 01/04/2013 null null high of 5 August 13 pre-tax Dental Plan 29,65 EE + SP

    11-111-1111 Vlad 16505 01/04/2013 null null MED 5 August 13 149 medical plan pre-tax EE + SP

    11-111-1111 Vlad 16505 01/04/2013 Vision Plan before taxes of 5 August 13

    Select distinct
    ' 11-111-1111 "as ssn,
    WOMEN'S WEAR. Employee_number,
    "Vlad" as EMPLOYEE_FULL_NAME,
    TO_CHAR (papf.start_date, "MM/DD/YYYY") as Date_Of_Hire
    a.effective_start_date,
    PETF.element_name,
    pivf. Name,
    peevf.screen_entry_value

    Of
    PER_all_PEOPLE_F women's wear
    per_assignments_f A
    pay_element_types_f petf
    pay_element_links_f pelf
    PAY_ELEMENT_ENTRIES_F penf
    PAY_ELEMENT_ENTRY_VALUES_F peevf
    pay_input_values_f pivf
    WHERE
    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
    AND papf.employee_number IS NOT NULL
    AND A.assignment_type = 'E '.
    AND A.person_id = papf.person_id
    and papf.effective_end_date > sysdate
    and a.effective_end_date > sysdate
    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
    and a.assignment_id = 42643
    and a.assignment_status_type_id = '1'
    order of petf.element_name;

    Change with your query

    SELECT * FROM (select distinct)

    ' 11-111-1111 "as ssn,

    WOMEN'S WEAR. Employee_number,

    "Vlad" as employee_full_name,

    TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire

    a.effective_start_date,

    PETF.element_name,

    pivf. Name,

    peevf.screen_entry_value

    Of

    PER_all_PEOPLE_F women's wear

    per_assignments_f A

    pay_element_types_f petf

    pay_element_links_f pelf

    PAY_ELEMENT_ENTRIES_F penf

    PAY_ELEMENT_ENTRY_VALUES_F peevf

    pay_input_values_f pivf

    WHERE

    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")

    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID

    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)

    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)

    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)

    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID

    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID

    AND papf.employee_number IS NOT NULL

    AND A.assignment_type = 'E '.

    AND A.person_id = papf.person_id

    and papf.effective_end_date > sysdate

    and a.effective_end_date > sysdate

    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)

    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)

    and a.assignment_id = 42643

    and a.assignment_status_type_id = '1')

    PIVOT (MAX (screen_entry_value) FOR (name) TO ("Amount" AS 'Amount', 'level' AS 'level', 'Option High' AS 'High Option', 'Pay the value' AS 'Value to pay', 'Period of Type' AS 'Type period'))

    order by element_name;

    (GOLD)

    SELECT ssn,

    Employee_number,

    employee_full_name,

    date_of_hire,

    effective_start_date,

    element_name,

    Max (decode (Name, 'Amount', screen_entry_value)) 'amount. "

    Max (decode (Name, 'Level', screen_entry_value)) 'level ',.

    MAX (DECODE (name, "High Option", screen_entry_value)) "High Option",

    MAX (DECODE (name, 'Value of pay', screen_entry_value)) 'value of pay. "

    MAX (DECODE (name, 'Period Type', screen_entry_value)) 'period of Type '.

    FROM (select distinct)

    ' 11-111-1111 "as ssn,

    WOMEN'S WEAR. Employee_number,

    "Vlad" as employee_full_name,

    TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire

    a.effective_start_date,

    PETF.element_name,

    pivf. Name,

    peevf.screen_entry_value

    Of

    PER_all_PEOPLE_F women's wear

    per_assignments_f A

    pay_element_types_f petf

    pay_element_links_f pelf

    PAY_ELEMENT_ENTRIES_F penf

    PAY_ELEMENT_ENTRY_VALUES_F peevf

    pay_input_values_f pivf

    WHERE

    PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")

    and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID

    and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)

    and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)

    and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)

    and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID

    and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID

    AND papf.employee_number IS NOT NULL

    AND A.assignment_type = 'E '.

    AND A.person_id = papf.person_id

    and papf.effective_end_date > sysdate

    and a.effective_end_date > sysdate

    and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)

    and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)

    and a.assignment_id = 42643

    and a.assignment_status_type_id = '1')

    GROUP BY ssn, employee_number, employee_full_name, date_of_hire, effective_start_date, NOM_ELEMENT

    order by element_name;

  • Need help with the query using the aggregation

    If I have a table, defined as follows:

    CREATE TABLE range_test
    (
    range_id NUMBER (20) NOT NULL,
    rank of char (1) NOT NULL,
    lower_bound_of_range NUMBER (5.2) NOT NULL,
    upper_bound_of_range NUMBER (5.2) NOT NULL,
    received_date_time_stamp SYSTIMESTAMP NOT NULL DEFAULT TIMESTAMP
    );

    And I wanted to query the table to find the range associated with the last line inserted for each 'class' (for example 'A', 'B', 'C', etc.), how would I go about this?

    I want something like the following, but I know that it will not work right:

    SELECT
    grade,
    lower_bounding_of_range,
    upper_bounding_of_range,
    Max (received_date_time_stamp)
    Of
    range_test GROUP BY received_date_time_stamp;

    Thanks for your help... I am frustrated with this one and I think that it should be possible without having to use the PL/SQL (i.e. the functions of SQL aggregation or subqueries should work).

    Perhaps something along the lines of...

    SQL> ed
    Wrote file afiedt.buf
    
      1  select deptno, empno, ename, hiredate
      2  from emp
      3* order by deptno, empno
    SQL> /
    
        DEPTNO      EMPNO ENAME      HIREDATE
    ---------- ---------- ---------- --------------------
            10       7782 CLARK      09-JUN-1981 00:00:00
            10       7839 KING       17-NOV-1981 00:00:00
            10       7934 MILLER     23-JAN-1982 00:00:00
            20       7369 SMITH      17-DEC-1980 00:00:00
            20       7566 JONES      02-APR-1981 00:00:00
            20       7788 SCOTT      19-APR-1987 00:00:00
            20       7876 ADAMS      23-MAY-1987 00:00:00
            20       7902 FORD       03-DEC-1981 00:00:00
            30       7499 ALLEN      20-FEB-1981 00:00:00
            30       7521 WARD       22-FEB-1981 00:00:00
            30       7654 MARTIN     28-SEP-1981 00:00:00
            30       7698 BLAKE      01-MAY-1981 00:00:00
            30       7844 TURNER     08-SEP-1981 00:00:00
            30       7900 JAMES      03-DEC-1981 00:00:00
    
    14 rows selected.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  select deptno, empno, ename, hiredate
      2  from (
      3        select deptno, empno, ename, hiredate
      4              ,row_number() over (partition by deptno order by hiredate desc) as rn
      5        from emp
      6       )
      7  where rn = 1
      8* order by deptno, empno
    SQL> /
    
        DEPTNO      EMPNO ENAME      HIREDATE
    ---------- ---------- ---------- --------------------
            10       7934 MILLER     23-JAN-1982 00:00:00
            20       7876 ADAMS      23-MAY-1987 00:00:00
            30       7900 JAMES      03-DEC-1981 00:00:00
    
    SQL>
    
  • need help with the query: (Frank)

    Hi Frank,.

    I have the oracle 8 database.
    I have the following table with the data structure:

    TABLE P (PID, PNAME, COLOR)
    PRIMARY KEY: PID
    PID     Pname    Color
    p1...........          Red
    P2.................    green
    p3...............       blue
    p4 ..................   red
    CAT TABLE (SID, PID, COST)
    KEY: SID MAIN + PID
    KEY REFERENCE: SID REFERENCE S.SID
    PID P.PID REFERENCES
    Sid        Pid       COst....
    S1        P1
    S2        P2
    S3        P3
    S1      P4
    S2       p1
    S3        p1
    now my question is:
    I want to find the SIDS of suppliers that provide a red and a green part

    for this I used the following query, but it gives the result worng...

    can help me please in this way to get this...
    select sid from cat
    where pid in (select pid from p where color= 'red'
              union
              select pid from p where color = 'gren') 
    group by sid having count(pid) >= 2

    Hello

    Jojo wrote:
    Hi Frank,.

    It would be nice if someone with a different name helped you? Do not restrict yourself by addressing someone in particular in your subject line or the first message.

    I have the oracle 8 database.
    I have the following table with the data structure:

    TABLE P (PID, PNAME, COLOR)
    PRIMARY KEY: PID

    PID     Pname    Color
    p1...........          Red
    P2.................    green
    p3...............       blue
    p4 ..................   red
    

    CAT TABLE (SID, PID, COST)
    KEY: SID MAIN + PID
    KEY REFERENCE: SID REFERENCE S.SID
    PID P.PID REFERENCES

    Sid        Pid       COst....
    S1        P1
    S2        P2
    S3        P3
    S1      P4
    S2       p1
    S3        p1
    

    now my question is:
    I want to find the SIDS of suppliers that provide a red and a green part

    for this I used the following query, but it gives the result worng...

    can help me please in this way to get this...

    select sid from cat
    where pid in (select pid from p where color= 'red'
              union
              select pid from p where color = 'gren')
    group by sid having count(pid) >= 2
    

    You don't like what is the number of lines with a pid; If you had 20 rows all said 'p1', that would be good enough. The number of distinct colors of the whole ('red', 'green') is what is important.
    Avoid the UNION when there is a simple alternative. The UNION tends to be slow.

    You can use a join instead, like this:

    SELECT       c.sid
    FROM       cat     c
    ,            p
    WHERE       p.pid          = c.pid
    AND       p.color       IN ('red', 'green')
    GROUP BY  c.sid
    HAVING       COUNT (DISTINCT p.color)     = 2
    ;
    

    If you would care to post CREATE TABLE and INSERT statements for the sample data and the results that you want from this data, then I could test this.

    Published by: Frank Kulash, January 10, 2011 15:25

  • Help with the query to create hourly statistics

    Hello!

    I have an array of jobs. Each task has a start_date and a column end_date. end_date can be null if the job is still running.

    I need to create a query to display the number of jobs running for all hours during the last two weeks.

    A job can run for more than an hour.


    I tried to define what it means for a job during an interval:


    Job.Start_date < = Interval.end AND Job.Finish_date > = Interval.start


    Can help you with this query?


    Thank you!

    Mihai


    Hi, Mihai,

    User810719-Oracle wrote:

    Hello!

    I have an array of jobs. Each task has a start_date and a column end_date. end_date can be null if the job is still running.

    I need to create a query to display the number of jobs running for all hours during the last two weeks.

    A job can run for more than an hour.

    I tried to define what it means for a job during an interval:

    Job.Start_date <= interval.end="" and="" job.finish_date="">= Interval.start...

    You gave essentially the solution yourself.  You just need to outside join your jobs table to a table (or, in the example below, a result set that acts like a table) containing 1 row for each interval.  You can use NVL to equate finish_dates with an effective DATE NULL, so they will be counted:

    WITH intervals AS

    (

    SELECT TRUNC (SYSDATE, 'HH') - ((LEVEL-1)/24) AS interval_start

    , TRUNC (SYSDATE, 'HH') - ((LEVEL-2)/24) AS interval_end

    OF the double

    CONNECT BY LEVEL<= 14="" *="">

    )

    SELECT i.interval_start

    EARL of (j.start_date) AS jobs_running

    Intervals I have

    LEFT OUTER JOIN jobs j WE j.start_date<=>

    AND NVL (j.finish_date

    i.interval_end

    ) > = i.interval_start

    ;

    If you would care to post a small example of data (CREATE TABLE and INSERT statements) and the results desired from this data, I was able to test this.

    Simplify the problem for display.  Do what you are interested only for the past 6 hours, not the last 2 weeks.  We will find a solution that can easily adapt to any number or intervals.

  • Need help with the query string manipulation

    Hello

    With the help of 10.1.0.4.2

    Given a table called PREFIX_CODES that contains a column called PREFIX_CODE
    with the following data:

    PREFIX_CODE - VARCHAR2 (20)
    -------------------
    AAA
    BENAMER
    CARTER

    and another table called SUBSCRIBERS with a column called SUBSCRIBER_ID
    with the following data:

    SUBSCRIBER_ID - VARCHAR2 (30)
    ---------------------
    BBBB-123456
    AAA-444444
    DD-2222222
    EEEE-888888

    Is there a query that will pull all the SUBSCRIBER_ID that begin with
    each of the PREFIX_CODES? The following query is not valid, but it
    This will give you an idea of what I'm trying to do:

    SELECT SUBSCRIBER_ID
    SUBSCRIBERS
    WHERE AS SUBSCRIBER_ID (SELECT PREFIX_CODE |) » %'
    OF PREFIX_CODES)

    Using the data from above, I would like that the query to return:

    BBBB-123456
    AAA-444444

    Thanks for your help!

    Hello

    Welcome to the forum!

    You were on the right track. To find if a given subscriber corresponds to any prefix_code, you can do an EXISTS subquery:

    SELECT     subscriber_id
    FROM     subsribers     s
    WHERE     EXISTS ( SELECT  NULL
                      FROM      prefix_codes
               WHERE      s.subscriber_id     LIKE prefix_code || '%'
                )
    ;
    

    This will tell you if at least a prefix code. It won't tell you exactly how many, or what they were.
    Your message, I'm guessing that there may be more than one, and you can deduct the subscriber_id itself, then the above query should work for you.

  • Help with the query to return the last possible value

    Can someone please help me to create a query for the situation below?

    Table: TABLEA
    Columns:
    FACID VARCHAR2 (10),
    DEPTID VARCHAR2 (10),
    CHARGENUMBER NUMBER (10)

    I have the following data:

    A, B, 1
    A, B, 2
    C, D, 3
    C, D, 4

    I will return the following:

    A, B, 2
    C, D, 4

    In other words, I would return the last possible CHARGENUMBER for FACID and DEPTID.
    The table has no index, and it is responsible for a worksheet in that order.

    Thank you very much!

    Hello

    If you have a TIMESTAMP column, called entry_dt, you can use a Top - N query like this to find the last x entries.

    WITH  got_rnum  AS
    (
         SELECT  my_table.*
         ,     RANK () OVER (ORDER BY entry_dt DESC)     AS rnum
         FROM     my_table
    )
    SELECT     *     -- or list all columns except rnum
    FROM     got_rnum
    WHERE     rnum     <= x
    ;
    

    It is very common to have a trigger to ensure that columns like entry_dt are met.

    If you have only one statement that inserts with hundreds of lines, they can all have the same entry_dt. (The value of SYSTIMESTAMP is constant throughout a statement, even if it takes a few seconds).
    The above query uses RANK, so if you tell him you want the last 10 entries, it can return more than 10, because it includes all lines with exactly the same entry_dt as the 10th. If you want to exactly 10 rows returned, even if there is a tie for 10th place, then use ROW_NUMBER instead of RANK; the rest of the query is the same.

Maybe you are looking for

  • Problems with multitasking on Satellite Pro P200

    I am the owner of a new Satellite Pro P200.I use this laptop to use software such as photoshop, indesign, etc. * Now I have problems with multitasking! *It is not possible to run 3 programs at the same time.That I have to restart my laptop again and

  • Re: Forgotten password on the Satellite L300

    really hope someone can help. my daughter (12!) has the laptop above and set a password, she now can not remember :-/! When you start the laptop, the only option is for her to enter his password in his profile. There is no user "admin" or "invited" o

  • Use two WiFi routers?

    Can I have an old Airport Extreme (flying saucer model) and a new running on my system time Capsule at the same time?  I would like to use some sort of splitter for ethernet (recommendations will be appreciated).

  • False Boolean input values Cluster

    Hi all I seem to be encountering a problem with an entry in one of my screws as shown in the image below, I have a while loop with a structure inside the event. I'm pass in a set of values of Boolean control, which is modified by a series of input us

  • switch mode

    Hi all. I am a student of the University (currently in thesis) required instrumentation OR its function. The University has made avaiable for me an NI USB-6289 data acquisition and LabView software for the manipulation of data. For the first part of