Row to column - no pivot

Hi all,
I have data in this format:
USER_ID | TOP10_1 | TOP10_2 | TOP10_3 | ... | TOP10_10
USER1 | 1 | 2 | 3 | ... | 10
USER2 | 4 | 5 | 10 | ... | 1
USER3 | 3 | 6 |1 | ... | 2
I need to have this view
USER_ID | USER1 | USER2 | USER3
TOP10_1 | 1 | 4 | 3
TOP10_2 | 2 | 5 | 6
TOP10_3 | 3 | 10 | 1
TOP10_10 | 10 | 1 | 2
I didn't get it managed with what I found here, because I have no grouping. And I'm not very firm in this stuff.
I'm working in 10g.
Thanks for your help.
chrissy

If you were happy for the USER_ID's to come out as data rather than the actual column names then you could do something like:
SQL> ed
Wrote file afiedt.buf
  1  with t as (select 'USER1' as USER_ID, 1 as top10_1, 2 as top10_2, 3 as top10_3, 4 as top10_4, 5 as top10_5, 6 as top10_6, 7 as top10_7, 8 as top10_8, 9 as top10_9, 10 as top10_10 from dual union all
  2             select 'USER2', 4, 5, 10, 2, 4, 6, 8, 10, 9, 1 from dual union all
  3             select 'USER3', 3, 6, 1, 3, 5, 7, 9, 8, 6, 2 from dual)
  4  --
  5  -- end of test data
  6  --
  7      ,x as (select user_id, row_number() over (order by user_id) as rn from t)
  8  select 'USER_ID' as c0
  9        ,max(decode(rn,1,user_id)) as c1
10        ,max(decode(rn,2,user_id)) as c2
11        ,max(decode(rn,3,user_id)) as c3
12  from x
13  union all
14  select c0, c1, c2, c3
15  from (
16        select 'TOP10_'||z.rn as c0
17              ,to_char(max(decode(y.rn,1,decode(z.rn,1,top10_1,2,top10_2,3,top10_3,4,top10_4,5,top10_5,6,top10_6,7,top10_7,8,top10_8,9,top10_9,10,top10_10)))) as c1
18              ,to_char(max(decode(y.rn,2,decode(z.rn,1,top10_1,2,top10_2,3,top10_3,4,top10_4,5,top10_5,6,top10_6,7,top10_7,8,top10_8,9,top10_9,10,top10_10)))) as c2
19              ,to_char(max(decode(y.rn,3,decode(z.rn,1,top10_1,2,top10_2,3,top10_3,4,top10_4,5,top10_5,6,top10_6,7,top10_7,8,top10_8,9,top10_9,10,top10_10)))) as c3
20        from t     join x on (x.user_id = t.user_id)
21                   join (select rownum rn from dual connect by rownum <= 3) y on (y.rn = x.rn)
22             cross join (select rownum rn from dual connect by rownum <= 10) z
23        group by z.rn
24        order by z.rn
25*      )
SQL> /
C0                   C1                   C2                   C3
USER_ID              USER1                USER2                USER3
TOP10_1              1                    4                    3
TOP10_2              2                    5                    6
TOP10_3              3                    10                   1
TOP10_4              4                    2                    3
TOP10_5              5                    4                    5
TOP10_6              6                    6                    7
TOP10_7              7                    8                    9
TOP10_8              8                    10                   8
TOP10_9              9                    9                    6
TOP10_10             10                   1                    2
11 rows selected.
SQL>

Similar Messages

  • Dates against rows and columns in pivot require two date dimensions???

    Hi,
    I have an Excel pivot table connected to a SSAS tabular cube.Now For this Report i want the row label area of pivot to contain Shipping Month and column label area contains the Order Months.Do i need two date dimensions for this purpose?? What is the way
    to do such kinds of Reports where the row as well as columns contain date dimension???
    Thanks.

    Hi Nimo0102,
    Based on your description, you want Shipping Month to be shown on row label, and Order Months to be shown on the column label, right? When connecting to a data base on an Excel pivot table, if we want to the dimension appear on the row or column label, we
    can drag the dimension to ROWS or COLUMNS. So in your scenario, if you have Shipping Month Order Months on your cube, you can drag it to ROWS or COLUMNS directly. Otherwise, I am afraid you need to create them on the cube.
    If I have anything misunderstood, please point it out.
    Regards,
    Charlie Liao
    TechNet Community Support

  • Rows To Columns Or Pivot Table with SQL Model over two tables

    hi @all
    oracle 10.2.0.4
    i have following situation
    table_1
    t1_id   t1_name
    1      reggie
    table_2
    t2_id   t2_t1_id   t2_start_date   t2_end_date
    1      1      01.01.2008      31.03.2008
    2      1      01.06.2008      31.07.2008
    cardinality from table_1 to table_2 is one to many
    now i need this
    t1_name   t2_start_date_1   t2_end_date_1   t2_start_date_2   t2_end_date_2   t2_start_date_x   t2_end_date_x
    reggie      01.01.2008      31.03.2008      01.06.2008      31.07.2008
    i think the max occurrence of start/enddate can be 15
    can somebody help

    Hello,
    One way, assuming 15 is the maximum:
    SELECT t1.name,
            t2.t2_start_date t2_start_date_1,   t2.t2_end_date t2_end_date_1,
            LEAD(t2.t2_start_date,1) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date) t2_start_date_2,
            LEAD(t2.t2_end_date,1) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_2,
            LEAD(t2.t2_start_date,2) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_3,
            LEAD(t2.t2_end_date,2) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_3,
            LEAD(t2.t2_start_date,3) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_4,
            LEAD(t2.t2_end_date,3) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_4,
            LEAD(t2.t2_start_date,4) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_5,
            LEAD(t2.t2_end_date,4) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_5,
            LEAD(t2.t2_start_date,5) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_6,
            LEAD(t2.t2_end_date,5) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_6,
            LEAD(t2.t2_start_date,6) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_7,
            LEAD(t2.t2_end_date,6) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_7,
            LEAD(t2.t2_start_date,7) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_8,
            LEAD(t2.t2_end_date,7) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_8,
            LEAD(t2.t2_start_date,8) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_9,
            LEAD(t2.t2_end_date,8) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_9,
            LEAD(t2.t2_start_date,9) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_10,
            LEAD(t2.t2_end_date,9) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_10,
            LEAD(t2.t2_start_date,10) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_11,
            LEAD(t2.t2_end_date,10) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_11,
            LEAD(t2.t2_start_date,11) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_12,
            LEAD(t2.t2_end_date,11) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_12,
            LEAD(t2.t2_start_date,12) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_13,
            LEAD(t2.t2_end_date,12) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_13,
            LEAD(t2.t2_start_date,13) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date)t2_start_date_14,
            LEAD(t2.t2_end_date,13) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date)t2_end_date_14,
            LEAD(t2.t2_start_date,14)  OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_start_date) t2_start_date_15,        
            LEAD(t2.t2_end_date,14) OVER (PARTITION BY t2.t2_t1_id ORDER BY t2.t2_end_date) t2_end_date_15
      FROM table_1 t1
      JOIN table_2 t2 ON (t1.t1_id = t2.t2_t1_id)
    WHERE (t2.t2_start_date,   t2.t2_end_date) IN (
      SELECT MIN(t2_start_date), MIN(t2_end_date)
        FROM  table_2 t22
      WHERE t2.t2_id = t22.t2_id);
    Edit
    Fixed incomplete code

  • How to convert data from rows into columns

    Hi,
    I have a sql table and the data looks like this
    GLYEAR GLMN01 GLMN02 GLMN03 GLMN04
    2007 -109712.40 6909.15 4758.72 56.88
    2007 -13411.32 19132.9 -5585.07 4362.64
    Where GLyear reprsents Year and GLMN01 is February, GLMN02 is March and so on,
    Now i want my output to be something like this which i want to insert into another table
    GLYear GLMonth GLAmount
    2007 February -109712.40
    2007 March 6909.15
    2007 April 56.88
    My new table has 3 columns, GLYear,GLMonth,GLAmount.
    Can someone please help me with the select statement on how to do this, i can work with the inserts.
    Thanks.

    I want you to check these form tread they have the same discussion as you.  They will definitely solve your problem
    http://blog.jontav.com/post/8344518585/convert-rows-to-columns-columns-to-rows-in-sql-server
    http://dba.stackexchange.com/questions/19057/convert-rows-to-columns-using-pivot-in-sql-server-when-columns-are-string-data
    http://stackoverflow.com/questions/18612326/how-to-convert-multiple-row-data-into-column-data-in-sql-server
    I hope this helps you in solving your problem. 
    Please remember to click “Mark as Answer” on the post that has answered your question as it is very relevant to other community members dealing with same problem in seeking the right answer

  • Convert rows into columns nad vice versa in 10g

    how to convert rows into columns in 10g??

    Qwerty wrote:
    see below for rows to column case
    SQL> WITH t as
    2      (
    3       SELECT 'US' test_string FROM DUAL UNION
    4       SELECT 'AMERICA'  FROM DUAL UNION
    5       SELECT'HOLLYWOOD'  FROM DUAL UNION
    6       SELECT 'WASHINGTON'  FROM DUAL
    7      )
    8      select ltrim (sys_connect_by_path(test_string,','),',') test_string
    9        from (
    10     SELECT row_number() over(order by test_string) rno, test_string
    11       FROM t)
    12       WHERE connect_by_isleaf = 1 and rownum=1
    13       connect by rno = prior rno+1;
    TEST_STRING
    AMERICA,HOLLYWOOD,US,WASHINGTONI hope you can do it for column to rows now.That's not really rows to columns. That's rows to a column, which is more commonly called string aggregation.
    Rows to columns (or pivot) is more like:
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as
      2       (
      3        SELECT 'US' test_string FROM DUAL UNION
      4        SELECT 'AMERICA'  FROM DUAL UNION
      5        SELECT'HOLLYWOOD'  FROM DUAL UNION
      6        SELECT 'WASHINGTON'  FROM DUAL
      7       )
      8  --
      9  select max(decode(rn,1,test_string)) as col_1
    10        ,max(decode(rn,2,test_string)) as col_2
    11        ,max(decode(rn,3,test_string)) as col_3
    12        ,max(decode(rn,4,test_string)) as col_4
    13* from (select test_string, row_number() over (order by test_string) as rn from t)
    SQL> /
    COL_1      COL_2      COL_3      COL_4
    AMERICA    HOLLYWOOD  US         WASHINGTON
    SQL>And columns to rows (or unpivot) is like:
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as
      2       (
      3        SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
      4       )
      5  --
      6  select col_1 as col from t union all
      7  select col_2 from t union all
      8  select col_3 from t union all
      9* select col_4 from t
    SQL> /
    COL
    US
    AMERICA
    HOLLYWOOD
    WASHINGTONor...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as
      2       (
      3        SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
      4       )
      5  --
      6  select decode(rownum,1,col_1,2,col_2,3,col_3,4,col_4) as col
      7* from t, (select * from dual connect by rownum <= 4)
    SQL> /
    COL
    US
    AMERICA
    HOLLYWOOD
    WASHINGTON
    SQL>

  • Navigation on row and column total in pivot report

    Hi All,
    I have created a pivot report with 2 dimension columns and one measure and defined row and column total. Now, users wants to have a navigation report on the row/column total so that once he clicks on total value, he can see the detail report. While doing this, he also wants to pass on the 2 dimension column values to detail report.
    Is there any workaround for this?
    Thanks in advance.

    use a formula column/row. use RANK function in that. (e.g. Rank([A], asc) will sort the rows based on column A values in ascending order)
    you can use this rank in your heading.
    But frankly this is not so easy. You have to do it in a very intelligent way, so that rank gives you column number/row number any how.
    Have a try and let see if you find a appropriate solution.
    Regards,
    Rahul

  • Pivot Table Row and Column extension OBIEE 11g

    Hi,
    I have pivot view report, I have 3 columns,
    1) User Name
    2) Date
    3) Login Count
    User Name will be row(Vertical)
    Date will be Column heading (Horizontal)
    Login Count Measures
    So I will get login Count of user wrt date.
    Now When I give Wide range of filter i.e, from 1st Jan 2010 to 31st Dec 2010.
    There is increase in row also column, when it reaches certain extent it gives error as *'Exceeded configured maximum number of allowed input records.'*
    How to change the limit? or there is any fix?
    Thanks,
    Satheesh

    Hi,
    I have also faced the same issue, i have solved by changing DefaultRowsDisplayed count in instance config file.
    <Views>
    <pivot table>
    <DefaultRowsDisplayed>75</DefaultRowsDisplayed>
    <pivot table>
    </Views>
    Paste that code (which i mentioned in my last post) in instanceconfig.xml before these tags
    </ServerInstance>
    </WebConfig>
    But remember you need to restart the presentation service once this is done....
    Thanks
    Deva

  • Pivoting rows into columns in Oracle 10g

    Hi,
    I want to pivot rows into column in some optimal way.
    I don't want to go with the DECODE option as the number of columns can be more than 200.
    i have also tried the transpose logic which is making the pl/sql block too huge.
    can i directly query the database for the desired output instead of storing the data into some arrays and displaying rows as columns?

    Hi,
    Here's a dynamic way to do this is Oracle 10, using theSQL*Plus @ command to handle the dynamic parts.
    First, let's see how we would do this using a static query:
    WITH     col_cntr    AS
         SELECT     column_name
         FROM     all_tab_columns
         WHERE     owner          = 'FKULASH'
         AND     table_name     = 'TEST_EMP'
         AND     column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ,     unpivoted_data     AS
         SELECT     e.type_val
         ,     c.column_name
         ,     CASE c.column_name
                  WHEN  'X_AMT'  THEN  x_amt     -- *****  Dynamic section 1  *****
                  WHEN  'Y_AMT'  THEN  y_amt     -- *****  Dynamic section 1  *****
                  WHEN  'Z_AMT'  THEN  z_amt     -- *****  Dynamic section 1  *****
              END     AS v
         FROM          test_emp  e
         CROSS JOIN     col_cntr  c
    SELECT       column_name     AS type_val
    ,       SUM (CASE WHEN type_val = 'Q1' THEN v ELSE 0 END)     AS q1     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q2' THEN v ELSE 0 END)     AS q2     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q3' THEN v ELSE 0 END)     AS q3     -- ***** Dynamic section 2  *****
    ,       SUM (CASE WHEN type_val = 'Q4' THEN v ELSE 0 END)     AS q4     -- ***** Dynamic section 2  *****
    FROM       unpivoted_data
    GROUP BY  column_name
    ORDER BY  column_name
    ;Column names are hard-coded in two places:
    (1) in the sub-query unpivoted_data, we had to know that there were 3 columns to be unpivoted, and that they were called x_amt, y_amt and z_amt. You want to derive all of that from all_tab_columns.
    (2) in the main query, we had to know that there would be 4 pivoted columns in the rsult set, and that they would be called q1, q2, q3 and q4. You want to derive all that from the data actually in test_emp.
    Instead of hard-coding those 2 dynamic sections, have Preliminary Queries write them for you, a split second before you run the main query, by running this script:
    --  Before writing sub-scripts, turn off features designed for human readers
    SET     FEEDBACK    OFF
    SET     PAGESIZE    0
    PROMPT *****  Preliminary Query 1  *****
    SPOOL     c:\temp\sub_script_1.sql
    SELECT    '              WHEN  '''
    ||       column_name
    ||       '''  THEN  '
    ||       LOWER (column_name)     AS txt
    FROM       all_tab_columns
    WHERE       owner          = 'FKULASH'
    AND       table_name     = 'TEST_EMP'
    AND       column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ORDER BY  column_name
    SPOOL     OFF
    PROMPT     *****  Preliminary Query 2  *****
    SPOOL     c:\temp\sub_script_2.sql
    SELECT DISTINCT  ',       SUM (CASE WHEN type_val = '''
    ||                type_val
    ||           ''' THEN v ELSE 0 END)     AS '
    ||           LOWER (type_val)          AS txt
    FROM           test_emp
    ORDER BY      txt
    SPOOL     OFF
    --  After writing sub-scripts, turn on features designed for human readers
    SET     FEEDBACK    5
    SET     PAGESIZE    50
    -- Main Query:
    WITH     col_cntr    AS
         SELECT     column_name
         FROM     all_tab_columns
         WHERE     owner          = 'FKULASH'
         AND     table_name     = 'TEST_EMP'
         AND     column_name     NOT IN ('EMP_ID', 'TYPE_VAL')
    ,     unpivoted_data     AS
         SELECT     e.type_val
         ,     c.column_name
         ,     CASE c.column_name
                  @c:\temp\sub_script_1
              END     AS v
         FROM          test_emp  e
         CROSS JOIN     col_cntr  c
    SELECT       column_name     AS type_val
    @c:\temp\sub_script_2
    FROM       unpivoted_data
    GROUP BY  column_name
    ORDER BY  column_name
    ;As you can see, the main query looks exactly like the static query, except that the two dynamic sections have been replaced by sub-scripts. These 2 sub-scripts are written by 2 prelimiary queries, right before the main query.
    As others have said, the fact that you're asking this question hints at a poor table design. Perhaps the table should be permanently stored in a form pretty much like unpivoted_data, above. When you need to display it with columns x_amt, y_amt, ..., then pivot it, using GROUP BY type_col. When you need to display it with columns q1, q2, ..., then pivot it using GROUP BY column_name.

  • Need help with Pivoting rows to columns

    Hi,
    I need help with pivoting rows to columns. I know there are other posts regarding this, but my requirement is more complex and harder. So, please give me a solution for this.
    There are two tables say Table 1 and Table 2.
    Table1
    name address email identifier
    x e g 1
    f s d 2
    h e n 3
    k l b 4
    Table2
    identifier TRno zno bzid
    1 T11 z11 b11
    1 T12 z12 b12
    1 T13 z13 b13
    2 T21 z21 b21
    2 T22 z22 b22
    As you can see the identifier is the column that we use to map the two tables. The output should be like below
    output
    name address email identifier TRno1 zno1 bzid1 TRno2 zno2 bzid2 TRno3 zno3 bzid3
    x e g 1 T11 z11 b11 T12 z12 b12 T13 z13 b13
    f s d 2 T21 z21 b21 t22 z22 b22
    Also we do not know exactly how many TRno's, zno's, etc each value in the identifier will have. There may be only 1 TRNO, zno and bzid, or there may be four.
    All the values must be in separate columns, and not be just comma delimitted. There are also other conditions that i have to add to restrict the data.
    So, can you please tell me what is should use to get the data in the required format? We are using Oracle 10g. Please let me know if u need any more information

    Something like this ?
    SCOTT@orcl> ed
    Wrote file afiedt.buf
      1  select a.name,
      2  a.address,
      3  a.email,
      4  b.* from (
      5  select distinct identifier
      6  ,max(trno1) trno1
      7  ,max(zno1) zno1
      8  ,max(bzid1) bzid1
      9  ,max(trno2) trno2
    10  ,max(zno2) zno2
    11  ,max(bzid2) bzid2
    12  ,max(trno3) trno3
    13  ,max(zno3) zno3
    14  ,max(bzid3) bzid3
    15  ,max(trno4) trno4
    16  ,max(zno4) zno4
    17  ,max(bzid4) bzid4
    18  from (select identifier
    19  ,decode(rn,1,trno,null) trno1
    20  ,decode(rn,1,zno,null) zno1
    21  ,decode(rn,1,bzid,null) bzid1
    22  ,decode(rn,2,trno,null) trno2
    23  ,decode(rn,2,zno,null) zno2
    24  ,decode(rn,2,bzid,null) bzid2
    25  ,decode(rn,3,trno,null) trno3
    26  ,decode(rn,3,zno,null) zno3
    27  ,decode(rn,3,bzid,null) bzid3
    28  ,decode(rn,4,trno,null) trno4
    29  ,decode(rn,4,zno,null) zno4
    30  ,decode(rn,4,bzid,null) bzid4
    31  from (select identifier,
    32  trno,bzid,zno,
    33  dense_rank() over(partition by identifier order by trno,rownum) rn
    34  from table2)
    35  order by identifier)
    36  group by identifier) b,table1 a
    37* where a.identifier=b.identifier
    SCOTT@orcl> /
    NAME       ADDRESS    EMAIL      IDENTIFIER TRNO1      ZNO1       BZID1      TRNO2      ZNO2       BZID2      TRNO3      ZNO3       BZID3      TRNO4      ZNO4       BZID4
    x          e          g          1          T11        z11        b11        T12        z12        b12        T13        z13        b13
    f          s          d          2          T21        z21        b21        T22        z22        b22
    SCOTT@orcl> select * from table1;
    NAME       ADDRESS    EMAIL      IDENTIFIER
    x          e          g          1
    f          s          d          2
    h          e          n          3
    k          l          b          4
    SCOTT@orcl> select * from table2;
    IDENTIFIER TRNO       ZNO        BZID
    1          T11        z11        b11
    1          T12        z12        b12
    1          T13        z13        b13
    2          T21        z21        b21
    2          T22        z22        b22
    SCOTT@orcl>Regards
    Girish Sharma

  • How to display row to columns without using pivot keyword

    hi,
    could someone help me how to dispaly rows into columns without using pivot keyword and actuall my scenario is,iam having two tables with names and sample data is shown below
    ID PROJECT MID MINAME TASKID TASKNAME
    1     PROJ1     1     AA     100     PR1_TASK1
    1     PROJ1     3     CC     102     PR1_TASK3
    1     PROJ1     4     DD     103     PR1_TASK4
    1     PROJ1     5     EE     104     PR1_TASK5
    1     PROJ1     6     FF     105     PR1_TASK6
    2     PROJ2     5     EE     114     PR2_TASK1
    2     PROJ2     6     FF     115     PR2_TASK2
    2     PROJ2     7     GG     116     PR2_TASK3
    2     PROJ2     8     HH     117     PR2_TASK4
    2     PROJ2     9     JJ     118     PR2_TASK5
    2     PROJ2     10     KK     119     PR2_TASK6
    2     PROJ2     1     AA     120     PR2_TASK7
    The output should display project and count of tasks in particular milestone as shown below
    project AA BB CC DD EE FF GG HH JJ KK
    1 2 0 1 5 3 2 0 2 1 0
    2 1 2 0 2 1 0 2 4 3 1
    Thanks in advance ,
    vvr

    WITH t1 AS
    (SELECT 1 ID,
             'PROJ1' PROJECT,
             1 MID,
             'AA' MINAME,
             100 TASKID,
             'PR1_TASK1' TASKNAME
        FROM DUAL
      UNION
      SELECT 1, 'PROJ1', 3, 'CC', 102, 'PR1_TASK3'
        FROM DUAL
      UNION
      SELECT 1, 'PROJ1', 4, 'DD', 103, 'PR1_TASK4'
        FROM DUAL
      UNION
      SELECT 1, 'PROJ1', 5, 'EE', 104, 'PR1_TASK5'
        FROM DUAL
      UNION
      SELECT 1, 'PROJ1', 6, 'FF', 105, 'PR1_TASK6'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 5, 'EE', 114, 'PR2_TASK1'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 6, 'FF', 115, 'PR2_TASK2'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 7, 'GG', 116, 'PR2_TASK3'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 8, 'HH', 117, 'PR2_TASK4'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 9, 'JJ', 118, 'PR1_TASK5'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 10, 'KK', 119, 'PR1_TASK6'
        FROM DUAL
      UNION
      SELECT 2, 'PROJ2', 1, 'AA', 120, 'PR1_TASK7' FROM DUAL)
    SELECT id project,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'AA'
                  AND id = t_out.id),
               0) AA,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'BB'
                  AND id = t_out.id),
               0) BB,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'CC'
                  AND id = t_out.id),
               0) CC,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'DD'
                  AND id = t_out.id),
               0) DD,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'EE'
                  AND id = t_out.id),
               0) EE,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'FF'
                  AND id = t_out.id),
               0) FF,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'GG'
                  AND id = t_out.id),
               0) GG,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'HH'
                  AND id = t_out.id),
               0) HH,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'JJ'
                  AND id = t_out.id),
               0) JJ,
           NVL((SELECT mid
                 FROM t1
                WHERE miname = 'KK'
                  AND id = t_out.id),
               0) KK
      FROM (SELECT DISTINCT id FROM t1) t_out
    PROJECT     AA     BB     CC     DD     EE     FF     GG     HH     JJ     KK
    1     1     0     3     4     5     6     0     0     0     0
    2     1     0     0     0     5     6     7     8     9     10As I understand, you want MID of MINAMEs displayed ? But output is not like yours.. What is exactly your requirements?

  • Need help in displaying Rows to Columns

    Hi,
    I am facing problem in displaying Rows to Columns
    I am using pivot function:
    select *
    from
    (select vendor_name
    from tablea)
    pivot
    (count(vendor_name)
    for vendor_name in ('a,b,'c'));
    its working fine showing vendor_name and count
    but when i want to display the output as:(How to include the Salalry column in the query?)
    Name:{a b c}
    Sal Total:(400,600,800}
    Any help will be needful for me

    Not sure what you mean:
    select  *
      from  (select deptno,sal from emp)
      pivot(sum(sal) for deptno in (10,20,30))
            10         20         30
          8750      10875       9400
    SQL> SY.

  • Oracle 10g Rows to column

    Hi
    I have folowing scenario. I am getting the result with some query like below :
    STATE      DAY     AVG
    NY        02/02     5
    NY        02/03     10
    NY        02/04     20
    NY        02/05     15
    IL        02/02     23
    IL        02/03     34
    IL        02/04     29
    IL        02/05     9
    FL        02/02     15
    FL        02/03     8
    FL        02/04     9
    FL        02/05     10
    and so on..Now I want to convert it from rows to column, but still want to keep STATE column group by :
    so the result would be :
    STATE   02/02   02/03   02/04   02/05
    NY        5      10       20    15
    IL        23     34       29    9
    FL        15     8        9     10
    and so on...I tried using pivot function, but it didnt work. is it even possible? or I will have to do it in code (.net c#) side.
    Thanks

      1  WITH t AS (
      2  SELECT 'NY' state,'02/02' mydt,5 myavg FROM dual
      3  UNION all
      4  SELECT 'NY','02/03',10 FROM dual
      5  UNION all
      6  SELECT 'NY','02/04',20 FROM dual
      7  UNION all
      8  SELECT 'NY','02/05',15 FROM dual
      9  UNION all
    10  SELECT 'IL','02/02',23 FROM dual
    11  UNION all
    12  SELECT 'IL','02/03',34 FROM dual
    13  UNION all
    14  SELECT  'IL','02/04',29 FROM dual
    15  UNION all
    16  SELECT  'IL','02/05',9 FROM dual
    17  UNION all
    18  SELECT  'FL','02/02',15 FROM dual
    19  UNION all
    20  SELECT  'FL','02/03',8 FROM dual
    21  UNION all
    22  SELECT  'FL','02/04',9 FROM dual
    23  UNION all
    24  SELECT  'FL','02/05',10 FROM dual
    25  )
    26  SELECT * FROM t
    27  PIVOT
    28  (
    29  SUM(myavg)
    30  FOR mydt IN('02/02','02/03','02/04','02/05')
    31  )
    32* ORDER BY state DESC
    SQL> /
    ST    '02/02'    '02/03'    '02/04'    '02/05'
    NY          5         10         20         15
    IL         23         34         29          9
    FL         15          8          9         10Am I missing some thing here ?

  • Cursor in select query in row to column format

    Hi
    I have the query like below
    SELECT d.department_id,
                 CURSOR(SELECT e.first_name,
                         e.last_name
                  FROM   employees e
                  WHERE  e.department_id = d.department_id
           ) emps
    FROM   depatments dI want the result set in a format of Row To columns like
    10                             20
    <cursor result>   <cursor result>pls give ur suggestions how to achieve this in a efficient way?I tried the method of "max(decode(.." but dont think so its possible with this

    vishnu prakash wrote:
    Hi
    I have the query like below
    SELECT d.department_id,
    CURSOR(SELECT e.first_name,
    e.last_name
    FROM   employees e
    WHERE  e.department_id = d.department_id
    ) emps
    FROM   depatments dI want the result set in a format of Row To columns like
    10                             20
    <cursor result>   <cursor result>pls give ur suggestions how to achieve this in a efficient way?I tried the method of "max(decode(.." but dont think so its possible with thisNumber of column of a select query is static. Must be known at the parsing time itself. But in your case i dont think the number of columns will be limited to 2 (10 and 20) there could be many more.
    You can search this forum to see how to PIVOT your data. There are lot of example. You can also try dynamic pivot. Its all in here, just search.

  • Row to Column in 10g

    Hello All,
    I have a requirement where I have to convert rows to columns and vice versa in 10g.
    Pivot isn't supported in 10g.
    Actual query looks like this
    change_name    primary_key_id
    ASSET              1501 
    COLLATERAL         1501
    ASSET              1502
    COLLATERAL         1510
    ASSET              1503
    COLLATERAL         1515
    Required Output:
    change_table_name  Asset      Collateral
    Primary_key_id         1501       1501
    Primary_key_id2        1502     
    Primary_key_id3                     1510
    Primary_key_id4        1503     
    Primary_key_id5                     1515Thx
    Shank.
    Edited by: SamFisher on May 16, 2012 7:48 PM

    Hi,
    SamFisher wrote:
    Hello All,
    I have a requirement where I have to convert rows to columns and vice versa in 10g.
    Pivot isn't supported in 10g.Pivot can be done in any version of Oracle.
    The PIVOT keyword of the SELECT command isn't supported in Oracle 10. That's only one way of pivoting.
    Actual query looks like this
    change_name    primary_key_id
    ASSET              1501 
    COLLATERAL         1501
    ASSET              1502
    COLLATERAL         1510
    ASSET              1503
    COLLATERAL         1515
    Required Output:
    change_table_name  Asset      Collateral
    Primary_key_id         1501       1501
    Primary_key_id2        1502     
    Primary_key_id3                     1510
    Primary_key_id4        1503     
    Primary_key_id5                     1515
    I think you want something like this:
    SELECT     'Primary_key_id
         || TO_CHAR ( NULLIF  ( ROW_NUMBER () OVER (ORDER BY  NVL ( a.primary_key_id
                                             , c.primary_key_id
                      , 1
               )          AS change_table_name
    ,     a.primary_key_id     AS asset
    ,     c.primary_key_id     AS collateral
    FROM          table_x      a
    FULL OUTER JOIN     table_x  c  ON  a.primary_key_id  = c.primary_key_id
    -- Next 4 lines added after Ankit, below
                       AND     a.change_name     = 'ASSET'
                       AND     c.change_name     = 'COLLATERAL'
    WHERE     a.change_name     = 'ASSET'
    OR     c.change_name     = 'COLLATERAL'
    ;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
    This generates a unique change_table_name for each row. I don't see how you get the values you said you wanted associated with the other columns. That is, I have the same question as Justin:
    Justin Cave wrote:
    ... How do you know that 1502 goes with primary_key_id2 and not primary_key_id3 or 4 or 1?Perhaps you don't really care which change_table_name is given to each row, just so long as they are unique, and numbered with consecutive integers (except for 1). If you really do need exactly what you posted, explain how you get it. You probably just need to change the analytic ORDER BY clause.
    Edited by: Frank Kulash on May 17, 2012 6:56 AM
    Corrected query

  • Mimic "show data as percent of row parent" in non-pivot table

    Is it possible to implement or mimic the 'percentage of parent row' functionality, foreseen for pivot tables in obiee 11g, for a 'normal' table?
    We use a rather fixed table, in which no drilling is allowed - conditions are applied by means of prompts and filters. The report is based upon the store dimension, which has as hierarchy: country > cluster > store. We have added a column to represent the 'share' of a store's net turnover in the cluster total. The results looks like:
    country  cluster  store  turnover   share
    =======  =======  =====  ========   =====
    belgium  1        a        20.000   66.7%
                      b        10.000   33.3%
             cluster total:    30.000  100.0%
             2        c        40.000   57.1%
                      d        30.000   42.9%
             cluster total:    70.000  100.0%
    country total:            100.000  100.0%
    holland  3        e        50.000   71.4%
             4        f        20.000   28.6%
             cluster total:    70.000  100.0%
    country total:             70.000  100.0%
    grand total:              170.000  100.0%Instead of all the total levels showing as 100% however, we would like to see each sub-total represented as a percentage of the level above, much like the forementioned pivot table functionality. For the example above that would mean it should look like:
    country  cluster  store  turnover   share
    =======  =======  =====  ========   =====
    belgium  1        a        20.000   66.7%
                      b        10.000   33.3%
             cluster total:    30.000   30.0%
             2        c        40.000   57.1%
                      d        30.000   42.9%
             cluster total:    70.000   70.0%
    country total:            100.000   58.8%
    holland  3        e        50.000   71.4%
             4        f        20.000   28.6%
             cluster total:    70.000  100.0%
    country total:             70.000   41.2%
    grand total:              170.000  100.0%Any advice or idea would be greatly appreciated...

    902360 wrote:
    Hello,
    I'd like to know if there's a way to configure the Pivot Table "Show Data As - Percent of" precision.
    ATM, I have some reports using SR types where one of those are much bigger than the others.. So when the report its generated, it returns something like:
    TYPE A: 99%
    TYPE B: 0%
    TYPE C: 0%
    TYPE D: 0%
    How can I show the other types correct value? Something like number of decimal places...
    Ps: As criteria to this report, I have changed my Data Format to "Number", "Minus: -123" with 15 Decimal Places.
    Thanks,
    RamonYup. Can do. Read this link:
    http://shivabizint.wordpress.com/2008/09/14/changing-the-precession-of-percent-of-columns-in-pivot-table-of-obiee/

Maybe you are looking for