How to write this query using correlation subquery or non exists clause

-- Tables description.
--step-1- 4 employees present in EMP table.
--step-2- each employee having 1 country_no in EMP_DOCS table.3 employees having same country_no(i.e emp's 1,2,3)
--step-3- 1 emp document can have multiple items.In this case each employee having one each in the EMP_ITEMS table.
--step-4- 1 EMPLOYEE  can have Multiple Documents so we have a relation between EMP_ITEMS and DOCUMENT_ITEMS.whatever items present in EMP_ITEMS those items will be inserted into DOCUMENT_ITEMS.so we have a item-item relation.
--so we stored EMP_ITEMS id in EMP_ITEMS_REF_ID_1 of DOCUMENT_ITEMS table.
-- step-5- DOCUMENT-INVOICE has 1-1 relation once invoice is created we stored invoice id in DOCUMENT table.
--This is the requirement.Let's say in this example 3 employees are using same country_no and 4th employee is using another country_no
--which is not used by other 3 employees.
--Condtion-1:
--if all of the employees have created INVOICE which is using same country_no of different country_no then the query should display all records.
--Condition-2:
--if any one of the employee not created INVOICE which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
--Condition-3:
--if any one of the employee not created even DOCUMENT  which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
I hope I explain the conditions clearly.if you understand well by looking at the data i posted below may i know what is the final result should be displayed?
create table EMP
  ID       NUMBER not null,
  TYPE     VARCHAR2(1)
alter table EMP
  add constraint ID primary key (ID);
create table EMP_DOCS
  ID         NUMBER not null,
  EMP_ID     NUMBER,
  COUNTRY_NO VARCHAR2(15)
alter table EMP_DOCS
  add constraint PK_EMP_DT primary key (ID);
alter table EMP_DOCS
  add constraint FK_EMP_DT foreign key (EMP_ID)
  references EMP (ID);
create table EMP_ITEMS
  ID         NUMBER not null,
  EMP_DOC_ID     NUMBER
alter table EMP_ITEMS
  add constraint PK_EMP_ITEMS_DT primary key (ID);
alter table EMP_ITEMS
  add constraint FK_EMP_ITEMS_DT foreign key (EMP_DOC_ID)
  references EMP_DOCS (ID);
  create table DOCUMENT
  ID   NUMBER not null,
  DOCNO VARCHAR2(15),
  INVOICE_REF_1 NUMBER
alter table DOCUMENT
  add constraint DOC_PK_ID primary key (ID);
  create table DOCUMENT_ITEMS
  ID         NUMBER not null,
  DOC_ID     NUMBER,
  EMP_ITEMS_REF_ID_1    NUMBER
alter table DOCUMENT_ITEMS
  add constraint PK_DOCUMENT_ITEMS_DT primary key (ID);
alter table DOCUMENT_ITEMS
  add constraint FK_DOCUMENT_ITEMS_DT foreign key (DOC_ID)
  references DOCUMENT (ID);
create table INVOICE
  ID   NUMBER not null,
  INVOICE_NO VARCHAR2(15)
alter table INVOICE
  add constraint INVOICE_PK_ID primary key (ID);
  INSERT INTO EMP ( ID, TYPE ) VALUES (
1, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
2, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
3, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
4, 'A');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
1, 1, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
2, 2, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
3, 3, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
4, 4, 'USA');
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
1, 1);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
2, 2);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
3, 3);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
4, 4);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
1, 'DOC1', 1);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
2, 'DOC1', 2);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
3, 'DOC3', 0);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
4, 'DOC4', 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
1, 1, 1);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
2, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
3, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
4, 3, 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
5, 4, 4);
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
1, 'INV1');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
2, 'INV2');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
3, 'INV3');
commit;
-- I have written below query to satisfy above conditions but still required results are not coming..
SELECT *
  FROM EMP E
WHERE E.TYPE = 'A'
   AND NOT EXISTS (SELECT *
          FROM EMP_DOCS       EDOC1,
               EMP_DOCS       EDOC2,
               EMP            E1,
               EMP_ITEMS      EMPI,
               DOCUMENT_ITEMS DOCITM,
               DOCUMENT       DOC,
               INVOICE        INV
         WHERE EDOC1.EMP_ID = E.ID
           AND EDOC2.EMP_ID = E1.ID
           AND EDOC1.ID = EMPI.EMP_DOC_ID
           AND EDOC1.COUNTRY_NO = EDOC2.COUNTRY_NO
           AND EMPI.ID = DOCITM.EMP_ITEMS_REF_ID_1
           AND DOCITM.DOC_ID = DOC.ID
           AND INV.ID = DOC.INVOICE_REF_1);

DB version:oracle 10g;10.2

Similar Messages

  • How to write this query to filter combination of few values

    Hi,
    I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
    columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
    HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
    I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
    OR
    if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
    How to write this query?
    Regards

    Hi,
    I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
    CHILD_ID
           3
           4? If so, here's one way to get them:
    WITH     all_vacc_ids     AS
         SELECT     c.child_id
         ,     c.vacc_id          AS child_vacc_id
         ,     v.vacc_id
         ,     COUNT ( CASE
                             WHEN  c.vacc_id = 'HEXA'
                       THEN  1
                         END
                    )       OVER ( PARTITION BY  c.child_id
                                       )    AS hexa_itself
         FROM          vacc   v
         LEFT OUTER JOIN     chimm  c     PARTITION BY (c.child_id)
                          ON     c.vacc_id     = v.vacc_id
         WHERE   v.vacc_desc       = 'HEXA'     -- See note below
    SELECT       child_id
    FROM       all_vacc_ids
    WHERE       child_vacc_id     IS NULL
      AND       vacc_id     != 'HEXA'
      AND       hexa_itself     = 0
    GROUP BY  child_id
    rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
    You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher).

  • How to write this query ?

    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno

    Hi,
    806540 wrote:
    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
    ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno
    If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
    You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
    Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
    If you really, really don't want to use analytic functions at all, you can do this:
    SELECT     *
    FROM     scott.emp
    WHERE     deptno     IN  (
                      SELECT       deptno
                      FROM       scott.emp
                      GROUP BY  deptno
                      HAVING       AVG (sal) =  (
                                                       SELECT    MAX (AVG (sal))
                                               FROM          scott.emp
                                               GROUP BY  deptno
    ;

  • Please help to re-write this query using exists or with

    Hi please help to re-write this query using exists or with, i need to write same code for 45 day , 90 days and so on but sub query condition is same for all
    SELECT SUM (DECODE (t_one_mon_c_paid_us, 0, 0, 1)) t_two_y_m_mul_ca_
    FROM (SELECT SUM (one_mon_c_paid_us) t_one_mon_c_paid_us
    FROM (
    SELECT a.individual_id individual_id,
    CASE
    WHEN NVL
    (b.ship_dt,
    TO_DATE ('05-MAY-1955')
    ) >= SYSDATE - 45
    AND a.country_cd = 'US'
    AND b.individual_id in (
    SELECT UNIQUE c.individual_id
    FROM order c
    WHERE c.prod_cd = 'A'
    AND NVL (c.last_payment_dt,
    TO_DATE ('05-MAY-1955')
    ) >= SYSDATE - 745)
    THEN 1
    ELSE 0
    END AS one_mon_c_paid_us
    FROM items b, addr a, product d
    WHERE b.prod_id = d.prod_id
    AND d.affinity_1_cd = 'ADH'
    AND b.individual_id = a.individual_id)
    GROUP BY individual_id)
    Edited by: user4522368 on Aug 23, 2010 9:11 AM

    Please try and place \ before and after you code \Could you not remove the inline column select with the following?
    SELECT a.individual_id individual_id
         ,CASE
            when b.Ship_dt is null then
              3
            WHEN b.ship_dt >= SYSDATE - 90
              3
            WHEN b.ship_dt >= SYSDATE - 45
              2
            WHEN b.ship_dt >= SYSDATE - 30
              1
          END AS one_mon_c_paid_us
    FROM  items           b
         ,addr            a
         ,product         d
         ,order           o
    WHERE b.prod_id       = d.prod_id
    AND   d.affinity_1_cd = 'ADH'
    AND   b.individual_id = a.individual_id
    AND   b.Individual_ID = o.Individual_ID
    and   o.Prod_CD       = 'A'             
    and   NVL (o.last_payment_dt,TO_DATE ('05-MAY-1955') ) >= SYSDATE - 745
    and   a.Country_CD    = 'US'

  • How to write the query using Index

    Hi All,
    I have to fetch the records from Database table using Index, how can i write the query using Index. Can any body plz send me the sample code.
    Help Me,
    Balu.

    Hi,
    See the below Example.
    select * from vbak up to 100 rows
    into table t_vbak
    where vbeln > v_vbeln.
    sort t_vbak by vbeln descending.
    read table t_vbak index 1.
    Regards,
    Ram
    Pls reward points if helpful.

  • How to write a query use between two months

    Hello,
    I have two views v1 and v2. Both have two columns c1 and c2: c1 with mon-yyyy format, c2 is a number. I am writing a query like this;
    select v1.c2, v2.c2 from v1, v2
    where v1.c1 = v2.c1 and v1.c1 between to_date('Jan-2011','mon-yyyy')
    and to_date('Dec-2011','mon-yyyy')
    i got: ORA-01858: a non-numeric character was found where a numeric was expected
    V1 and V2 value are like these:
    Mon-2011 89
    Feb-2011 33
    Mar-2011 45
    Apr-2011 98
    How to make it work.
    Thanks for your help.
    Jen

    Hello Jen Hu,
    Try this:
    with t as ( select 'feb-2011' as c1, 33 as nr from dual union
    select 'mar-2011',45 from dual union
    select 'apr-2011',98 from dual union
    select 'jun-2011',86 from dual union
    select 'jul-2011',78 from dual union
    select 'aug-2011',87 from dual union
    select 'sep-2011',68 from dual union
    select 'oct-2011',56 from dual union
    select 'nov-2011',68 from dual union
    select 'dec-2011',55 from dual union
    select 'jan-2012',54 from dual
    ) SELECT * FROM T
    where to_date(c1,'mon-yyyy') between to_date('aug-2011','mon-yyyy') and to_date('dec-2011','mon-yyyy');@Tauceef:
    >
    Tauceef wrote:
    You have to store full date, the conversion you are doing to_date('jan-2011','mon-yyyy') is not at all accepted, this is where you are getting that error.
    >
    Why not? Have you tried:
    select to_date('jan-2011','mon-yyyy') from dual;Once you convert the character string into date it is easier to compare!
    I agree with @clcarter's point:
    >
    Firstly, if you want to compare dates, use a DATE datatype, not varchar2. If the varchar2 has invalid dates you'll have all sorts of trouble. 'Feb-31-2011' is a perfectly valid string. But its not a date.
    >
    But if you have better mechanism of valid string input as in here:
    http://apex-plugin.com/oracle-apex-plugins/item-plugin/month-picker_148.html
    then problems of invalid string input are minimized!
    Hope it helps!
    Regards,
    Kiran

  • How to INSERT into table using CORRELATED subquery

    I have 3 tables:
    1.TEMP_PHONE(person_id, phonenumber, phone_type) - this holds all phone numbers relating to a person(just a temporary holding area)
    2.PHONE_CONNECT(PERSON_ID, PHONE_ID) this table shows all the phone numbers relating to an individual. Phone_id is a unique number to identify a phonenumber and type(cell, work, home) - so in this table a person can have multiple phone ids)
    3.MASTER_PHONE(PHONE_ID, PHONENUMBER, PHONE_TYPE) this is a master phone table. each combination of phone number and type has a unique identifier-phone_id.
    What i need to figure out is how to populate PHONE_CONNECT with the information from TEMP_PHONE IF PERSON_ID already exists but phone_id is different. In other words, if the person gets a new phone number, i need to insert a new row into phone_connect.
    Before that step is started, the master_phone is populated first with a new phone_id associated to the phonenumber/type
    any help would be much appreciated. Thanks in advance.
    So far, this is what i have come up with, but not sure if it makes sense:
    insert into phone_connect(person_id)
    select a.person_id
    from temp_phone a
    where
    person_id = (select b.person_id from phone_connect b, master_phone c
    where
    a.person_id=b.person_id
    and b.phone_id <> c.phone_id
    and c.phonenumber||c.phone_type=a.phonenumber||a.phone_type);
    update phone_connect c
    set phone_id=(
    select b.phone_id
    from temp_phone a, master_phone b
    where a.person_id = c.person_id
    and a.phonenumber||a.phone_type = b.phonenumber||b.phone_type)
    where phone_id is null;

    It does. You are right. But that's what i need help with. I don't think my code is correct. After the insert, the code is actually updating the same exact record I just inserted. I'm sure this all can be done with one insert. I just really don't know how to show that in my code.
    I need to insert a new record into phone_connect with person_id and phone_id. phone_id is already populated in master_phone. I guess my problem is how to go about creating the joins to all three tables to make sure im inserting the data correctly, or not inserting data that already exists.

  • How to write this query.. Please Help

    Hello Guys,
    I have a table that contains all possible leave types, it is a look up table. Table name is Att_Leave_Types and data in the table is:
    Leavetype_ID Leavetype_Desc
    1 Annaul
    2 Sick
    3 Casual
    4 Long
    5 Maternity
    Now, another table Att_Emp_Leaves Contains leaves of an employee
    Columns and values are
    Emp_ID leave_date levetype_id
    14210 28-AUG-06 2
    14210 30-AUG-06 3
    14210 12-JUL-06 1
    14210 13-JUL-06 1
    14210 14-JUL-06 1
    6902 27-AUG-06 2
    6902 30-AUG-06 3
    Now i want to get the following result from the query:
    Group by month and year
    list all leavetypes and leaves employee have availved, if no leavetype availved then the result must show 0 for the leavetype. calculate leaves for a month based on leavetype ID.
    This result will help you clear what i result i want by my query.
    MON_YYYY employee_id leavetype_id number_of_leaves
    JUL-2006 14210 1 3
    JUL-2006 14210 2 0
    JUL-2006 14210 3 0
    JUL-2006 14210 4 0
    JU:-2006 14210 5 0
    AUG-2006 14210 1 0
    AUG-2006 14210 2 1
    AUG-2006 14210 3 1
    AUG-2006 14210 4 0
    AUG-2006 14210 5 0
    AUG-2006 6902 1 0
    AUG-2006 6902 2 1
    AUG-2006 6902 3 1
    AUG-2006 6902 4 0
    AUG-2006 6902 5 0
    Please guide me and help me, its very urgent.
    I will be thankful to you.
    Regards,
    Imran Baig

    Here we are:
    DROP TABLE Att_Leave_Types;
    CREATE TABLE Att_Leave_Types
               Leavetype_ID NUMBER
              ,Leavetype_Desc VARCHAR2(30)
    INSERT INTO Att_Leave_Types VALUES(1, 'Annual');
    INSERT INTO Att_Leave_Types VALUES(2, 'Sick');
    INSERT INTO Att_Leave_Types VALUES(3, 'Casual');
    INSERT INTO Att_Leave_Types VALUES(4, 'Long');
    INSERT INTO Att_Leave_Types VALUES(5, 'Maternity');
    DROP TABLE Att_Emp_Leaves;
    CREATE TABLE Att_Emp_Leaves
               Emp_ID NUMBER
              ,leave_date DATE
              ,leavetype_id NUMBER
    INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('01-JUL-2006', 'DD-MON-YYYY'), 1);
    INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('10-JUL-2006', 'DD-MON-YYYY'), 1);
    INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('15-JUL-2006', 'DD-MON-YYYY'), 1);
    INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('15-AUG-2006', 'DD-MON-YYYY'), 2);
    INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('25-AUG-2006', 'DD-MON-YYYY'), 3);
    INSERT INTO Att_Emp_Leaves VALUES (6902, TO_DATE('15-AUG-2006', 'DD-MON-YYYY'), 2);
    INSERT INTO Att_Emp_Leaves VALUES (6902, TO_DATE('25-AUG-2006', 'DD-MON-YYYY'), 3);
    SELECT
              LP.*
              ,EL.Leave_date
    COLUMN Emp_ID FOR 999999
    COLUMN Leave_Month FOR A8
    COLUMN Leavetype_ID FOR 999
    COLUMN Leavetype_desc FOR A10
    COLUMN Leave_Count FOR 999
    SELECT
               LP.Emp_ID
              ,TO_CHAR(LP.Leave_Month, 'MON-YYYY') Leave_Month
              ,LP.Leavetype_ID
              ,LP.Leavetype_desc
              ,COUNT(EL.Emp_ID) Leave_Count
         FROM
              SELECT *
                   FROM
                        SELECT
                                   Emp_ID
                                  ,ADD_MONTHS(Min_Leave_month, RN - 1) Leave_Month
                                  ,Max_Leave_month
                             FROM
                                  SELECT ROWNUM RN FROM
                                       USER_OBJECTS
                                       WHERE ROWNUM <= 100
                                  SELECT
                                             Emp_ID
                                            ,TRUNC(MIN(Leave_date), 'MM') Min_Leave_month
                                            ,TRUNC(MAX(Leave_date), 'MM') Max_Leave_month
                                       FROM Att_Emp_Leaves
                                       GROUP BY Emp_ID
                             WHERE ADD_MONTHS(Min_Leave_month, RN - 1) <= Max_Leave_month
                        ,Att_Leave_Types
                   -- ORDER BY Emp_ID, Leave_Month, Leavetype_ID
              ) LP -- Leave periods
              ,Att_Emp_Leaves EL -- Employee leaves
         WHERE EL.Emp_ID(+) = LP.Emp_ID
              AND EL.Leavetype_ID(+) = LP.Leavetype_ID
              AND EL.Leave_date(+) >= LP.Leave_Month
              AND EL.Leave_date(+) < ADD_MONTHS(LP.Leave_Month, 1)
         GROUP BY
               LP.Emp_ID
              ,LP.Leave_Month
              ,LP.Leavetype_ID
              ,LP.Leavetype_desc
         ORDER BY
               LP.Emp_ID
              ,LP.Leave_Month
              ,LP.Leavetype_ID
    EMP_ID LEAVE_MO LEAVETYPE_ID LEAVETYPE_ LEAVE_COUNT
      6902 AUG-2006            1 Annual               0
      6902 AUG-2006            2 Sick                 1
      6902 AUG-2006            3 Casual               1
      6902 AUG-2006            4 Long                 0
      6902 AUG-2006            5 Maternity            0
    14210 JUL-2006            1 Annual               3
    14210 JUL-2006            2 Sick                 0
    14210 JUL-2006            3 Casual               0
    14210 JUL-2006            4 Long                 0
    14210 JUL-2006            5 Maternity            0
    14210 AUG-2006            1 Annual               0
    14210 AUG-2006            2 Sick                 1
    14210 AUG-2006            3 Casual               1
    14210 AUG-2006            4 Long                 0
    14210 AUG-2006            5 Maternity            0
         BW

  • Sql question how to write this query

    This is my query:
    SELECT msi.segment1 item, bsd.operation_code, bd.department_code,
    bsd.operation_description
    FROM mtl_system_items msi,
    bom_operational_routings bort,
    bom_operation_sequences bos,
    bom_departments bd,
    bom_operation_resources br,
    bom_resources bor,
    bom_standard_operations bsd
    WHERE msi.inventory_item_id = bort.assembly_item_id
    AND msi.organization_id = bort.organization_id
    AND bort.routing_sequence_id = bos.routing_sequence_id
    AND bos.department_id = bd.department_id
    AND bd.department_id = bsd.department_id
    AND bos.standard_operation_id = bsd.standard_operation_id
    AND bos.operation_sequence_id = br.operation_sequence_id
    AND bor.resource_id = br.resource_id
    AND bos.reference_flag = 1
    AND msi.organization_id = '82'
    AND bos.disable_date IS NULL
    GROUP BY msi.segment1,
    bsd.operation_code,
    bd.department_code,
    bos.operation_description,
    bsd.operation_description,
    bor.resource_code
    Which essentially produces this output:
    Item Op code Dept Description
    123 10 Warehouse Move parts
    123 20 Assembly Finish Parts
    123 30 Inspection Complete
    I need to capture when the part goes into Inspection and from where it came, so in this case, From Assembly to Inspection.
    I don't even know where to start
    Thanks for any direction

    Not sure I've got the columns names right from your example, but does this get you started at all?
    It sounds like you might need an analytic function like LAG or LEAD.
    with t as
    (select 123 item, 10 op, 'Warehouse' code, 'Move' dept, 'parts' description
    from dual
    union
    select 123, 20, 'Assembly', 'Finish', 'Parts'
    from dual
    union
    select 123, 30, 'Inspection', 'Complete',null
    from dual)
    select *
    from (
    select item, op, code, dept, description, lag(code) over (partition by item order by op) previous_code
    from t
    where code = 'Inspection';
          ITEM         OP CODE       DEPT     DESCR PREVIOUS_C
           123         30 Inspection Complete       AssemblyMessage was edited by:
    dombrooks

  • Help me:how to write this query

    i have a table employees,the data is :
    LAST_NAME DEP SALARY HIRE_DATE COMMISSION_PCT
    gets 10 4000 20060101000000
    davis 20 1500 20060303000000
    king 20 4000 20051118000000
    gets 30 5000 20040101000000
    kochhar 5000 20051201000000
    higens 40 3500 20050706000000
    my question is :
    create a query that will display the toatl number of employees and ,the number of employees hired in 2004,2005,2006.as follow:
    total 2004 2005 2006
    6 1 3 2
    thanks a lot.

    This might help you
    SQL> select * from emp;
    LAST_NAME         DEP     SALARY           HIRE_DATE COMMISSION_PCT
    gets               10       4000      20060101000000
    davis              20       1500      20060303000000
    king               20       4000      20051118000000
    gets               30       5000      20040101000000
    kochhar            30       5000      20051201000000
    higens             40       3500      20050706000000
    6 rows selected.
    SQL> SELECT COUNT(1),SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2004,1,0)) "2004",
      2                  SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2005,1,0)) "2005",
      3                  SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2006,1,0)) "2006"
      4  FROM EMP
      5  /
      COUNT(1)       2004       2005       2006
             6          1          3          2
    SQL>Regards,
    Mohana
    I didn't see Jameel's and APC's solution. They are faster than me :)
    Message was edited by:
    Mohana Kumari

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • How to get this output using sql query?

    Hi,
      How to get this output using sql query?
    Sno Name Age ADD Result
    1 Anil 23 delhi Pass
    2 Shruti 25 bangalor Pass
    3 Arun 21 delhi fail
    4 Sonu 23 pune Pass
    5 Roji 26 hydrabad fail
    6 Anil 28 delhi pass
    Output
    Sno Name Age ADD Result
    1 Anil 23 delhi pass
    28 delhi pass

    Hi Vamshi,
    Your query is not pretty clear.
    write the select query using Name = 'ANIL' in where condition and display the ouput using Control-break statements.
    Regards,
    Kannan

  • How to write this sql query in php code ?

    for example:
    insert into temp
    select *
    from testtable;
    after this, i will query data from sql below:
    select *
    from temp;
    how to write this php code ?
    who can help me ?
    thanks!

    Have a look at the manual to find out how to issue queries.
    http://us3.php.net/oci8

  • Which planning function i have to use and how to write this planning fucnti

    Hi Bi Guru's,
    I have rolled out  BW SEM-BPS Planning Layout's for the Annual Budget in my organistaion.
    There are two levels of layout given for the each sales person.
    1)  Sales quantity to be entered Material and  country wise for all 12 months ( April 2009 to March 2010)
    2)  Rate per unit and to entered in second sheet, Material and country wise for the total qty entered in the first layout.
    Now i need to calculate the sales vlaue for each period and for the each material.
    Which planning function i have to use and how to write this planning fucntion.
    Please suggest me some solution ASAP.
    Thanks in Advance,
    Nilesh

    Hi Deepti,
    Sorry to trouble you...
    I require your help for the following scenario for caluclating Sales Value.
    I have Plan data in the following format.
    Country   Material    Customer    Currency    Fiscyear    Fiscper           Qty         Rate        Sales Value
    AZ          M0001      CU001          #             2009          001.2009        100.00                        
    AZ          M0001      CU002          #             2009          001.2009        200.00                        
    BZ          M0001      CU003          #             2009          001.2009        300.00
    BZ          M0001      CU003          #             2009          002.2009        400.00
    BZ          M0002      CU003          #             2009          002.2009        300.00
    AZ          M0001       #               USD          2009             #                                 10.00
    BZ          M0001       #               USD          2009             #                                 15.50
    BZ          M0002       #               USD          2009             #                                 20.00
    In the Above data the Rate lines are entered in the Second Layout, Where the user enters on the Country Material Level with 2009 value for FISCYEAR.
    I am facing problem with this type of data. 
    I want to store the sales value for each Material Qty.
    Please suggest some solution.
    Re
    Nilesh

  • Hi, I've checked how to use kannada keyboard in Yosemite. But, there are some 'vattu forms' and other grammar in Kannada language that I don't know how to use it. For eg, Preetiya taatanige nimma muddu mommagalu maduva namaskara? How to write this?

    Hi, I've checked how to use Kannada keyboard in Yosemite. But, there are some 'vattu forms' and other grammar in Kannada language that I don't know how to use it. I know how to change between the keyboards and can also view keyboard. However, For eg, 'Preetiya taatanige nimma muddu mommagalu maduva namaskara' How to write this? If anybody can answer this, very much appreciated.
    Thanks in advance for your reply.

    In general, you need to use the "f" (kannada qwerty keyboard) or "d" key (kannada) between letters to make special forms.  Also you may need to adjust the typography settings of the font, which is done by doing Format > Font > Show Fonts > Gear Wheel > Typography as shown below.  If you can provide screenshots or more detailed info on things you cannot make, I can perhaps help.
    It is important to note that MS Word for Mac does not support Indic scripts -- any other app should work OK.

Maybe you are looking for

  • Make a hard drive attached to the Airport Extreme work with Time Capsule?

    I have an external hard drive attached to my airport extreme that shows up on my desktop as if it was attached locally, but time machine recognizes it as the wrong format. In the Get Info screen, the drive's format appears to be 'Appleshare', when it

  • How to uninstall the Adobe Acrobat Professional 7.0?

    I can't uninstall the Adobe Acrobat Professional 7.0 from my computer. It shows saying "C:\Users\Administrator\AppData\Local\Adobe\Acrobat\7.0  Get last Error:5." It also can't be re-installed. How to do it?

  • How do I get Itunes to stop playing song where I left off?

    Whenever I play a song it starts from where I ast left off, which to me is getting kind of annoying, since I'm used to it playing from the beginning. I also switch songs either in the middle or towards the end..So it's a hassle to go switch the time

  • Can WRT54G perform NAT? If not...what can?

    I have a quick question.  I understand that typically you wouldn't use a simple device like the WRT54G for this application, but it's what they have at the moment so I want to see if I can make it work...for now. I need to see if the WRT54G can perfo

  • VSM 7 500 concurrent users reached

    Hello, This morning when logging into VSM 7 we are getting an error that 500 concurrent users has been exceeded. Which is odd since at most we *might* have a few dozen concurrent users. Anyone know where to look to see active VSOM and SASD sessions f