help in the application without using the pivot function

Hi gurus,

Can you please help write the query without using the PIVOT function.
 WITH indata AS
         (SELECT 1 sn, '123:456,789,323:456,213,344,345:5454' x
            from dual
          UNION ALL
          SELECT 2, 'abcd:fgrfr,rfrf,rfred,tg:tg,tg:ophhh,op,vdfgbh:poijn' x from dual),
        instr AS
         (SELECT a.SN,
                 rownum RN,
                 B.column_value || CASE
                   WHEN B.column_value NOT LIKE '%:%' THEN
                    ':'
                 END column_value 
            FROM indata a,
                 TABLE(CAST(multiset
                            (SELECT trim(SUBSTR(x,
                                                (CASE LEVEL
                                                  WHEN 1 THEN
                                                   1
                                                  ELSE
                                                   instr(x, ',', 1, LEVEL - 1) + 1
                                                END),
                                                (CASE instr(x, ',', 1, LEVEL)
                                                  WHEN 0 then
                                                   4000
                                                  ELSE
                                                   instr(x, ',', 1, LEVEL) - 1 -
                                                   (CASE LEVEL
                                                      WHEN 1 THEN
                                                       0
                                                      ELSE
                                                       instr(x,
                                                             ',',
                                                             1,
                                                             level - 1)
                                                    END)
                                                END)))
                               FROM dual
                             CONNECT BY level <=
                                        LENGTH(x) -
                                        LENGTH(REPLACE(x, ',', '')) + 1) AS
                            sys.odcivarchar2list)) b)
        SELECT  col1_val2,
                col1_val1,
                col2_val2,
                col2_val1,              
               systimestamp
          FROM (select SN,
                      ROW_NUMBER() over(partition by SN order by RN) RN,
                       SUBSTR(column_value, INSTR(column_value, ':') + 1) VAL1,
                       substr(column_value, 1, instr(column_value, ':') - 1) val2
                  FROM instr ) PIVOT(MAX(VAL1) VAL1, MAX(VAL2) VAL2 FOR SN IN(1 as col1,2 as col2));
Thanks in advance!...

Any help would be appreciated.

user590978 wrote:

Can you please help write the query without using the PIVOT function.

with indata as (
                select 1 sn,'123:456,789,323:456,213,344,345:5454' x from dual union all
                select 2, 'abcd:fgrfr,rfrf,rfred,tg:tg,tg:ophhh,op,vdfgbh:poijn' x from dual
               ),
         t1 as (
                select  x
                  from  indata
                  where sn = 1
               ),
         t2 as (
                select  x
                  from  indata
                  where sn = 2
               ),
         t3 as (
                select  level lvl,
                        substr(
                               x,
                               instr(',' || x,',',1,level),
                               instr(x || ',',',',1,level) - instr(',' || x,',',1,level)
                              ) col1
                  from  t1
                  connect by level <= length(x) - length(translate(x,'A,','A')) + 1
               ),
         t4 as (
                select  level lvl,
                        substr(
                               x,
                               instr(',' || x,',',1,level),
                               instr(x || ',',',',1,level) - instr(',' || x,',',1,level)
                              ) col2
                  from  t2
                  connect by level <= length(x) - length(translate(x,'A,','A')) + 1
               ),
         t5 as (
                select  lvl,
                        substr(col1,1,instr(col1 || ':',':') - 1) col1_val1,
                        substr(col1,instr(col1 || ':',':') + 1) col1_val2
                  from  t3
               ),
         t6 as (
                select  lvl,
                        substr(col2,1,instr(col2 || ':',':') - 1) col2_val1,
                        substr(col2,instr(col2 || ':',':') + 1) col2_val2
                  from  t4
               )
select  col1_val1,
        col1_val2,
        col2_val1,
        col2_val2,
        systimestamp
  from      t5
        full join
            t6
        on (
            t6.lvl = t5.lvl
           )
  order by coalesce(t5.lvl,t6.lvl)
/

COL1_VAL1  COL1_VAL2  COL2_VAL1  COL2_VAL2  SYSTIMESTAMP
---------- ---------- ---------- ---------- -------------------------------------
123        456        abcd       fgrfr      23-OCT-12 11.50.12.015000 AM -04:00
789                   rfrf                  23-OCT-12 11.50.12.015000 AM -04:00
323        456        rfred                 23-OCT-12 11.50.12.015000 AM -04:00
213                   tg         tg         23-OCT-12 11.50.12.015000 AM -04:00
344                   tg         ophhh      23-OCT-12 11.50.12.015000 AM -04:00
345        5454       op                    23-OCT-12 11.50.12.015000 AM -04:00
                      vdfgbh     poijn      23-OCT-12 11.50.12.015000 AM -04:00

7 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

  • How to use the Pivot function for range group in oracle SQL

    Hello

    Hello!!!

    I need to display the data in the format below. There are 2 columns 1 is State and another is rate.

    State < 100 100-199, 200-299 300-399 400-499, 500-599 600-699 700-799 800-899 900-999 > = 1000 Total
    AK 1 2 0 4 1 4 4 35 35 4 1 25
    AL 0 0 2 27 10 17 35 2 2 35 0 103
    AR 0 0 1 0 0 2 2 13 13 2 0 6
    AZ 0 1 2 14 2 14 13 3 3 13 0 57
    CA     0     0     1     6     2     7     3     4     4     3     0     34

    Developed the sub query but unable to use the beach on the pivot function. Please help on this.

    (select (SELECT short_description
    OF CODE_VALUES
    WHERE CODE_TYPE_CODE = ad. STATE_TYPE_IND_CODE
    AND VALUE = ad. STATE_CODE
    ) STATE,
    Nr.rate
    N-NEUTRAL
    c contacts,
    announcement of addresses,
    xref_contacts_addresses xca,
    neutral_rates nr
    where n.contact_id = c.contact_id
    and n.address_id = ad.address_id
    and xca.address_id = ad.address_id
    and xca.contact_id = c.contact_id
    and nr.contact_id = n.contact_id
    and nr.rate_frequency = 'HOUR')

    Like this

    with t
    as
    (
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 67 value from dual union all
    select 'AL' state, 23 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 78 value from dual union all
    select 'AL' state, 34 value from dual union all
    select 'AL' state, 4 value from dual union all
    select 'AL' state, 12 value from dual union all
    select 'AL' state, 15 value from dual union all
    select 'AZ' state, 6 value from dual union all
    select 'AZ' state, 123 value from dual union all
    select 'AZ' state, 123 value from dual union all
    select 'MA' state, 23 value from dual union all
    select 'MA' state, 120 value from dual union all
    select 'MA' state, 456 value from dual union all
    select 'MA' state, 11 value from dual union all
    select 'MA' state, 24 value from dual union all
    select 'MA' state, 34 value from dual union all
    select 'MA' state, 87 value from dual union all
    select 'MA' state, 23 value from dual union all
    select 'MA' state, 234 value from dual union all
    select 'MA' state, 789 value from dual union all
    select 'MH' state, 54321 value from dual
    )
    -- End of test data
    select state,
           nvl(count(decode(value, 0, 0)), 0) "<100",
           nvl(count(decode(value, 1, 1)), 0) "100-199",
           nvl(count(decode(value, 2, 2)), 0) "200-299",
           nvl(count(decode(value, 3, 3)), 0) "300-399",
           nvl(count(decode(value, 4, 4)), 0) "400-499",
           nvl(count(decode(value, 5, 5)), 0) "500-599",
           nvl(count(decode(value, 6, 6)), 0) "600-699",
           nvl(count(decode(value, 7, 7)), 0) "700-799",
           nvl(count(decode(value, 8, 8)), 0) "800-899",
           nvl(count(decode(value, 9, 9)), 0) "900-999",
           nvl(count(decode(value, 10, 10)), 0) ">=1000"
      from (
              select state, case when value < 100 then 0
                                 when value between 100 and 199 then 1
                                 when value between 200 and 299 then 2
                                 when value between 300 and 399 then 3
                                 when value between 400 and 499 then 4
                                 when value between 500 and 599 then 5
                                 when value between 600 and 699 then 6
                                 when value between 700 and 799 then 7
                                 when value between 800 and 899 then 8
                                 when value between 900 and 999 then 9
                                 when value >= 1000 then 10
                            end value
                from t
           )
     group
        by state
    
  • When I click on "View history" I completely lose the Firefox menu bar, I can't even quit the application without using a keyboard shortcut.

    The question describes all this: when I select "view history", the menu bar disappears, so I have no way of navigation, there is no arrow to the left, nothing. I quit Firefox, but only using the Mac keyboard shortcut. I can go back to a previous window only by moving the historic window and I can close this previous window - there is a button for this - but there is NOTHING that will allow me to change, or close, the historical window. My only option is to leave the program.

    Both of your corrections helped. The first reminded me that I could simply use command-W to close the library window, because it did not show a close"" button. But your second response on rename these two files really made the tour of Firefox back to his old self once more. Now, everything is back to normal. Thank you.

  • Help with fft vibrations without using the package of noise and vibrations

    I'm looking for help in the analysis of vibrations. I use an example updated NI 9233 VI, to get a signal from the accelerometer for display using a FFT power spectrum. I'm not entirely sure if it works, because it's the first time I've ever done vibration analysis on LabView. So if you could explain a thing or two about vibrations or TFF, I'd be more than willing to hear from you. I have included my code along with a photo of an analysis of vibration of the computer, I work with. (even when I don't know if his work that I just thought it would be good to show an output)

    Brandon

    Data sheet:

    I have LabView 2011

    I FPGA, real-time

    I have a model of research of Wilcoxon accelerometer 797-33

    With an NI 9233

    On a cRio-9012

    Hi Brandon,.

    You can use the FFT Complex (photo attached) to calculate the magnitude of the acceleration at different frequencies. You will need to take a little further to build a new waveform with this release, which includes d0, df and the output of the FFT. In order to calculate the df, please refer to the user manual on page 10-3. With respect to the scale that is output by the FFT, it must be same as input. Hope this helps to answer your questions. Thank you!

    See you soon,.

    CARISA Leal

  • Need help with the analytic function

    I want to get the highest employee details and the 2nd highest employee for a particular service. But also the Department should have more than 1 employee.
    I tried the query and it gave me the correct results. But I wonder if there is another solution than to use the subquery.

    Here is the table and the query result:
    with t as
    (
    select 1 emp_id,3 mgr_id,'Rajesh' emp_name,3999 salary,677 bonus,'HR' dpt_nme from dual union
    select 2 ,3 ,'Gangz',4500,800,'Finance' from dual  union
    select 3 ,4 ,'Sid',8000,12000,'IT' from dual  union
    select 4 ,null,'Ram',5000,677,'HR' from dual  union
    select 5 ,4,'Shyam',6000,677,'IT' from dual union
    select 6 ,4 ,'Ravi',9000,12000,'IT' from dual   
    )
    select * from 
    (select emp_id, mgr_id, emp_name, dpt_nme, salary, row_number() over (partition by dpt_nme order by salary desc) rn from t where dpt_nme in 
    (select dpt_nme from t group by dpt_nme having count(*) > 1)) where rn < 3

    Hello

    You need a subquery, but you don't need more than that.
    Here's a way to eliminate the additional subquery:

    WITH     got_analytics     AS
    (
         SELECT  emp_id,     mgr_id,     emp_name, dpt_nme, salary
         ,     ROW_NUMBER () OVER ( PARTITION BY  dpt_nme
                                   ORDER BY          salary     DESC
                           )         AS rn
         ,     COUNT (*)     OVER ( PARTITION BY  dpt_nme
                                       )         AS dpt_cnt
         FROM     t
    )
    SELECT  emp_id,     mgr_id,     emp_name, dpt_nme, salary
    ,     rn
    FROM     got_analytics
    WHERE     rn     < 3
    AND     dpt_cnt     > 1
    ;
    

    Analytical functions are calculated after the clause WHERE is applied. Since we need to use the results of the analytical ROW_NUMBER function in a WHERE clause, which means that we have to calculate ROW_NUMBER in a subquery and use the results in the WHERE clause of the main query. We can call the COUNT function analytical in the same auxiliary request and use the results in the same WHERE clause of the main query.

    Would what results you if there is a link for the 2nd highest salary in some Department? For example, if you add this line to your sample data:

    select 7 ,3 ,'Sunil',8000,12000,'IT' from dual  union
    

    ? You can use RANK rather than ROW_NUMBER.

  • I need a little help with the INSTR function

    Hello everyone,

    im still new to pl sql and experienced an issue with the INSTR function. I need to analyze some values of a CLOB containing the xml code and use INSTR to determine the beginning and end of the value, I would like to analyze.

    the data I need is in a <!-[CDATA [[Some Text]]]-> block. up to this point, I managed to take the start of the word, but I can't seem to capture the end of the CDATA block. I use this statement:

    POS: = INSTR (TO_CHAR (input_clob) [,'] ', pos2 + 6, 1);

    but it doesn't have the desired effect. [It seems to sort of go back to the length of the string, while the]-sign is actually in the xml file. my result would look to "Some Text]]--> < xmlxml >... < / xmlxml >...» ». [I tried to escape by writing ' \] "but if I do, the function returns zero and my parsed string is left blank.

    Unfortunately I can't use regular expressions, because I work with oracle 9.2i. Anyone know what I'm doing wrong?

    Thank you

    Edited by: user8719779 the 25.08.2009 02:23
    with my_tab as (select ''||chr(10)||
                           '  '||chr(10)||
                           '   '||chr(10)||
                           '  '||chr(10)||
                           '' col1 from dual)
    -- end of mimicking your data; USE SQL below:
    select substr(col1, instr(col1, '', 1, 1) - instr(col1, 'I try to use SUBSTR and INSTR function to parse a variable without breaking to the top of the variable. As you can see that the problem is space, negotiating
     
    DECLARE
         blank_space NUMBER(2);
         full_name VARCHAR2(30) := 'Robert P. Simmons');
         first_name VARCHAR2(30);
         last_name VARCHAR2(30);
         
    BEGIN 
         blank_space := INSTR(full_name, ' ');
         first_name := SUBSTR(full_name, 1, (blank_space -1));
         DBMS_OUTPUT.PUT_LINE( 'Your first name is ' || first_name);
         last_name := SUBSTR(full_name, (blank_space + 1),
         (LENGTH(full_name) - blank_space));                
         DBMS_OUTPUT.PUT_LINE( 'You have ' || 
         LENGTH(last_name) || ' characters in your last name!');
         
    END;     
    How can I enter the number of characters in name only? It lists just to the right of the 1st blank_space.

    Thank you
    SQL> declare
       blank_space   number (2);
       full_name     varchar2 (30) := 'Robert P. Simmons';
       first_name    varchar2 (30);
       last_name     varchar2 (30);
    begin
       blank_space := instr (full_name, ' ');
       first_name := substr (full_name, 1, (blank_space - 1));
       dbms_output.put_line ('Your first name is ' || first_name);
    
       dbms_output.put_line ('You have ' || length (substr (full_name, instr (full_name, ' ', -1) + 1)) || ' characters in your last name!');
    end;
    /
    Your first name is Robert
    You have 7 characters in your last name!
    PL/SQL procedure successfully completed.
    
  • Please help fix the Decode function

    DECODE (p_resp_key,'|) "' AP" ' |', LINT_LOGI_STG_ITEM_DETAILS_PKG. Description of the AS GET_AP_ITEM_CATEGORY_DTL (ol.inventory_item_id), ldmf_istore_order_pkg.get_wwdb_prod_description (ol.inventory_item_id))

    I'm getting errors during execution analysis. I believe that something is incorrect in Decode statement. I call this statement of Decopde in the package. Help, please.

    Concerning
    M

    I think I've found the error:

    p_resp_key is a parameter to your home...

    If it could work.

     := 'select ... DECODE('''||p_resp_key||''',''AP'',LINT_LOGI_STG_ITEM_DETAILS_PKG.GET_AP_ITEM_CATEGORY_DTL(ol.inventory_item_id),ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id)) AS description ...'
    

    I still very strongly suggest to do not to use the dynamic version of this code. You have a couple of potential Sql Injection issues in there and it will be better if its not done dynamically.

    Published by: W. Sven on September 11, 2009 14:15

    Published by: W. Sven on September 11, 2009 14:17

  • Need help with the DECODE function

    Hello

    I try to use by default within the decode service and whenever I get a missing expression. I searched everywhere and can not know what I am doing wrong. Thank you

    Select decode (request_id, 0, "no file found", by DEFAULT)
    select decode (request_id,0,'No files found', request_id)...
    
  • With the help of the index function based! =

    Hello

    Is it possible to create an index of basic function that is used when the! = operator is used?

    I have a question with the following in the where clause

    WHERE (SYMBOL = 1 AND LOGICAL_STATUS! = '8')

    Thank you

    Carl

    Published by: Carl Holmes on November 5, 2009 10:18

    But you can create a FBI such as the following which will be a NULL entry when the condition is not put in correspondence:

    CREATE INDEX .... ON TABLE .... (CASE WHEN SYMBOL=1 AND LOGICAL_STATUS !='8' THEN 1 ELSE NULL END);
    

    Then you might change your SQL to match the FBI:

    WHERE CASE WHEN SYMBOL=1 AND LOGICAL_STATUS !='8' THEN 1 ELSE NULL END = 1
    

Maybe you are looking for

  • Videos, play is not in Firefox

    When I try to play videos on sites like YouTube or Newsy.com I just get a white screen where the video should be. No error message.

  • Update of XP home and fake win 7

    a friend sent computer to another to upgrade to win 7 he returned with the recovery here disc exotic win 7 is broken, what can I do to fix

  • HP 6500 has more wireless printer = &gt; hp eprint does not not with pdf

    I have a brand new HP 6500 has more the wireless printer. I tried to print using hp eprint Centre by sending a pdf file as an attachment to the e-mail address of printers. But the printer don't like pdf and print an error instead, with the message be

  • U3415w Dual Audio

    Hello I have two U3415w connected to a Lenovo docking station. The docking station has two Displayports and one HDMI. Display #1 is connected via HDMI from the dock to the monitor, display #2 is connected via a HDMI cable to DisplayPort adapter then

  • Creative cloud does not show installed applications

    Several weeks ago my creative cloud just kept loading of and. So, I found some info from Adobe and reinstalled. Once I got it going, he is more showed State of the apps I had installed except one - it's show Acrobat DC in the list "installed". I have