Need help to write an interesting query

I need some help in framing the query for the following scenario.
I have three tables (in general)
Departments
DNO (PK)
DNAME
Employees
DNO (FK to DNO in Departments)
ENO (PK)
CODE
History
seqid (PK)
ENO (FK to ENO in Employees)
The summary of the data in Employees table looks like below:
DNo           No of employees
10              10
20               5
30               8so there will be total of 23 records in employees table. I want to check if all the 10 employees from dept 10 are existing in history table and if exists I want to display the dno and dname from the employees and departments tables respectively. Even if the history table has 9 employees out of 10 from employees table I don't want the query to retrieve that dept details.
Can any one help me with this?
Meanwhile I will also try my level best and let you people know if I come up with anything.
Thanks for your time

Here the example that will help you.
emp_test is the history table for you. You can change ct>9SQL> SELECT * FROM
  2  ( SELECT empno ,
  3          ename ,
  4       e.deptno,dname,
  5       COUNT(*) OVER(PARTITION BY e.deptno ORDER BY e.deptno) ct
  6     FROM EMP e ,DEPT d
  7     WHERE e.deptno=d.deptno
  8     AND EXISTS (SELECT 'x' FROM EMP_TEST T
  9                 WHERE T.empno=e.empno))
10  WHERE ct>3;
     EMPNO ENAME          DEPTNO DNAME                  CT
      7499 ALLEN              30 SALES                   4
      7698 BLAKE              30 SALES                   4
      7654 MARTIN             30 SALES                   4
      7521 WARD               30 SALES                   4
SQL> set linesize 300
SQL> select * from emp_test;
     EMPNO ENAME                JOB              MGR HIREDATE         SAL       COMM     DEPTNO
      7369 SMITH                CLERK           7902 17-DEC-80      800.2                    20
      7499 ALLEN                SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD                 SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES                MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN               SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE                MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK                MANAGER         7839 09-JUN-81       2450                    10
7 rows selected.
SQL> select * from emp;
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
      7369 SMITH      CLERK           7902 17-DEC-80      800.2                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10formatted
Message was edited by:
devmiral

Similar Messages

  • Help me write a SQL query; urgent

    Hi, can somebody please help me write a SQL query.
    I have 3 tables each with the same column names (Col1, Col2, Col3). Col1 is PK with Unique Constraint.
    I wanted to add values of Col2 and Col3 (from all 3 tables) and put it in a separate table (i.e aggregated) of all values found in Col1.
    Does anybody help me please ?
    thanks alot.

    Please don't mark your question as urgent. You've been around here long enough that you should know that it will not get your question anwered any faster, and may just irritate people into not answering at all.
    I'm not sure exactly what you want.
    Are you saying you want t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3 for the rows that have the same c1 in all three tables?
    If so, it would be like this, I think: insert into t4
    select t1.c1
    , t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3
    from t1, t2, t3
    where t2.c1 = t1.c1
    and t3.c1 = t1.c1If that's not what you want, please clarify.
    And next time maybe you should post your SQL question in a SQL forum.

  • Need help in writing a select query to pull required data from 3 tables.

    Hi,
    I have three tables EmpIDs,EmpRoles and LatestRoles. I need to write a select Query to get roles of all employees present in EmpIDs table by referring EmpRoles and LatestRoles.
    The condition is first look into table EmpRoles and if it has more than one entry for a particular Employee ID than only need to get the Role from LatestRoles other wise consider
    the role from EmpRoles .
    Sample Script:
    Create Table #EmpIDs
    (EmplID int )
    Create Table #EmpRoles
    (EMPID int,Designation varchar(50))
    Create Table #LatestRoles
    EmpID int,
    Designation varchar(50)
    Insert into #EmpIDs values (1),(2),(3)
    Insert into #EmpRoles values (1,'Role1'),(2,'Role1'),(2,'Role2'),(3,'Role1')
    Insert into #LatestRoles values (2,'Role2')
    Employee ID 2 is having two roles defined in EmpRoles so for EmpID 2 need to fetch Role from LatestRoles table and for
    remaining ID's need to fetch from EmpRoles .
    My Final Output of select query should be like below.
    EmpID Role
    1 Role1
    2 Role2
    3 Role1
    Please help.
    Mohan

    Mohan,
    Can you check if this answers your requirement:
    Create Table #EmpIDs
    (EmplID int )
    Create Table #EmpRoles
    (EMPID int,Designation varchar(50))
    Create Table #LatestRoles
    EmpID int,
    Designation varchar(50)
    Insert into #EmpIDs values (1)
    Insert into #EmpIDs values (2)
    Insert into #EmpIDs values (3)
    Insert into #EmpRoles values (1,'Role1')
    Insert into #EmpRoles values (2,'Role2')
    Insert into #EmpRoles values (2,'Role1')
    Insert into #EmpRoles values (3,'Role1')
    Insert into #LatestRoles values (2,'Role2')
    --Method 1
    select e.EmplID,MIN(ISNULL(l.Designation,r.Designation)) as Designation
    from #empids e
    left join #emproles r on e.emplID=r.EmpID
    left join #latestRoles l on e.emplID=l.EmpID
    group by e.EmplID
    --Method 2
    ;with cte
    as
    select distinct e.EmplID,r.Designation,count(*) over(partition by e.emplID) cnt
    from #empids e
    left join #emproles r on e.emplID=r.EmpID
    select emplID,Designation
    from cte
    where cnt=1
    UNION ALL
    select a.EmplID,l.Designation
    from
    (select distinct EmplID from cte where cnt>1) a
    join #Latestroles l on a.EmplID=l.EmpID
    order by emplID
    Thanks,
    Jay
    <If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

  • Need help in rewriting a sql query

    Can any one please tell me if there is any utility that can help me correcting the sql I have I need to tune the query as its taking lot of time. I want to use some tool that will help me re-formating the query.
    Any help in this regard will be highly appreciated.

    If you think that Oracle SQL Tuning Tools like SQL Tuning Advisor and SQL Access Advisor are not helping.
    You might look into thrid party tools like Quest- SQL Navigator and TOAD.
    But I don't advise this based on the following:
    Re: Oracle Third Party Tools and Oracle Database
    Oracle have enough tools of its own to satisfy the various needs.
    Adith

  • Need help - I2C write/read with TAOS TCS3414 light sensor using USB-8451

    Hello, I'm new to labview and need help setting up a vi that will allow me to communicate with a digital light sensor (TAOS TCS3414) using a USB-8451. I need to use the sensor to measure light from a light source that I designed and built as part of a project im working on. I've tried looking at several labview I2C exampled but find them to be very confusing. I've used an arduino to interface with the sensor successfully but need to use labview and dont understand how to write the program. The actions are simple; I need initialize the sensor with a simple command and then request data from 8 data registers and then read that data. The data will then be used in further calculations. The portion i need help with is writing and reading from the sensor. I've attached the datasheet for the sensor as a guide. I can also provide the arduino code that i use to read data from the sensor if that would help. 
    Pleae keep in mind that i am completely new to labVIEW. I really do want to learn from this but need quick results so the more help the better. It would greately appreciate any help or explaination. 
    Attachments:
    TCS3414_Datasheet_EN_v1.pdf ‏1806 KB

    Hi Aaron,
    Here you go, this is made with a USB-8452.
    When you run the code tick the power en dac enable box on.
    Maybe you can help me with my problem, I want to use a fiber to sense light from a led.
    Do you use any fiber hardware with the TCS3414?
    gr,
    Attachments:
    TCS3414.vi ‏63 KB

  • Need help inLOV's in AF Query

    Hi,
    I need help in adding LOV's in AF Query. Please do the need full.
    Regards,

    Hi all,
    My issue was resolved i have followed the link"http://andrejusb.blogspot.com/2008/11/adf-query-component-and-view-criteria.html"
    Regards,

  • Need help in PO Approval Group Query

    Hi,
    I need a Query which will list of users that has the Approval Group “BUYER” and “BUYER_DEMO” assigned to them.
    It will be needful if anyone provides me the Query
    Thanks and Regards

    Hi
    Try this.
    SELECT PAPF.employee_number
    ,PAPF.full_name
    FROM po_control_groups_all PCGA
    ,po_position_controls_all PPCA
    ,per_all_assignments_f PAAF
    ,per_all_people_f PAPF
    WHERE PCGA.control_group_name IN ('BUYER','BUYER_DEMO')
    AND PCGA.control_group_id = PPCA.control_group_id
    AND PPCA.position_id = PAAF.position_id
    AND PAAF.person_id = PAPF.person_id
    Thanks and Regards,
    JD

  • Need help to re-format the query

    Hi All,
    Could any one please help me to rewrite this query or explain me how to do it as it is taking 20 mins to execute....
    SELECT
    AC.ACC_NO||DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.PAYMENT_SET      
                                       FROM
            MIF LMI2 WHERE LMI2.MID =
            (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID= MI.MID AND RELATION='ROF')),MI.PAYMENT_SET)||
            DECODE(MI.MSG_CLASS,'ROF', (SELECT LMI2.SERVICE FROM MIF LMI2 WHERE LMI2.MID = (SELECT CHILDMID FROM
      MFAMILY WHERE PARENTMID=MI.MID AND RELATION='ROF')),MI.SERVICE) HASH_KEY,
      MI.MID ,
      DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.PAYMENT_SET FROM MIF LMI2
      WHERE LMI2.MID = (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND
      RELATION='ROF')),MI.PAYMENT_SET) PAYMENT_SET,
      MI.AMOUNT,
      NVL(DECODE(MI.MSG_CLASS,'ROF',MI.CRAMOUNT, MI.DBAMOUNT) ,0) AMT,
      AC.ACC_NO,
      DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.BATCH_COMPANY_CD FROM MIF LMI2 WHERE
      LMI2.MID = (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND
      RELATION='ROF')),MI.BATCH_COMPANY_CD) BATCHCOMP,
      AC.ACC_ALIAS, NVL(AC.IBAN,
      NVL(AC.ACC_ALIAS,AC.ACC_NO)) IBAN_ACCNO,
      AC.CURRENCY , AC.ACCOUNTNAME ,
      CASE
           WHEN (SIGN(MI.PROCESS_DT - TO_DATE(:B2 ,'YYYYMMDD'))) = '1'
           THEN 'F'
           ELSE 'C' END FUTURE_IND,
           (CASE
                WHEN (MI.MSG_STATUS = 'CANCELED' OR   MI.STATBFHELD = 'CANCELED')
                THEN 'C'
                WHEN MI.BATCH_PAYMENT_TP = 'RMTRT'
                THEN 'R'
                WHEN (MI.REJECT_RETURN='REJT' OR MI.MSG_CLASS = 'ROF' OR MI.MSG_STATUS = 'REJECTED')
                THEN 'A'
                WHEN (MI.REJECT_RETURN='RETN' OR (MI.BATCH_PAYMENT_TP='RTACH' AND MSG_CLASS='ROF'))
                THEN 'B'
                WHEN  MI.MSG_STATUS='FORWARD_PROCESSING'
                THEN 'F'
                WHEN (MI.MSG_STATUS NOT IN  ('COMPLETE','CANCELED','REJECTED') OR (MI.MSG_STATUS='AGED' AND
                     MI.STATBFHELD NOT IN ('COMPLETE','CANCELED','REJECTED')) )
                THEN 'U'
                WHEN  MI.PROCESS_DT = TO_DATE(:B2 ,'YYYYMMDD')
                THEN 'P' END)
      PAY_STATUS,
      MT.DB_FEE_ACCOUNT_CCY,
      (SELECT MESSAGEFREETEXT.CONTENTS
           FROM
           MESSAGEFREETEXT
           WHERE
                FIELDNAME ='MANDATEREFERENCE'
                     AND MESSAGEFREETEXT.MID= MI.MID) MANDREF,
      (SELECT MESSAGEFREETEXT.CONTENTS
           FROM MESSAGEFREETEXT
           WHERE FIELDNAME ='DIRECT_DEBIT_REFERENCE' AND MESSAGEFREETEXT.MID=MI.MID)  DDREF,
      MT.CUSTOMER_BATCH_REFERENCE LOCINFO,
      (SELECT  MESSAGEFREETEXT.CONTENTS
           FROM MESSAGEFREETEXT WHERE FIELDNAME =  'SECOND_PARTY_ID' AND MESSAGEFREETEXT.MID=MI.MID) SECPRTYINF,
      (SELECT  MESSAGEFREETEXT.CONTENTS
           FROM MESSAGEFREETEXT WHERE FIELDNAME = 'HUB_PAY_PROD_TYP' AND MESSAGEFREETEXT.MID=MI.MID) PAYPRODTYP,
      DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.CHANNEL_REFERENCE FROM MIF LMI2
                          WHERE LMI2.MID = (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND
                                         RELATION='ROF')),MI.CHANNEL_REFERENCE) CHNREF,
      (SELECT  MESSAGEFREETEXT.CONTENTS
           FROM MESSAGEFREETEXT
           WHERE FIELDNAME =  'CHN_BATCH_REF' AND MESSAGEFREETEXT.MID=MI.MID) CHNBATREF,
      TO_CHAR(DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.VALUE_DATE FROM MIF LMI2
                     WHERE LMI2.MID = (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND
                                    RELATION='ROF')),MI.VALUE_DATE),'DD/MM/YYYY') VALUEDT,
      DECODE(MI.MSG_CLASS,'ROF',(SELECT LMI2.ORIG_REFERENCE FROM MIF LMI2 WHERE LMI2.MID = (SELECT
                          CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND RELATION='ROF')),
                                                        MI.ORIG_REFERENCE) ORIGREF,
      CASE
           WHEN (MI.OFFICE <> MI.ORIG_OFFICE)
           THEN MT.DR_SIDE_RATE_AM
           ELSE
                CASE
                     WHEN MI.DBCURRENCY = MI.CRCURRENCY
                     THEN
                          CASE
                               WHEN MI.CRCURRENCY = :B4
                               THEN MT.DR_RATE ELSE MT.CR_RATE
                          END
                     ELSE
                          CASE
                               WHEN   MI.CRCURRENCY = :B4
                               THEN MT.DR_RATE ELSE MT.CR_RATE
                          END
                END
           END EXCHRT,
      MT.BBK_ADDR1,
      MT.BBK_ADDR2,
      MT.BBK_ADDR3 ,
      MT.BBK ,
      MT.BNF_ADDR1,
      MT.BNF_ADDR2,
      MT.BNF_ADDR3,
      MT.BNF_ADDR4 ,
      MT.BNF ,
      DECODE(MT.BNF_IDCODE,'AC',MT.BNF_ID,' ') ORGBNFID,
      DECODE(NVL(MI.ORIG_INSTRUCT_AMOUNT,0),0, MI.ORIG_AMOUNT) ORGINSTAMT,
      NVL(TRIM(MI.ORIG_INSTRUCT_CURRENCY),  MI.ORIG_CURRENCY) ORGINSTCUR,
      CASE
           WHEN MOP LIKE 'DRFT%'
           THEN 'DD'
           WHEN MOP   LIKE 'CHEQ%'
           THEN 'CO'
           WHEN MOP LIKE 'SWIFT'
           THEN 'TT'
           WHEN MOP LIKE 'DDP%'
           THEN 'T'
           WHEN MOP LIKE 'BOOK'
           THEN 'T' ELSE 'LP'
      END PYMTTP,
      MT.OBI ,
      TO_CHAR(MI.PROCESS_DT,'YYYYMMDD') PROCESSDT,
      MI.REFERENCE,
      MT.BBK_BIC,
      MT.BBK_IDCODE,
      DECODE(MT.BBK_IDCODE,'AC',' ',SUBSTR(MT.BBK_ID,1,12)) BBKID,
      MI.LOCAL_REF,
      MI.CURRENCY INDCURR,
      MI.MSG_STATUS,
      MI.PK_BATCH_SUBSET,
      TO_CHAR(SBBATCH.PROCESS_DT,'YYYYMMDD') SBPROCESSDT,
      DECODE(MI.MSG_CLASS,'ROF',MI.CRCURRENCY, MI.DBCURRENCY) DBCURR,
      MI.ORIG_REFERENCE ORIGREFRET
      FROM
           MIF MI,
           MTF1000 MT,
           ACCOUNTS AC,
           (     SELECT
                     MF.PK_BATCH_SUBSET,
                     MF.PROCESS_DT,
                     MTF.ORG_ID,
                     MTF.MP_CR_ACC,
                     MTF.CR_ACC_NO
                FROM
                     MIF MF,
                     MTF1000
                     MTF
                WHERE
                     ((MSG_STATUS IN ('COMPLETE','CANCELED','REJECTED')) OR
                     (MSG_STATUS = 'AGED' AND STATBFHELD IN ('COMPLETE','CANCELED','REJECTED')))
                AND
                     BATCH_MSG_TP = 'S' AND MF.MID = MTF.MID )
           SBBATCH
      WHERE
           MI.MSG_TYPE =   :B3
      AND MI.PROCESS_DT = TO_DATE(:B2 ,'YYYYMMDD')
      AND MI.BATCH_MSG_TP = 'I'
      AND DECODE(MI.MSG_CLASS,'ROF',
           (SELECT LMI2.SERVICE FROM MIF LMI2 WHERE LMI2.MID =
           (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND RELATION='ROF')),MI.SERVICE)
           IN ('NET','HCN','BIB','LST','MRI')
      AND MI.PK_BATCH_EXCH IN (SELECT PK_BATCH_EXCH FROM BATCH_EXCH)
      AND MI.MID = MT.MID
      AND SBBATCH.PK_BATCH_SUBSET = MI.PK_BATCH_SUBSET
      AND TRIM(DECODE(MI.MSG_CLASS,'ROF',MT.CR_ACC_NO, NVL(MT.ORG_ID,NVL(MT.MP_DB_ACC,MT.ACC_NO))) )=AC.ACC_NO
      AND AC.OFFICE = :B1
      ORDER BY HASH_KEY,PAY_STATUS

    Hi,
    please try this.
    The first decode or at least part of it repeats at least 7 times.
         DECODE(MI.MSG_CLASS,'ROF',
                                         (SELECT LMI2.PAYMENT_SET  
                                    FROM MIF LMI2 WHERE LMI2.MID =
          (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND RELATION='ROF')
                                            ),MI.PAYMENT_SET
                   ||
        DECODE(MI.MSG_CLASS,'ROF',
                                         (SELECT LMI2.SERVICE
                                                                                 FROM MIF LMI2 WHERE LMI2.MID =
                   (SELECT CHILDMID FROM MFAMILY WHERE PARENTMID=MI.MID AND RELATION='ROF')
                                             ),MI.SERVICE) HASH_KEY,
    is similar to this
    select mif.* from mif, mfamily where mif.mid=mfamily.childmid
    and relation = 'ROF' and msg_class='ROF'
    insert it as an inline view and add a conditition
    from ...
    (select mif.* from mif, mfamily where mif.mid=mfamily.childmid
    and relation = 'ROF' and msg_class='ROF') rof
    Where ...
    and mi.mid=Rof.mid (+)...
    and your code above would look like
    nvl(rof.payment_set,mi.payment_set) || nvl(rof.service,mi.service) HASH_KEY,
    nvl(rof.payment_set,mi.payment_set) PAYMENT_SET
    replace all the same decode above with corresponding nvl(...)Hope this helps.

  • Need help to write matrix query

    Hi all,
    I have the query . I'm getting output like this
    chargetype amount start_date
    DA1 170 04/01/2005
    DA2 1170 04/01/2005
    DA3 1730 04/01/2005
    DA4 17 04/01/2005
    DA5 -120 04/01/2005
    DA6 0 04/01/2005
    DA1 170 04/02/2005
    DA2 2005 04/02/2005
    DA3 590 04/02/2005
    DA4 201 04/02/2005
    DA5 340 04/02/2005
    DA6 120 04/02/2005
    I need my output like this
    chargetype 04/01/2005 04/02/2005 04/03/2005 ......
    DA1 170 170
    DA2 1170 2005
    DA3 1730 590
    DA4 17 201
    DA5 -120 340
    DA6 0 120
    pls help me out. your help is greatly appreciated.
    the real query is .
    SELECT st . sttl_item_nme charge_type,
         SUM ( NVL ( sd . sttl_item_amt , 0 )) Adjustment ,
              trunc(s . START_DT_GMT) start_date_gmt
    from      nm_sttl_item_dtl_type st ,
              nm_sttl_item_dtl sd ,
              nm_settlement s
    where st . sttl_item_num = sd . sttl_item_num
         and sd . sttl_id = s . sttl_id
    and trunc(s . START_DT_GMT) between trunc(to_date('04/01/2005', 'mm/dd/yyyy')) and trunc(to_date('04/02/2005', 'mm/dd/yyyy'))
    and s . ptcpt_cd = 'DEMO'
         and s . sttl_pub_cd = ( select sttl_pub_cd
                                  from nm_settlement c
                                  where trunc(c.master_rpt_version_dt_sys) = (select trunc(max(ss.master_rpt_version_dt_sys))
                                                      from nm_settlement ss
                                                      where ss.ptcpt_cd = 'DEMO'
                                                                                         and ss.source_cd = 'ISO'
                                                                                                   and trunc(ss.start_dt_gmt) = trunc(s.start_dt_gmt)
                                       and sttl_id = s.sttl_id)
         group by st . sttl_item_nme, trunc(s . START_DT_GMT)
         order by trunc(s . START_DT_GMT), sttl_item_nme     
    Thanks & Regards,
    Ramana.

    Search this site for "pivot" or "cross tab."

  • Need help in write a query

    Hi guys,
    i have two table. here in each table data like below
    tab1(id, app_id)
    id    app_id
    1     111
    2     222
    tab2(id,role_id)
    id      role_id
    1      900,901,902
    and one emp(id,empid) table.
    id      empid
    1       1234
    Now i want to display app_id, role_id which are assigned a perticular empid = 1234 and which saticifies the ID join condition.
    can any one help me on this
    Rgds,
    KLR

    Wowwww... where is my prev post?
    ok i repeat it :
    1) you need this?
    select e.empid, t2.role_id, t1.app_id from emp e
    join tab2 t2
    on t2.id = e.id
    join tab1 t1
    on t1.id = e.id
    SQL>
    19  /
         EMPID ROLE_ID         APP_ID
          1234 900,901,902        111
    2) or this?
    select empid,
           REGEXP_SUBSTR(role_id,'[^,]+',1,row_number() over(partition by empid order by empid)) val,
           app_id
      from (select e.empid, t2.role_id, t1.app_id
              from emp e
              join tab2 t2
                on t2.id = e.id
              join tab1 t1
                on t1.id = e.id
            connect by INSTR(t2.role_id, ',', 1, level - 1) > 0
                   and prior e.empid = e.empid
                   and prior sys_guid() is not null)
    EMPID    VAL    APP_ID
    1234    900    111
    1234    901    111
    1234    902    111
    Ramin Hashimzade

  • Need help to write a query for Update statement with  join

    Hi there,
    The following update statement gives me error as the given table in set statement is invalid. But its the right table .
    Is the statement correct? Please help .
    update (
           select distinct(vpproadside.VEHICLE_CRED_OVERRIDE.vin)            
             from vpproadside.VEHICLE_CRED_OVERRIDE
             join vpproadside.vpp_vehicle
               on vpproadside.vpp_vehicle.vin = vpproadside.VEHICLE_CRED_OVERRIDE.vin
            where VPP_CARRIER_SEQ_NUMBER = 90
              and EXPIRY_DATE = '17-MAR-10'
       set vpproadside.VEHICLE_CRED_OVERRIDE.EXPIRY_DATE = '15-SEP-10';Edited by: Indhu Ram on Mar 12, 2010 1:00 PM
    Edited by: Indhu Ram on Mar 12, 2010 1:22 PM
    Edited by: Indhu Ram on Mar 12, 2010 2:35 PM
    Edited by: Indhu Ram on Mar 15, 2010 8:04 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:06 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:28 AM

    Ask Tom has very good discussion about this, if UPDATE does not work for PK issue, you can use MERGE
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:760068400346785797

  • Need help to write query

    Hi
    I have following table structure
    Name Type
    HOSTNAME VARCHAR2(30)
    COL1 VARCHAR2(30)
    COL2 VARCHAR2(30)
    data inthe table is like this
    HOSTNAME COL1          COL2
    H1               Authorized          Allowed
    H1               Authorized          Allowed
    H1               Authorized          Allowed
    H2               UnAuthorized     Denied
    H3               UnAuthorized     Denied
    H4               Authorized          Denied
    Now what i want is a query which will give count of Authorized, Unauhorized from col1
    and Count of Denied from col2
    output would look like this
    Authorized 4
    Unauthorized 2
    Denied     3
    Thanks in Advance,
    Kuldeep

    what did you try?
    WITH T AS
              (SELECT 'H1' hostname,'Authorized' col1 ,'Allowed'col2  FROM dual
                   UNION ALL
              SELECT 'H1', 'Authorized' ,'Allowed'    FROM dual
                   UNION ALL
              SELECT 'H1', 'Authorized', 'Allowed'      FROM dual
                   UNION ALL
              SELECT  'H2', 'UnAuthorized', 'Denied'    FROM dual
                   UNION ALL
              SELECT 'H3', 'UnAuthorized', 'Denied'     FROM dual
                   UNION ALL
              SELECT  'H4' ,'Authorized' ,'Denied'  FROM dual
    --- end of data               
    SELECT col1,ct
    FROM (SELECT col1,COUNT(*)ct FROM T GROUP BY col1
           UNION
            SELECT col2,COUNT(DECODE(col2,'Denied',1)) FROM T GROUP BY col2)
    WHERE ct>0

  • Need Help to write SQL Query

    Hi Experts,
    I have one requirement as below.
    I have two tables say test_1 and test_2 with same structure
    Now what i want to accomplish is loop through every record in table table_1 and look for records that have no corresponding entries in table table_2 and delete these rows from table table_1.
    Thanks.

    Hi,
    SQL> create table test_1(sno number,name varchar2(10))
      2  /
    Table created.
    SQL> select * from test1
      2  /
    no rows selected
    SQL> desc test1
    Name                                      Null?    Type
    SNO                                                NUMBER
    NAME                                               VARCHAR2(10)
    SQL> insert into test1 values(1,'vijay')
      2  /
    1 row created.
    SQL> insert into test1 values(2,'vijay')
      2  /
    1 row created.
    SQL> insert into test_1 values(2,'vijay')
      2  /
    1 row created.
    SQL> insert into test_1 values(3,'vi')
      2  /
    1 row created.
    SQL> commit
      2  /
    Commit complete.
    SQL> select * from test1
      2  /
           SNO NAME
             1 vijay
             2 vijay
    SQL> select * from test_1
      2  /
           SNO NAME
             2 vijay
             3 vi
    SQL> select * from test1
      2  minus
      3  select * from test_1
      4  /
           SNO NAME
             1 vijayRegards,
    Vijayaraghavan K

  • Need help to write a query in automation process

    i want to run the scripts in automation process.Is anyone tel me wat is the solution for it?
    how to run this scripts in automated process?
    Edited by: 927851 on Apr 19, 2012 9:56 AM

    Hi
    If you are unix user...
    you can use script like below:
    # $2 is the output filename, $1 is the .sql script
    runsql()
        sqlplus -s user/passwd/@somewhere <<EOF  > $2
        start $1
        exit
    EOF
    runsql file1.sql logfile1 &
    runsql file2.sql  logfile2 &
    runsql anotherfile.sql logfile3  &
    wait

  • Help to write an sql query

    Hi everyone!
    I need some help in composing a query that would calculate the number of occurrences of '29-Feb' between 2 given dates (i.e. substitution variables).
    What I figured so far is a way too complex and don't work all the time. Also I do know, that calculating a leap year is a bit different than just year mod 4 = 0.
    I would appreciate very much if you help me to write this query or just give me a guess.
    SELECT TO_NUMBER(TO_CHAR(date2, 'YYYY')-TO_CHAR(date1, 'YYYY'))/4
    FROM
    (SELECT CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(TO_DATE('&&date1', 'DD-Mon-RR'), 'RR')), 4) = 0
    THEN TO_DATE('&date1', 'DD-Mon-RR')
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 12), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 12)
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 24), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 24)
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 12), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date1', 'DD-Mon-RR'), 36)
    END)
    END)
    END)
    END date1,
    CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(TO_DATE('&&date2', 'DD-Mon-RR'), 'RR')), 4) = 0
    THEN TO_DATE('&date2', 'DD-Mon-RR')
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 12), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 12)
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 24), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 24)
    ELSE(CASE
    WHEN MOD(TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 12), 'RR')), 4) = 0
    THEN ADD_MONTHS(TO_DATE('&date2', 'DD-Mon-RR'), 36)
    END)
    END)
    END)
    END date2
    FROM DUAL);
    UNDEFINE date1;
    UNDEFINE date2;

    Hi,
    Interesting problem!
    The query below looks at the 59th day after January 1 in each year.
    If that date was in February, then it was a leap year.
    If that date was not in February, then it was a common year.
    DEFINE     date1     = "TO_DATE ('07-11-1609', 'DD-MM-YYYY')"
    DEFINE     date2     = "TO_DATE ('06-11-2009', 'DD-MM-YYYY')"
    WITH     all_years AS
         SELECT     ADD_MONTHS ( TRUNC (&date1, 'YYYY')
                      , 12 * (LEVEL - 1)
                      ) + 59       AS dt
         FROM     dual
         CONNECT BY     LEVEL <= 1 + CEIL ( MONTHS_BETWEEN ( &date2
                                                         , &date1
                               / 12
    SELECT     COUNT ( CASE
                   WHEN  TO_CHAR (dt, 'MM') = '02'
                   THEN  1
              END
               )          AS leap_year_cnt
    FROM     all_years
    WHERE     dt     BETWEEN      &date1
              AND      &date2
    ;A less efficient (for what that's worth) way is to generate all dates between &date1 and &date2, and test each one to see if it is '02-29'.
    I'll leave that as an exercise to the reader.
    HINT: the coding is simpler than the query above. (In my prolix style, it took 10 lines. No sub-query is needed.)

Maybe you are looking for

  • Updates are not downloading on 4 Gen iPad

    Is anyone having trouble downloading app updates? I had no problem till about a week or so. Now when I go to the update screen, nothing happens. My wife also has a ipad and checked her update screen and she also has the same problem.

  • 968 InfoPlist.strings duplicate files or not?

    What are InfoPlist.strings files? I used the * search function in Finder and found 986 files like these. They are listed as a document. Almost all of them, except a few, were created on the same date and time. I am trying to clean out all the useless

  • Two radeon 4870 cards

    I have a 2009 Mac Pro with an ATI Radeon HD 4870 Video card, I've been using two monitors since I first got the machine, but I would like to add a second 4870 card and go to 4 monitors (one vertical for coding). Can this be done? There are a number o

  • I don't have a touch screen

    Is Adobe Reader Touch Screen for PC's with touch screens only?

  • In subcontracting process same material to come back from vendor how?

    hi in subcontracting process i want to send x material for processing purpose and same material is going to come back tell me the process how can i do chandra