DECODE query help

SELECT * FROM MYTABLE IS:
DOC_ID          STATUS  PKG     NOS     AMOUNT
IN1439324     GEN     BX     5     513
IN1439324     GEN     CT     2     756
IN1439324     EXP     BX     1     770
IN1439324     EXP     CT     1     1134
IN1439999     GEN     CT     2     956
IN1439999     EXP     BX     1     1270i want a query to show the summery as below
          GEN_BX |GEN_CT |EXP_BX |EXP_BX |GENAMT_BX |GENAMT_CT |EXPAMT_BX |EXPAMT_CT
IN1439324    5   |   2   |   1   |   1   |   513    |   756    |   770    | 1134
IN1439999        |   2   |   4   |       |          |   956    |  1270    | Edited by: RamiahRAGU on Oct 30, 2008 12:37 PM

This is a classic pivot query.
In 10g or below you would do something like...
SQL> ed
Wrote file afiedt.buf
  1  with t as (select 'IN1439324' as doc_id, 'GEN' as status, 'BX' as pkg, 5 as nos, 513 as amount from dual union all
  2             select 'IN1439324', 'GEN', 'CT', 2, 756 from dual union all
  3             select 'IN1439324', 'EXP', 'BX', 1, 770 from dual union all
  4             select 'IN1439324', 'EXP', 'CT', 1, 1134 from dual union all
  5             select 'IN1439999', 'GEN', 'CT', 2, 956 from dual union all
  6             select 'IN1439999', 'EXP', 'BX', 1, 1270 from dual)
  7  --
  8  select doc_id
  9        ,max(case when status='GEN' and pkg='BX' then nos else null end) as gen_bx
10        ,max(case when status='GEN' and pkg='CT' then nos else null end) as gen_ct
11        ,max(case when status='EXP' and pkg='BX' then nos else null end) as exp_bx
12        ,max(case when status='EXP' and pkg='CT' then nos else null end) as exp_ct
13        ,max(case when status='GEN' and pkg='BX' then amount else null end) as genamt_bx
14        ,max(case when status='GEN' and pkg='CT' then amount else null end) as genamt_ct
15        ,max(case when status='EXP' and pkg='BX' then amount else null end) as expamt_bx
16        ,max(case when status='EXP' and pkg='CT' then amount else null end) as expamt_ct
17  from t
18  group by doc_id
19* order by 1
SQL> /
DOC_ID        GEN_BX     GEN_CT     EXP_BX     EXP_CT  GENAMT_BX  GENAMT_CT  EXPAMT_BX  EXPAMT_CT
IN1439324          5          2          1          1        513        756        770       1134
IN1439999                     2          1                              956       1270
SQL>In 11g you can look at the new PIVOT keyword.

Similar Messages

  • SQL Query Help - Is this possible or impossible????

    Hi guys,
    I need help with an SQL query that I'm trying to develop. It's very easy to explain but when trying to implement it, I'm struggling to achieve the results that I want.....
    For example,
    I have 2 tables
    The first table is:
    1) COMPANY create table company (manufacturer varchar2(25),
                                                          date_established date,
                                                          location varchar2(25) );My sample test date is:
    insert into company values ('Ford', 1902, 'USA');
    insert into company values ('BMW', 1910, 'Germany');
    insert into company values ('Tata', 1922, 'India');The second table is:
    2) MODELS create table models (manufacturer varchar(25),
                                                 model varchar2(25),
                                                 price number(10),
                                                 year date,
                                                 current_production_status varchar2(1) ) ;My sample test data is:
    insert into models values ('Ford', 'Mondeo', 10000, 2010, 0);
    insert into models values ('Ford', 'Galaxy', 12000,   2008, 0);
    insert into models values ('Ford', 'Escort', 10000, 1992, 1);
    insert into models values ('BMW', '318', 17500, 2010, 0);
    insert into models values ('BMW', '535d', 32000,   2006, 0);
    insert into models values ('BMW', 'Z4', 10000, 1992, 0);
    insert into models values ('Tata', 'Safari', 4000, 1999, 0);
    insert into models values ('Tata', 'Sumo', 5500,   1996, 1);
    insert into models values ('Tata', 'Maruti', 3500, 1998, 0);And this is my query:
    SELECT
            com.manufacturer,
            com.date_established,
            com.location,
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
            mod.current_production_status
    FROM
           company com,
           models mod
    WHERE
          mod.manufacturer = com.manufacturer
          and com.manufacturer IN ('Ford', 'BMW', 'Tata')
          and mod.current_production_status IN (1,0)
    ORDER BY
            mod.current_production_status DESCWhat I want the query to output is this:
    com.manufacturer        com.date_established          com.location          mod.model          mod.price             mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    BMW               1910                    Germany               -               -               -          0
    Tata               1922                    India               Sumo               5500               1998          1If current_production_status is 1 it means this particular model has been discontinued
    If current_production_status is 0 it means the manufacturer does not have any discontinued models and all are in procuction.
    The rule is only one record per manufacturer is allowed to have a current_production_status of 1 (so only one model from the selection the manufactuer offers is allowed to be discontinued).
    So the query should output the one row where current_production_status is 1 for each manufacturer.
    If for a given manufacturer there are no discontinued models and all have a current_production_status of 0 then ouput a SINGLE row that only includes the data from the COMPANY table (as above). The rest of the columns from the MODELS table should be populated with a '-' (hyphen).
    My query as it is above will output all the records where current status is 1 or 0 like this
    com.manufacturer        com.date_established          com.location          mod.model          mod.price          mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    Tata               1922                    India               Sumo               5500               1998          1
    Ford               1902                    USA               -               -               -          0                    
    Ford               1902                    USA               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    Tata               1922                    India               -               -               -          0
    Tata               1922                    India               -               -               -          0However this is not what I want.
    Any ideas how I can achieve the result I need?
    Thanks!
    P.S. Database version is '10.2.0.1.0'

    Hi Vishnu,
    Karthiks query helped...
    But this is the problem I am facing...
    SELECT
            com.manufacturer,
            com.date_established,
            com.location,
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
            mod.current_production_status
    FROM
           company com,
           models mod
    WHERE
          mod.manufacturer = com.manufacturer
          and com.manufacturer = 'Ford'
          and mod.current_production_status IN (1,0)
    ORDER BY
            mod.current_production_status DESCThe value of:
    and com.manufacturer = 'Ford'will be dependent on front end user input....
    When I run the query above I get all the rows where current_production_status is either 1 or 0.
    I only require the rows where current_production_status is 1.
    So if I amend it to look like this:
         and mod.current_production_status = 1This works....
    BUT if a user now passes in more than one manufacturer EG:
    and com.manufacturer IN ('Ford', 'BMW')The query will only return the one row for Ford where current_production_status is 1. However because BMW has no models where current_production_status is 1 (all 3 are 0), I still want this to be output - as one row....
    So like this:
    com.manufacturer        com.date_established          com.location          mod.model          mod.price             mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    BMW               1910                    Germany               -               -               -          0So (hopefully you understand), I want both cases to be catered for.....whether a user enters one manufacturer or more than one...
    Thanks you so much!
    This is really driving me insane :-(

  • Please convert decode query to case expression

    Please convert decode query to case expression
    Decode(Sign(id), 1, id, null) positive, Decode(Sign(id), -1, id, null) negative from dual;

    this is a serious forums that help people, if you want that we do your "homeworks" i must told you that you should pay us for that.
    Edited by: Fran on 05-jun-2013 23:41
    1002966     
    Handle:      1002966 
    Status Level:      Newbie
    Registered:      Apr 28, 2013
    Total Posts:      20
    Total Questions:      12 (12 unresolved)

  • Query Help-2

    Query Help:
    http://forum.java.sun.com/thread.jsp?forum=45&thread=471180&tstart=15&trange=15
    It seems I have confused enough people with my improper presentation of query. Sorry guys. I will restate my question with different table names.
    The above was my previous posting, which was not clear..so Iam restating my problem as follows....
    I have the following tables
    Customer(custID, Name, Address)
    Order(custID, OrderID, orderDate)
    CreditCard(custID, creditCard#, creditCardType)
    Now if I have 3 records in Order with custID 100 and 2 records in CreditCard as
    Order:
    100,A001,11/22/03
    100,A002,11/24/03
    100,A003,12/02/03
    CreditCard:
    100,42323232..., VISA
    100,5234234...., MASTER
    Now how can I get
    custID, Name, Address, OrderID, orderDate, creditCard#, creditCarType
    data in minimum no. of records....
    I think I have made my query clear..
    now please help me guys...
    thanks so much for your help.

    You are right.
    But frankly the actual tables on my database are not customer,orders and creditcards..but I just tried to reproduce the problem with these tables, please ignore that user needs a refund etc situtaion. If the tables were actually order,creditcards etc..it would have been a problem to be considered.
    Can you please help me with the query
    if I have m rows in Order and n rows in CreditCard. I will get m*n records, I looking for max(m,n).
    With the following fields in my query result,
    custID, Name, Address, OrderID, orderDate, creditCard#, creditCarType
    from Customer, Order, CreditCard tables
    Thanks so much for your htlp

  • VIDEOS ARE COVERED WITH WHITE RECTANGLES WITH THIS MESSAGE ON THEM Calibrated (Q) XD decode PLEASE HELP.

    I DOWNLOADED CALIBRATED XD DECODED DEMO VERSION TO WATCH A VIDEO THAT MY QUICKTIME WOULD NOT PLAY BECAUSE IT WAS SHOT WITH  XDcam AFTER WATCHING THE VIDEO I ERASED THE CALIBRATED XD...
    NOW THE PROBLEM IS EVERYTIME I GO AND BURN A DVD USING TOAST THE VIDEOS ARE COVERED WITH WHITE RECTANGLES WITH THIS MESSAGE ON THEM Calibrated (Q) XD decode PLEASE HELP..

    CONTACT CALIBRATED TECH SUPPORT.  THey can best help with the removal of the demo.

  • Simple Decode Query

    Decode Query
    Simplified version of query below.
    SELECT
    d.REG,
    d.DP,
    c.CHANNEL
    FROM
    CUSTOMER c ,
    ACCOUNT d
    WHERE c.ID = d.ID
    GROUP BY d.REG, d.DP, c.CHANNEL
    Channel Field in Customer Table contains Multiple Store ID’s (Store 1,Store 2, Store 3,Store 4 etc) – basically I want to use decode on the channel field so that instead of all Store ID’s being outputted what is outputted from Channel is
    Store 1 = ‘Large’
    Store 2 = ‘Medium’
    Store 3 = ‘Small’
    Every other store =’Other’
    Where should the decode statement be inserted and what should the format be – attempt below
    DECODE (c.CHANNEL, ‘Store 1,’Large’, ‘Store 2’,’Medium’, ‘Store 3’,’Small’, ‘Other’)from

    And, just because no one else has pointed it out - using CASE instead would be neater and more lovelisome (in my opinion)...
    SELECT   d.REG,
             d.DP,
             CASE c.CHANNEL
                 WHEN 'Store 1'
                 THEN 'Large'
                 WHEN 'Store 2'
                 THEN 'Medium'
                 WHEN 'Store 3'
                 THEN 'Small'
                 ELSE 'Other'
             END Channel
    FROM     CUSTOMER c,
             ACCOUNT d
    WHERE    c.ID = d.ID
    GROUP BY d.REG,
             d.DP,
             c.CHANNELCheers
    Ben

  • Need a query help

    hii
    i need a query help
    i have two tables
    the 1st table will look like this
    associate id weekid no.of. hours
    4000 810 40
    4000 820 30
    4000 830 60
    4000 840 70
    2nd table will look like this
    associate id weekid no.of.hours
    4000 810 40
    4000 820 70
    4000 830 130
    4000 840 200
    so when i subtract the last two records frm each other in the second table the value should be equal to the no.of.hours in the first table.. for example
    the query shud consider the last record and one before the last record and the difference between two records shud be equal to the value in the 1st table
    for example
    consider week id 830 and 840
    in second table 830=130
    840=200
    when u subtraced both values the difference shud be equal to value in the 1st table for tht week id
    1 ---->>>> 840 - 830
    =200 - 130
    =70
    in first table 840 has 70 hrs
    like this it shud check with all records and it shud return only the records which are not equal
    regards
    srikanth

    This..?
    sql>select * from t1;
    A_ID W_ID HRS
    4000  810  40 
    4000  820  30 
    4000  830  60 
    4000  840  70 
    4000  850  80 
    sql>select * from t2;
    A_ID W_ID HRS 
    4000  810  40 
    4000  820  70 
    4000  830  130 
    4000  840  200 
    4000  850  260 
    sql>
    select a_id,w_id,hrs,sum_hrs
    from(
    select t1.a_id a_id,t1.w_id w_id,t1.hrs hrs,t2.hrs sum_hrs,
           t2.hrs - nvl(lag(t2.hrs)  over(order by t1.w_id),0) diff
    from t1,t2
    where t1.w_id = t2.w_id)
    where diff != hrs;
    A_ID W_ID HRS SUM_HRS 
    4000  850  80  260                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Need help on DECODE query

    Hi Experts,
    Please help me on the below requirement.
    I have a table STUDENT with the following data.
    sid s_code
    1    50    
    2    50
    3    C
    4    A
    4    40
    5    70
    5    90
    6    70I have a table MASTER with the following data.
    stud_code STUD_ABB_CODE
    40        NAF
    50        NRI
    60        CAM
    70        NAS
    80        RMA
    90        MAS It 's having nearly 500 codes.
    It's having only number codes not character codes.
    i.e. C , A,
    we have lot of codes but I have mentioned only few.
    If S_CODE is C then I have show CLOSED
    If S_CODE is A then I have show ACTIVE
    If S_CODE is ANY number(50,60,70,90)
    except C and A then I have to show STUD_ABB_CODE from MASTER table.The out put should be like thisdecode(s_code)
    sid s_code DECODE(s_code)
    1    50     NRI
    2    50     NRI
    3    C      CLOSED
    4    A      ACTIVE
    4    40     NAF
    5    70     NAS
    5    90     MAS
    6    70     NASI have tried the following query but it's not working please help me.
    select s_id,s_code,
             DECODE (s_code,
                        'A', 'ACTIVE',
                        'C', 'CLOSED',DECODE(s_code,(SELECT DISTINCT stud_code
                        FROM student,master
                        WHERE s_code = stud_code
                        (SELECT DISTINCT stud_abb_code
                        FROM student,master
                        WHERE s_code = stud_code
                        from student;

    Hi,
    You are probably the most persistand member of this forum.
    user9077483
    Handle: user9077483
    Status Level: Newbie
    Registered: Jan 30, 2010
    Total Posts: 262
    Total Questions: 136 (136 unresolved)
    You never ever got any of your 135 quesations answered and still you ask again question number 136.
    Whow.
    Maybe this will be the first question you get a correct answer, if so please mark it as sush and give point to the correct answer.
    You can solve the problem of the missing A and C in the master table by simply adding them to that table. If you do not want to modify the master table you can do the following:
    with student (sid, s_code) as
    select 1,    '50' from dual union all   
    select 2,    '50' from dual union all
    select 3,    'C'  from dual union all
    select 4,    'A'  from dual union all
    select 4,    '40' from dual union all
    select 5,    '70' from dual union all
    select 5,    '90' from dual union all
    select 6,    '70' from dual
    1 50  
    2 50  
    3 C   
    4 A   
    4 40  
    5 70  
    5 90  
    6 70  
    ,master (stud_code, stud_abb_code )as
    select '40',        'NAF' from dual union all
    select '50',        'NRI' from dual union all
    select '60',        'CAM' from dual union all
    select '70',        'NAS' from dual union all
    select '80',        'RMA' from dual union all
    select '90',        'MAS' from dual
    40   NAF  
    50   NRI  
    60   CAM  
    70   NAS  
    80   RMA  
    90   MAS  
    ,master_1 as
    select stud_code, stud_abb_code  from master union all
    select 'A', 'ACTIVE' FROM DUAL UNION ALL
    select 'C', 'CLOSED' FROM DUAL
    STUD_CODE STUD_ABB_CODE
    40        NAF          
    50        NRI          
    60        CAM          
    70        NAS          
    80        RMA          
    90        MAS          
    A         ACTIVE       
    C         CLOSED       
    SELECT
      A.SID
      ,A.S_CODE
      ,B.STUD_ABB_CODE
    FROM
      STUDENT A LEFT OUTER JOIN
      MASTER_1 B ON A.S_CODE = B.STUD_CODE
    ORDER BY
      1,3
    SID S_CODE STUD_ABB_CODE
      1 50     NRI          
      2 50     NRI          
      3 C      CLOSED       
      4 A      ACTIVE       
      4 40     NAF          
      5 90     MAS          
      5 70     NAS          
      6 70     NAS          
    8 rows selected Regards,
    Peter

  • Help decoding query

    Hi
    I have a query running which will produce a report but i am having a problem trying to decode that actual query itself to figure out what is happening in it. The query contains a subquery in the FROM statement which is purely for the basis of decoding all date fields that are used. There is no FROM or WHERE statement in this query and the values retrieved from this query are then assigned to a variable. Following this subquery there is a number of other fields called but there is no select statement. Then underneath these fields there is another FROM statement. I have never seen this before. Has anyone any idea what is going on. Below is an example of the structure of the query. Any help at all would be appreciated.
    Thanks
    Michelle
    SELECT
    <Field>
    <Field>...................
    FROM
    (SELECT............
    END SUBQUERY)Report
    <Field>
    <Field>......................
    FROM
    <TABLE>
    <TABLE>
    (SELECT....
    FROM..... WHERE.....)K01(i am taking it that this is a variable to hold values)
    WHERE.................
    WHERE..............

    Considering the fact that you have given us absolutely NO information, consider the following:
    select a.tablespace_name,total,free,(total-free) used, ((total-free)/total)*100 pctused from
    (select tablespace_name,sum(bytes) total from dba_data_files group by tablespace_name ) a,
    (select tablespace_name,sum(bytes) free from dba_free_space group by tablespace_name) b
    where a.tablespace_name = b.tablespace_name;
    The "a" subquery is a virtual table of sorts, that will output the same fields as the subquery that can be accessed by a.tablespace_name, etc. There is no requirement that everything be a subquery or explicitly defined table.
    If this doesn't help, I would suggest actually posting something valid.

  • Query help needed

    Hi
    I need help in writing a sql query for the data given below
    Market Description Revenue
    LARGE CORPORATE 0.0
    LARGE CORPORATE 0.0
    OTHERS 0.0
    OTHERS 1.98
    LARGE CORPORATE 5.1299999999999999
    LARGE CORPORATE 6.8500000000000005
    LARGE CORPORATE 10.98
    LARGE CORPORATE 16.490000000000002
    LARGE CORPORATE 21.129999999999999
    LARGE CORPORATE 28.66
    LARGE CORPORATE 38.579999999999998
    OTHERS 68.420000000000002
    OTHERS 87.590000000000003
    LARGE CORPORATE 90.040000000000006
    LARGE CORPORATE 511.94
    LARGE CORPORATE 625.01999999999998
    LARGE CORPORATE 662.75999999999999
    LARGE CORPORATE 700.68000000000006
    LARGE CORPORATE 2898.6799999999998
    LARGE CORPORATE 3273.96
    OTHERS 3285.4400000000001
    LARGE CORPORATE 3580.0799999999999
    LARGE CORPORATE 4089.1900000000001
    LARGE CORPORATE 4373.5200000000004
    LARGE CORPORATE 16207.550000000001
    LARGE CORPORATE 19862.740000000002
    LARGE CORPORATE 33186.150000000001
    LARGE CORPORATE 107642.79000000001
    The output of query should be in the following format
    Market Description 1st 10%(revenue) 2nd 10%(revenue) 3rd 10%(revenue) 4th 10%(revenue) 5th 10%(revenue) rest 50%(revenue)
    Would appreciate any help on this query.

    Hi,
    What does 1st 10%, 2nd 10% etc. mean?
    Is it 0-10%, 11-20%, 21-30%....
    If so, combination of floor,decode and division should help to
    achieve the result.
    For example for the followint table,
    NAME SALE
    SALES_GROUP1 .2
    SALES_GROUP1 1
    SALES_GROUP1 9
    SALES_GROUP2 2.1
    SALES_GROUP2 12.2
    SALES_GROUP2 19.9
    SALES_GROUP3 22.2
    write,
    select name,decode(floor(sale/10),0,sale,null) "1st10%",
    decode(floor(sale/10),1,sale,null)"11to20%",
    decode(floor(sale/10),2,sale,null) "21to30%"
    from revenue;
    I mean, using floor and division, your rounding all values b/w 0 to 10 as 0
    Similarly, 11 to 20 has be rounded to 1 etc...then apply decode function it
    to achive the result.
    I hope this helps.
    Regards,
    Suresh
    8i OCP.

  • Query help,  Percentages / ratio to reports / nests

    Hi
    I have a query that returns data like this
    D_NO POINTS COUNT_POINTS ID_COUNT
    4002 L_T_69 12 282
    4219 L_T_69 1 151
    4228 L_T_69 18 193
    4229 L_T_69 7 181
    4230 L_T_69 0 197
    I need to also output a column that works out a percentage of count_points and Id_count. e.g 12/282 * 100 = 4.2
    I had a try with ratio to reports function but no joy for me. I think i need to add in
    another nested select or something but what i was trying wasnt working.
    Can anyone help.
    here is the query so far
    SELECT D_NO,
    GROUPS.POINTS,
    DECODE(GROUPS.POINTS, 'L_T_69' , L_T_69) AS COUNT_POINTS,
    ID_COUNT
    FROM
         (SELECT D_NO,
         Count (CASE WHEN VERBAL <= 69 THEN 1
              END) AS L_T_69,
         COUNT(ID_NUMBER) AS ID_COUNT
         FROM TBL_1
         WHERE VERBAL IS NOT NULL
         group by D_NO)
    TBL_1,
    ( SELECT 'L_T_69' POINTS FROM DUAL )GROUPS
    thank you

    Not sure if this is what you're looking for but it may give you some clues:
    select object_type
          ,has_a_c
          ,type_total
          ,round(100 * (has_a_c / type_total),2) ratio
    from
       select object_type
             ,sum (case when instr(object_name,'C') <> 0 then 1
                        else 0
                   end) has_a_c
             ,count(*) type_total
       from   all_objects
       group by object_type
    OBJECT_TYPE          HAS_A_C   TYPE_TOTAL   RATIO
    CONSUMER GROUP             1            2      50
    EVALUATION CONTEXT         1            1     100
    FUNCTION                  50          113   44.25
    INDEX                      7           20      35
    LIBRARY                    0            2       0
    OPERATOR                   1            2      50
    PACKAGE                  500         1158   43.18
    PACKAGE BODY             487         1126   43.25
    PROCEDURE                 54           86   62.79
    SEQUENCE                  62          116   53.45
    SYNONYM                 1060         2298   46.13
    TABLE                    365          721   50.62
    TABLE PARTITION           15           15     100
    TYPE                     104          272   38.24
    VIEW                     834         1896   43.99
    15 rows selected.

  • Query help to find missing rows

    Hi,
    Create tableA (
    columname varchar2;
    Insert into tableA ('a3');
    Insert into tableA ('dd');
    Select * from tablename where column in ( a3, b12,c34, dd ); -- 4 Values provided
    Need a query to return b12,c34 ie I have to find which records or not returning from a table for a given values.
    Please help
    Thanks
    Arvy
    Edited by: ARVY on Jun 19, 2012 11:10 AM
    Edited by: ARVY on Jun 19, 2012 11:16 AM

    ARVY wrote:
    I am using Oracle 9i versionFollowing is query that should be compatible on 9i.
    Please test it,since I am not able to do it due to unavailability of Oracle 9i with me.
    with data as
      select 'a,b,c,d' || ',' col from dual
    isolated_data as
      select trim(both ',' from (substr(col,
                  DECODE(level,
                          1, 1,
                          instr(col, ',', 1, level - 1) + 1
                  DECODE(level,
                          1, instr(col, ',', 1, level),
                          instr(col, ',', 1, level) - instr(col, ',', 1, level - 1)
                 ) ))col1
        from data
       connect by level <= length(col) - length(replace(col, ','))
    select a.col1
      from isolated_data a
    where a.col1 NOT IN (select col from test_Table);Regards,
    P.

  • Oracle query help

    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.8.0 - Production
    Report Builder 10.1.2.0.2
    ORACLE Server Release 10.1.0.4.2
    Oracle Procedure Builder 10.1.2.0.2
    Oracle ORACLE PL/SQL V10.1.0.4.2 - Production
    Oracle CORE    10.1.0.4.0    Production
    Oracle Tools Integration Services 10.1.2.0.2
    Oracle Tools Common Area 10.1.2.0.2
    Oracle Toolkit 2 for Windows 32-bit platforms 10.1.2.0.2
    Resource Object Store 10.1.2.0.2
    Oracle Help 10.1.2.0.2
    Oracle Sqlmgr 10.1.2.0.2
    Oracle Query Builder 10.1.2.0.2 - Production
    PL/SQL Editor (c) WinMain Software (www.winmain.com), v1.0 (Production)
    Oracle ZRC 10.1.2.0.2
    Oracle XML Developers Kit 10.1.0.4.2 - Production
    Oracle Virtual Graphics System 10.1.2.0.2
    Oracle Image 10.1.2.0.2
    Oracle Multimedia Widget 10.1.2.0.2
    Oracle Tools GUI Utilities 10.1.2.0.2
    I have enclosed sample data and also table structure. I need help in getting the query.
    select dept_id,proc_code,override_goal,goal_override_date
      from table2
    where goal_override_date between '02-jan-2014' and '11-jan-2014'
       and dept_id = 10
       and proc_code = 'CP'
    select DEPT_ID, PROC_CODE, DAY_SUNDAY, DAY_MONDAY,
           DAY_TUESDAY, DAY_WEDNESDAY, DAY_THURSDAY,
             DAY_FRIDAY, DAY_SATURDAY
      from table1
    where dept_id =10
    and proc_code = 'CP'; 
    Table1  is kind of maintenance table.
    In Table2 values can be overridden.
    Requirement
    Check to see if there is data in table 2 for the date range . If table2 has no value then take value from table1 for that day the date falls into. Any more clarification please ask me.
    Sundays are all zeros.
    I want this data. and the sum for the date range.
    2-jan-2014  - 3
    3-jan-2014  - 3
    4-jan-2014  - 3
    5-jan-2014  - 0
    6-jan-2014  - 1
    7-jan-2014  - 3
    8-jan-2014  - 5
    9-jan-2014  - 5
    10-jan-2014 - 3
    11-jan-2014 - 3
    Sum for the date range has to be 29
    Sample table and data
    CREATE TABLE TABLE1
      DEPT_ID NUMBER NOT NULL,
      PROC_CODE VARCHAR2(2 BYTE) NOT NULL,
      DAY_SUNDAY NUMBER(4) NOT NULL,
      DAY_MONDAY NUMBER(4) NOT NULL,
      DAY_TUESDAY NUMBER(4) NOT NULL,
      DAY_WEDNESDAY NUMBER(4) NOT NULL,
      DAY_THURSDAY NUMBER(4) NOT NULL,
      DAY_FRIDAY NUMBER(4) NOT NULL,
      DAY_SATURDAY NUMBER(4) NOT NULL
    Insert into TABLE1
      (DEPT_ID, PROC_CODE, DAY_SUNDAY, DAY_MONDAY, DAY_TUESDAY,
      DAY_WEDNESDAY, DAY_THURSDAY, DAY_FRIDAY, DAY_SATURDAY)
    Values
      (10, 'CP', 0, 3, 3,
      3, 3, 3, 3);
    COMMIT;
    CREATE TABLE TABLE2
      DEPT_ID NUMBER NOT NULL,
      PROC_CODE VARCHAR2(2 BYTE) NOT NULL,
      OVERRIDE_GOAL NUMBER(4),
      GOAL_OVERRIDE_DATE DATE NOT NULL
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 1, TO_DATE('01/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 5, TO_DATE('01/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 5, TO_DATE('01/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;
    Help is highly appreciated.

    SELECT dates,
           override_goal,
           SUM(override_goal) OVER()
           FROM(SELECT dates,
                       CASE WHEN EXISTS(SELECT 1
                                        FROM table2 t2
                                        WHERE t2.goal_override_date = qry1.dates
                                        AND t2.dept_id = 10
                                        AND t2.proc_code = 'CP')
                            THEN (SELECT override_goal
                                  FROM table2 t2
                                  WHERE t2.goal_override_date = qry1.dates
                                  AND t2.dept_id = 10
                                  AND t2.proc_code = 'CP')
                       ELSE (SELECT DECODE(days,'SUN',day_sunday,
                                                'MON',day_monday,
                                                'TUE',day_tuesday,
                                                'WED',day_wednesday,
                                                'THU',day_thursday,
                                                'FRI',day_friday,
                                                'SAT',day_saturday )
                              FROM table1)
                       END as override_goal
                FROM (SELECT in_dt1+(LEVEL-1) dates,
                             TO_CHAR(in_dt1+(LEVEL-1),'DY') days
                      FROM(SELECT TO_DATE(&from_date,'DD-MON-YYYY') in_dt1,
                                  TO_DATE(&to_date,'DD-MON-YYYY') in_dt2
                                 FROM dual)
                      CONNECT BY LEVEL <= (in_dt2 - in_dt1)+1) qry1);
    Now run the query it will prompt you for the inputs. You can pass the date values and check the result. Otherwise replace &from_date,&to_date with user inputs

  • Simple query help in plsql - help

    oracle version : 10gR2
    indexes are created on each column, is there anyway to make them used while searching for the records rewriting the following query to test given data in any case (lower ,upper)...
    SELECT * FROM TX_USERS
    WHERE userid like decode( UPPER('Md'),null,userid, UPPER( 'MD')||'%' ) and
    first_name like decode(UPPER('Na'),null, first_name, UPPER( 'NA')||'%' ) AND
    LAST_name like decode(('Ra'),null, LAST_name, UPPER('RA')||'%' )
    -- list goes on..
    UPPER('Md') -- is the input values comes from form.. for example i_userid.. this query works fine .. is there anyway of getting indices used without using functional based indexing when we rewrite query like shown below??? input parameter valeus can be anything and table column values can be anything i.e. anycase (upper or lower or mix of both)..
    actual code would be
    upper(userid) like decode( UPPER(i_userid),null,userid, UPPER( i_userid)||'%' ) and
    upper(first_name) like decode(UPPER(i_first_name),null, first_name, UPPER( i_firstname)||'%' )
    if we put upper(userid) then index not used ..........anyway of rewriting using it or any other technique... or any other new way

    No, its not working... see the below..
    create table test5 as select owner, object_name, subobject_name, object_type from all_objects;
    create unique index test5_i5 on test5 (owner, object_name, subobject_name, object_type);
    select * from test5 where owner like 'SCOTT' AND OBJECT_NAME LIKE 'EMP';
    INDEX RANGE SCAN| TEST5_I5 | 4 | 248 | 1 (0)| 00:00:01 |
    but when i use
    select * from test5 where UPPER(OWNER) LIKE 'SCOTT%' AND UPPER(OBJECT_NAME) LIKE 'EMP%';
    TABLE ACCESS FULL| TEST5 | 3 | 186 | 65 (5)| 00:00:01 |
    i know it goes to full scan, i want to know is there any other way to make index used .. without using functional based indx...
    the reason is user can search any one of the column data and data is mixed case in table columns and/or conditions specified in query..
    .. any help...
    not sure how to use 'NLS_SORT=BINARY_CI' on multicolumn index and enable index used in search operation.. ANY OTHER WAY OF DOING THIS...
    requirements is
    mixed (lower,upper) data stored in db columns and mixed case data searched, 5 columns specified in where condition, data may be provided in search conditon to one or two or to all 5 columns in mixed case... matching records need to be returned.. suggest a good way of doing this... thnx

  • Spatial query help

    Can anyone help, I'm trying to run the following query but all columns are returning nulls. I know there is data for the first 2 tables but the third table contains no data. In a live situation sometimes the table will contain data, sometimes it wont, how can I get it to the return the data that is there? Normally would do an outer join but can you do that with the sdo_relate operator? Tried various nvl and decodes too.
    thanks !
    select p.ident PRN, wqz.ident WQZ_ID, nvl(ced.ident,'dont know')
    from property p, wqzone wqz, ced
    where sdo_relate(wqz.geometry,p.geometry,'MASK=ANYINTERACT QUERYTYPE=WINDOW') = 'TRUE'
    and sdo_relate(ced.geometry,p.geometry,'MASK=ANYINTERACT QUERYTYPE=WINDOW') = 'TRUE'
    and p.ident = 765432

    If you want to join the whole "property" table, you can try to run the select below.
    I don't know how many rows you must quering. On my tables this query works fine up to 1000 records.
    good job, regards
    Carl
    select
    p.ident PRN, wqz.ident WQZ_ID, nvl(ced.ced_ident,'dont know')
    from property p, wqzone wqz
    , (select B.ident p_ident, A.ident ced_ident from ced A, property B
    where sdo_relate(A.geometry, B.geometry,
    'MASK=ANYINTERACT QUERYTYPE=WINDOW') = 'TRUE') ced
    where p.ident = ced.p_ident (+)
    and sdo_relate(wqz.geometry,p.geometry
    ,'MASK=ANYINTERACT QUERYTYPE=WINDOW') = 'TRUE'

Maybe you are looking for

  • Upgrade my Bondi Blue G3 to OS 9.?

    Hi: The kids pulled out my old Bondi Blue G3. I have reinstalled all the original discs and so it is running on 8.5 (not 8.6....I am having a hard time trying to figure out how to upgrade now to 8.6...I'll take suggestions on this issue too, unable t

  • Need help with choosing a 6 cell and 9 cell battery (mo06, mo09)

    Hey hi all, I have a pavilion m6 series notebook which comes with mo06(6 cell) battery by default. I need to replace the battery with a new one and I've come across a mo09 (9 cell) battery with the same price as that of mo06. I've no idea if mo09 bat

  • STATIC with Dell Laptop and Inspire T7900

    I've been getting really annoying interference whenever I plug in my speakers. I also have a problem getting a strong connection through the?output socket in the laptop(alot of wiggling the cable to get half decent sound). And lastly the laptop can o

  • The song... Could not be used because the original file cannot be found....Any suggestions?

    I just got a new computer and I transfered all of my old music via an external hard drive to the new one.  All the music showed up in itunes fine so I deleted the old files.  Then I went to sync my iphone I got an error that said "The song... Could n

  • Paid / Free versions of an App and the reserved name

    Hi, lets say i have a Paid version of an app(w/ trial) and a Free version(Ad Supported), how am i supposed to deal with the "name reservation" thing? First, why having a Paid(w/ trial) and Free(w/ ads)? Well, simply because offering a Trial(free ad s