Pivot Static Columns for Group by Rows

I have some commonly alike problem but its different from dynamic pivot unpivot
Input Table
ID
Value
Guid1
Val1
Guid1
Val2
Guid1
Val3
Guid1
val4
Guid2
asd
Guid2
asd
Guid2
asd
Guid3
test
Guid4
Lee
Guid4
Jack
Guid4
Arnol
Guid4
wan
Guid4
Swin
Output Table
ID
Col1
Col2
Col3
Col4
Col5
Guid1
Val1
Val2
Val3
val4
NULL
Guid2
asd
asd
asd
NULL
NULL
Guid3
test
NULL
NULL
NULL
NULL
Guid4
Lee
Jack
Arnol
wan
Swin
I have to group by my input rows based on ID & rotate them to fixed 5 column table if values are not there I have to fill with NULL.
Kindly let me know if you need more clarification on the problem
I'm looking for solution in Either SSIS or T-SQL.

just this is enough
SELECT ID,
[1] AS Col1,
[2] AS Col2,
[3] AS Col3,
[4] AS Col4,
[5] AS Col5
FROM
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq,*
FROM table
)t
PIVOT(MAX([Value]) FOR Seq IN ([1],[2],[3],[4],[5]))p
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • How to aggregate a column based date column (for weekly single row)?

    How to aggregate a column based date column (for weekly single row)?

    Hi,
    Consider the below statement for daily bases which is ok
    SELECT ID, DATE, SUM(AMOUNT) FROM TABLE_NAME GROUP BY ID, DATE ORDER BY ID, DATE
    The same like the above statement, I want output on weekly, fortnightly, and monthly bases, How to do this? Need your help...

  • How to disable a Column for a particular row in Advanced Tables

    Hi All,
    We are using an Advanced Table for search criteria. Our requirement demands to disable a column (we are using messagechoice item in that column) for only first row. We use standard Add Another Row functionality to add the rows.For the remaining rows it (message choice) should be enabled. Can anyone help me to solve.
    Regards,
    Sundeep

    You can either use a Switcher or OADataBoundVariables for this.
    Please see the section OADataBoundVariable in the dev guide. Based on certain View Attribute value, you can set the specific column in the table as Read Only.
    Switcher can be very similar too. Decide an Attribute for the Switcher and make sure the first row (which you want to disable) has some specific value. All other rows should have some other value. Decode can help you here. Then you would need to create two cases. Under the specific to first row, have a read only item. For the other case, have an enterable item. A little read through of the Switcher chapter in the Dev guide would clarify the doubts if any.
    Regards
    Sumit

  • Trying to get ending effective date for groups of rows

    Hi Everyone,
    I've searched the forums, but I can't find a post that presents a problem quite like this.
    I have some data that looks like this:
            ID_NUM     EFFECTIVE ALLOC_PERCENT   ACCT
           101 01-JUL-11            21   A1
           101 01-JUL-11            72   A2
           101 01-JUL-11             7   A3
           101 01-JUL-12            20   B1
           101 01-JUL-12            80   B2
           101 01-JAN-13            20   A1
           101 01-JAN-13            20   A2
           101 01-JAN-13            50   A3
           101 01-JAN-13            10   B1
           101 01-JUN-13            50   A1
           101 01-JUN-13            50   A2(note - I manually inserted blank lines for clarity)
    Here's the logic: The rows represent a percentage allocation to the account number for the specified id number, for that effective date. A newer effective date supercedes previous ones, and if any row in the conceptual group is superceded, then they all are.
    I'm trying to find out the date when each group's effective period ended, and include that in the result set so that I can subsequently calculate the number of days a given row was effective; something like this;
      ID_NUM     EFFECTIVE END_DATE   ALLOC_PERCENT ACCT
           101 01-JUL-11 01-JUL-12             21 A1
           101 01-JUL-11 01-JUL-12             72 A2
           101 01-JUL-11 01-JUL-12              7 A3
           101 01-JUL-12 01-JAN-13             20 B1
           101 01-JUL-12 01-JAN-13             80 B2
           101 01-JAN-13 01-JUN-13             20 A1
           101 01-JAN-13 01-JUN-13             20 A2
           101 01-JAN-13 01-JUN-13             50 A3
           101 01-JAN-13 01-JUN-13             10 B1
           101 01-JUN-13 <null>                50 A1
           101 01-JUN-13 <null>                50 A2The END_DATE of the group is the EFFECTIVE_DATE of the following group (ordered by ID_NUM, EFFECTIVE_DATE).
    The last two rows' END_DATE is null because there is no group of rows with a later effective date - in my process, I'll NVL that to sysdate so that my days calculations will be valid.
    I tried some analytic queries with LEAD, but I couldn't figure out how to make it get the next group's effective date. I could get the next row's effective date, but not the next group's. I couldn't specify how many lead rows to look ahead, because there is not a consistent number of rows in each group.
    How do I populate the END_Date column?
    Here's the code to create the above.
    create table t
    (id_num number,
    effective_date date,
    alloc_percent number,
    acct_code varchar2(4)
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jul-2011',21.0,'A1');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jul-2011',72.0,'A2');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jul-2011',7.0,'A3');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jul-2012',20.0,'B1');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jul-2012',80.0,'B2');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jan-2013',20.0,'A1');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jan-2013',20.0,'A2');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jan-2013',50.0,'A3');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jan-2013',10.0,'B1');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jun-2013',50.0,'A1');
    insert into t (id_num,Effective_date,alloc_percent,acct_code) values(101,'01-jun-2013',50.0,'A2');
    commit;
    select * from t;Oracle version information:
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    "CORE     11.2.0.3.0     Production"
    TNS for Solaris: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    Thank you very much

    select  id_num,
            effective_date,
            first_value(effective_date)
              over(
                   partition by id_num
                   order by effective_date
                   range between 1 following and unbounded following
                  ) end_date,
            alloc_percent,
            acct_code
      from  t
      order by id_num,
               effective_date
        ID_NUM EFFECTIVE END_DATE  ALLOC_PERCENT ACCT
           101 01-JUL-11 01-JUL-12            21 A1
           101 01-JUL-11 01-JUL-12            72 A2
           101 01-JUL-11 01-JUL-12             7 A3
           101 01-JUL-12 01-JAN-13            20 B1
           101 01-JUL-12 01-JAN-13            80 B2
           101 01-JAN-13 01-JUN-13            20 A1
           101 01-JAN-13 01-JUN-13            10 B1
           101 01-JAN-13 01-JUN-13            20 A2
           101 01-JAN-13 01-JUN-13            50 A3
           101 01-JUN-13                      50 A1
           101 01-JUN-13                      50 A2
    11 rows selected.
    SQL> SY.

  • ADF Mobile updating a column for all the rows

    Hi Every one,
    This is My ADFMobile Usecase:
    In My use case, I created DB Webservices  and created a  datContol on it .
    I have few AMX pages in my Taskflow.
    1st page: Authentication page with userid and p/w.
    2nd page:Need to get DeptID as  LOV and a submit button.
    3rd Page:Need to get all Employees in that Department. Need to get Empl_name, Empl_Id, Empl_salary and Empl_ Attendence. and a SYNC Button.
    Up to now every thing is fine.
    But I need to change  Empl_Attendence Column As absent or Present . I used  SelectOneChoice. Default there will be PRESENT value in back end.So after Selecting  SlectOneChoice the value (Absent) should Automatically update in DB for that row.
    Requirement:
    So How to write logic for the SYNC button to update the Empl_ Attendence for all the rows at a time.
    Thanks In Advance
    Regards
    Varma.

    Hi Every one,
    This is My ADFMobile Usecase:
    In My use case, I created DB Webservices  and created a  datContol on it .
    I have few AMX pages in my Taskflow.
    1st page: Authentication page with userid and p/w.
    2nd page:Need to get DeptID as  LOV and a submit button.
    3rd Page:Need to get all Employees in that Department. Need to get Empl_name, Empl_Id, Empl_salary and Empl_ Attendence. and a SYNC Button.
    Up to now every thing is fine.
    But I need to change  Empl_Attendence Column As absent or Present . I used  SelectOneChoice. Default there will be PRESENT value in back end.So after Selecting  SlectOneChoice the value (Absent) should Automatically update in DB for that row.
    Requirement:
    So How to write logic for the SYNC button to update the Empl_ Attendence for all the rows at a time.
    Thanks In Advance
    Regards
    Varma.

  • Help with Formula/Placeholder columns for group totals

    I have select that is broken into 2 groups (a COMPANY group and a DETAIL group within company). I need to determine the COMPANY group totals based on values in a column in the DETAIL group. I.E. the column is a transaction description and can have up to 15 different values and it did not appear that a summary column would work for what I need. I have currently set up 15 formula columns in the DETAIL group and check the value of the REASON to accumulate totals and return the total to a PLACEHOLDER column (which is numeric and defined in the COMPANY group).
    I then report the Placeholder columns at break of COMPANY. This all works fine if I run only one company. If I run with multiple companies the Placeholder columns are used as a running total.
    My question is how do I reset the PLACEHOLDER columns at company break if at all, or is there a simpler way to accomplish what I need.
    Any and all assistance would be greatly appreciated.
    Tom Vereecke

    If you are using one placeholder column in different formula columns just ignore the following solution. It will not work in that case. I will post it here if i find out any way to initialize placeholder columns at different groups or anyother way to solve this issue.
    I think if you remove placeholder columns and create summary columns at Company Level, that will work. And formula columns will be changed as follows:
    Formula Column:
    active_total number(10) := 0;
    begin
    if :status_1 = 'ACTIVE' then
    active_total := active_total + 1;
    end if;
    if :status_2 = 'ACTIVE' then
    active_total := active_total + 1;
    end if;
    if :status_3 = 'ACTIVE' then
    active_total := active_total + 1;
    end if;
    return active_total;
    end;
    And in summary columns source will be formula columns. And reset the summary columns at Company.
    Hope this helps.
    Message was edited by:
    fs

  • Add a new line for grouping similar rows

    As subject, possible to do this using SQL?
    For eg:
    KEY       Description
    1           John
    1           John Smith
    2           Peter
    2           Peter Pan
    to:
    KEY      Description
    1          John
    1          John Smith
    <newline>
    2          Peter
    2          Peter Pan

    With the model clause:
    SQL> with t as (
      2    select 1 KEY, 'John' Description from dual union
      3    select 1 , 'John Smith' from dual union
      4    select 3 , 'Agent' Description from dual union
      5    select 3 , 'Agent Smith' from dual union
      6    select 2 , 'Peter' from dual union
      7    select 2 , 'Peter Pan' from dual)
      8  -- end of sample data
      9  SELECT decode(m, 0, to_char(k), 1, '<newline>') as key, d as description
    10    FROM t
    11   MODEL PARTITION BY (key as k)
    12         DIMENSION BY (description as d)
    13         MEASURES (0 as m)
    14   RULES UPSERT
    15   (m[null] = 1)
    16  /
    KEY                                      DESCRIPTION
    1                                        John
    1                                        John Smith
    <newline>                               
    3                                        Agent
    3                                        Agent Smith
    <newline>                               
    2                                        Peter
    2                                        Peter Pan
    <newline>                               
    9 rows selected
    SQL> If you don't really care about the <newline> string and the key itself will do you can skip the decode and put an explicit ORDER BY as in:
    SELECT k as key, d as description
      FROM t
    MODEL PARTITION BY (key as k)
           DIMENSION BY (description as d)
           MEASURES (0 as m)
    RULES UPSERT
    (m[null] = 1)
    ORDER BY k, dI'm assuming your column description won't have nulls for your key values.

  • Grouping consecutive rows with same value?

    Hi
    I have a table in which three columns are:
    ROWID  SHOPNAME
    1      SHOP_C     
    2      SHOP_A     
    3      SHOP_C     
    4      SHOP_C     
    5      SHOP_A     
    6      SHOP_A     
    7      SHOP_C     
    8      SHOP_B     
    9      SHOP_B     
    10     SHOP_E    
    11     SHOP_A     
    12     SHOP_D     
    13     SHOP_D     
    14     SHOP_F     
    15     SHOP_G     
    16     SHOP_F     
    17     SHOP_C     
    18     SHOP_C     
    19     SHOP_C     
    20     SHOP_C      Rowid is a primary key column with unique ids
    Shopname is varchar2 column
    I want to make groupings of every "shopname" value "order by ROWID" such that when there are consecutive rows of same shopname the group remains same and as soon as there is different value in shopname column for the next row the "group" changes.
    below is my desired output is with 3rd column of groupings as follows:
    ROWID  SHOPNAME    Groups
    1      SHOP_C      1
    2      SHOP_A      2
    3      SHOP_C      3 ------> same grouping because Shop name is same
    4      SHOP_C      3 ------> same grouping because shop name is same
    5      SHOP_A      4 ----> different shopname so group changed.
    6      SHOP_A      4 ----> same group as above because shopname is same
    7      SHOP_C      5
    8      SHOP_B      6
    9      SHOP_B      6
    10     SHOP_E      7
    11     SHOP_A      8
    12     SHOP_D      9
    13     SHOP_D      9
    14     SHOP_F      10
    15     SHOP_G      11
    16     SHOP_F      12
    17     SHOP_C      13
    18     SHOP_C      13
    19     SHOP_C      13
    20     SHOP_C      13I want that to be done via analytics, so that I can partition by clause for different shops over different cities
    regards
    Ramis.

    with data(row_id, shop) as
    select 1,      'SHOP_C' from dual union all
    select 2,      'SHOP_A' from dual union all     
    select 3,      'SHOP_C' from dual union all
    select 4,      'SHOP_C' from dual union all    
    select 5,      'SHOP_A' from dual union all     
    select 6,      'SHOP_A' from dual union all
    select 7,      'SHOP_C' from dual union all
    select 8,      'SHOP_B' from dual union all
    select 9,      'SHOP_B' from dual union all
    select 10,     'SHOP_E' from dual union all
    select 11,     'SHOP_A' from dual union all
    select 12,     'SHOP_D' from dual union all
    select 13,     'SHOP_D' from dual union all
    select 14,     'SHOP_F' from dual union all
    select 15,     'SHOP_G' from dual union all
    select 16,     'SHOP_F' from dual union all
    select 17,     'SHOP_C' from dual union all
    select 18,     'SHOP_C' from dual union all
    select 19,     'SHOP_C' from dual union all
    select 20,     'SHOP_C' from dual
    get_data as
      select row_id, shop,
             lag(shop) over (order by row_id) prev_shop
        from data
    select row_id, shop,
           sum(case when shop = prev_shop
                      then  0
                      else 1
                      end
                ) over (order by row_id) grps
      from get_data;
    ROW_ID SHOP   GRPS
         1 SHOP_C    1
         2 SHOP_A    2
         3 SHOP_C    3
         4 SHOP_C    3
         5 SHOP_A    4
         6 SHOP_A    4
         7 SHOP_C    5
         8 SHOP_B    6
         9 SHOP_B    6
        10 SHOP_E    7
        11 SHOP_A    8
        12 SHOP_D    9
        13 SHOP_D    9
        14 SHOP_F   10
        15 SHOP_G   11
        16 SHOP_F   12
        17 SHOP_C   13
        18 SHOP_C   13
        19 SHOP_C   13
        20 SHOP_C   13
    20 rows selected

  • How to store multiple values in a coloum for a particaular row?

    By mistake i posted this in abap general forum but i think this question should belong to Data Dictionary section
    I want to design a table where we can store a table where we can save multiple values of  column for a particular row. how to achieve this ? thanks in advance.
    Example
    id      name  val1    val2
    001  abc        1          a
                           2          b
                           3          d
                           4          e

    you can use deep structures to achieve this.
    or may be a simpilar approach based on the example that you have mentioned will be to create a table with all the primary keys that you want with an additional key field which will behave as a counter. some standard table do you this technique eg: PLMK where zaehl is a counter field which part of the kley fields.
    cheers, Prabhakaran

  • How do i group the columns of a table for a given row?

    Hi,
    I have a situation where i need to display a table that has a column for every day of  a given period of time. But the first two rows of the table should be grouped in such a way that each cell of the first row covers the seven columns of the rows below. how do i accomplish this in web dynpro abap table controls?

    Hi,
    Dynamic TABLE UI element generation and internal table 
    Re: Dynamic Tables and UI
    Re: Dynamic Programing
    Regards,
    Lekha.

  • Using same tablix column for 2 row groups

    Hi.  We run 2012 enterprise.  My tablix is grouped (for the most part) by hidden primary (values are 5,10,15,20) , secondary and tertiary sort columns.  And a couple of visible label items that are aligned left.
    Every row's data columns are completely %, $ or numerically formatted depending on a hidden helper field i've snuck into the tablix row group data fields after the tertiary sort.
    The primary sort column does equate to a label that I dont show right now.  In biz terms that label is actually the highest from a grouping standpoint but isnt being used right now for anything.  I left it for last.
    As I put the finishing touches on this report, I'd like to display that label ahead of the 4 sections it represents using the same column being occupied by the 1st (leftmost) label that is currently visible.  I'd like to center it in that column on
    its own row and apply some shading to the cells to its right.   And show nothing in those shadded cells.   All without changing my dataset if possible.
    To complicate things, I'd like to follow each of the 4 sections with a completely shaded row.  No data at all but grid lines can show thru shading.   Again without changing my dataset.  Is there a feature in ssrs that deals with this
    kind of visual nuance?

    Hi Jeewan,
    Try this formula, If I understood your question correctly it will work for you.

  • Mild challenge -pivoting *multiple* columns per row using only SQL

    Hello All,
    I'm in the process of learning the various pivoting techniques available
    in SQL, and I am becoming more familiar with the decode,function,group-by
    technique seen in many examples on these forums. However, I've got a case
    where I need to pivot out 3 different columns for 3 rows of data where the
    value of a different column is driving whether or not those columns are pivoted.
    I know that last sentence was as clear as mud so I'll show you/provide the simple
    scripts and data, and then I'll elaborate a little more beneath.
    create table temp_timeline (
    mkt_id varchar2(10),
    event_id number(8),
    event_type varchar2(3),
    mod_due_date date,
    cur_due_date date,
    act_due_date date
    insert into temp_timeline values('DSIM6',51,'S1','NOV-13-06','NOV-13-06',NULL);
    insert into temp_timeline values('DSIM6',51,'S2','DEC-20-06','DEC-20-06',NULL);
    insert into temp_timeline values('DSIM6',51,'S3','JAN-17-07','JAN-17-07',NULL);
    insert into temp_timeline values('DSIM6',51,'S4','FEB-14-07','FEB-14-07',NULL);
    commit;
    select * from temp_timeline;
    The "normal" output (formatted with period-separated fields) is:
    DSIM6.51.S1.NOV-13-06.NOV-13-06.NULL
    DSIM6.51.S2.DEC-20-06.DEC-20-06.NULL
    DSIM6.51.S3.JAN-17-07.JAN-17-07.NULL
    DSIM6.51.S4.FEB-14-07.FEB-14-07.NULL
    The DESIRED 1-row output (formatted with period-separated fields) is:
    DSIM6.51.NOV-13-06.NOV-13-06.NULL.DEC-20-06.DEC-20-06.NULL.JAN-17-07.JAN-17-07.NULL.FEB-14-07.FEB-14-07.NULL
    So, the first 2 columns in the table have the same data, and the third column
    makes the row unique (they could all have the same/similar dates).
    If this table only consisted of the first 3 columns then many of the examples seen
    on this forum would work well (grouping by the first 2 rows and pivoting out
    the "event_type" columns containing (S1,S2,S3,S4) etc.
    But, in my case, I need to discard the event_type column and pivot out the
    3 columns of date data onto the first row (for each different event_type).
    So the 3 Dates associated with the "S2" column would go to the first row, and the
    3 dates associated with the "S3" column would also go to the first row (and so on).
    The 3 dates need to be 3 distinct columns when they are
    pivoted out (not concatenated to each other and pivoted as one column).
    Given this, I will need to pivot out a total of 12 different columns for each distinct
    (mkt_id, event_id) pair.
    For the time being I have accomplished this with a union, but am trying to expand
    my abilities with other sql methods. I've seen some very elegant solutions on this
    forum so will be interested to see what others can come up with for this solution.
    Thanks in advance for any comments you may provide.

    Just DECODE based on the event type, which will generate your 12 columns.
    SELECT mkt_id, event_id,
           MAX(DECODE(event_type, 'S1', mod_due_date, NULL)) s1_mod_due,
           MAX(DECODE(event_type, 'S1', cur_due_date, NULL)) s1_cur_due,
           MAX(DECODE(event_type, 'S1', act_due_date, NULL)) s1_act_due,
           MAX(DECODE(event_type, 'S2', mod_due_date, NULL)) s2_mod_due,
           MAX(DECODE(event_type, 'S2', cur_due_date, NULL)) s2_cur_due,
           MAX(DECODE(event_type, 'S2', act_due_date, NULL)) s2_act_due,
           MAX(DECODE(event_type, 'S3', mod_due_date, NULL)) s3_mod_due,
           MAX(DECODE(event_type, 'S3', cur_due_date, NULL)) s3_cur_due,
           MAX(DECODE(event_type, 'S3', act_due_date, NULL)) s3_act_due,
           MAX(DECODE(event_type, 'S4', mod_due_date, NULL)) s4_mod_due,
           MAX(DECODE(event_type, 'S4', cur_due_date, NULL)) s4_cur_due,
           MAX(DECODE(event_type, 'S4', act_due_date, NULL)) s4_act_due
    FROM temp_timeline
    GROUP BY mkt_id, event_idTested, because you supplied create table and insert statements, thank you.
    John

  • Transposing group from rows to column

    Hi Team,
    I am having groups of rows of a fixed kind that need to be transposed to column. eg
    Attrib 
    Value
    Attribute1
    a
    Attribute2
    b
    Attribute3
    c
    Attribute4
    d
    Attribute1
    e
    Attribute2
    f
    Attribute3
    g
    Attribute4
    h
    Attribute2
    i
    Attribute3
    j
    Attribute4
    k
    This need to be transposed as ;
    Attribute1
    Attribute2
    Attribute3
    Attribute4
    a
    b
    c
    d
    e
    f
    g
    h
    NULL
    i
    j
    k
    Any help would be great...
    I tried using  PIVOT like
    pivot (min (AttributeValue) for rn in ([1], [2], [3], [4],[5], [6])) pvt  
    where i was having rank in rn... but the places where Attribute1 is not present, that also nneed to become NULL. This is something I am not able to do.

    Hi 
    If
    I may, there is very small fix to
    Rishabh's query in order to make it work.
    * as it is it will work only if the order of the insert fit the result, and not in any case! the SQL Server might bring the data in different order if there is no use of ORDER BY
    Option 1: 
    ;with cte as
    SELECT * , ROW_NUMBER () OVER (partition by Attrib ORDER BY (SELECT NULL)) as rnum
    FROM T
    --select * from cte
    select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
    FROM
    SELECT Attrib,Value , rnum
    FROM cte
    )p
    PIVOT
    (MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
    Option 2: Another option for solution might be
    ;with MyCTE as (
    select Attrib, Value, ROW_NUMBER() OVER (partition by Attrib order by (SELECT NULL)) RN
    from T
    select
    MAX(CASE when Attrib = 'Attribute1' then Value END) as Attribute1
    ,MAX(CASE when Attrib = 'Attribute2' then Value END) as Attribute2
    ,MAX(CASE when Attrib = 'Attribute3' then Value END) as Attribute3
    ,MAX(CASE when Attrib = 'Attribute4' then Value END) as Attribute4
    --, RN
    from MyCTE
    group by RN
    And here is all together (check your solution which is the first, it will not bring the correct result since I changed the order of inserted)
    create table T (Attrib NVARCHAR(100), Value NVARCHAR(10))
    insert T values
    ('Attribute1', 'a'),
    ('Attribute2', 'b'),
    ('Attribute3', 'j'),
    ('Attribute3', 'c'),
    ('Attribute4', 'd'),
    ('Attribute1', 'e'),
    ('Attribute2', 'f'),
    ('Attribute3', 'g'),
    ('Attribute4', 'h'),
    ('Attribute2', 'i'),
    ('Attribute4', 'k')
    GO
    ;with cte as
    SELECT * , (ROW_NUMBER () OVER (ORDER BY (SELECT NULL)) -1 )/ 4 as rnum
    FROM T
    --select * from cte
    select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
    FROM
    SELECT Attrib,Value , rnum
    FROM cte
    )p
    PIVOT
    (MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
    GO -- Not OK
    ;with MyCTE as (
    select Attrib, Value, ROW_NUMBER() OVER (partition by Attrib order by (SELECT NULL)) RN
    from T
    select
    MAX(CASE when Attrib = 'Attribute1' then Value END) as Attribute1
    ,MAX(CASE when Attrib = 'Attribute2' then Value END) as Attribute2
    ,MAX(CASE when Attrib = 'Attribute3' then Value END) as Attribute3
    ,MAX(CASE when Attrib = 'Attribute4' then Value END) as Attribute4
    --, RN
    from MyCTE
    group by RN
    GO -- OK
    ;with cte as
    SELECT * , ROW_NUMBER () OVER (partition by Attrib ORDER BY (SELECT NULL)) as rnum
    FROM T
    --select * from cte
    select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
    FROM
    SELECT Attrib,Value , rnum
    FROM cte
    )p
    PIVOT
    (MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
    GO --OK
    drop table T
      Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]

  • Radio group in classic report based on another column on the same row.

    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    Application Express 4.1.0.00.32
    How can I have a radio group column based on an LOV utilizing another column on the same row of the report?
    For example: what if I had a survey application and depending on the likert scale that was assigned to the question there would be different possible answer choices:
    Question 1 on row 1 of the report: The class instructor was friendly?
    Likert scale choice is Agreement.
    Choices on Radio Group: Strongly Agree, Agree, Undecided, Strongly Disagree
    Question 2 on row 2 of the report: The class offered good materials?
    Likert scale choice is Quality.
    Choices on Radio Group: Excellent, Below Average, Average, Above Average, Excellent
    The radio group can change per row depending on the Likert scale assigned to the question which is assigned to a different column on the row.
    Can LOV utilize the column? :
    SELECT scale_text
    FROM scale_choices
    WHERE scale_category_choice_id = 2 <<= this would be the Likert scale identifier
    ORDER
    BY display_order

    Here is the answer:
    APEX_ITEM.SELECT_LIST_FROM_QUERY(
    p_idx IN NUMBER,
    p_value IN VARCHAR2 DEFAULT NULL,
    p_query IN VARCHAR2,
    p_attributes IN VARCHAR2 DEFAULT NULL,
    p_show_null IN VARCHAR2 DEFAULT 'YES',
    p_null_value IN VARCHAR2 DEFAULT '%NULL%',
    p_null_text IN VARCHAR2 DEFAULT '%',
    p_item_id IN VARCHAR2 DEFAULT NULL,
    p_item_label IN VARCHAR2 DEFAULT NULL,
    p_show_extra IN VARCHAR2 DEFAULT 'YES')
    RETURN VARCHAR2;

  • SQL Tuning for 50 million rows--- Group by

    For example i have 50 million+ rows in a table with columns Customer_ID, Order_ID and trans_date. I need a query to return customers with more than 2 orders over the past 60 days.
    [sql]
    Select customer_id from table
    where trans_date between sysdate-60 and sysdate
    group by customer_id
    having count(customer_id)>=2
    [/sql]
    I am trying to understand if there is a fastest alternative for group by and having. Especially for large data sets. Thanks in advance for your help/suggestions.

    varun wrote:
    For example i have 50 million+ rows in a table with columns Customer_ID, Order_ID and trans_date. I need a query to return customers with more than 2 orders over the past 60 days.
    [sql]
    Select customer_id from table
    where trans_date between sysdate-60 and sysdate
    group by customer_id
    having count(customer_id)>=2
    [/sql]
    I am trying to understand if there is a fastest alternative for group by and having. Especially for large data sets. Thanks in advance for your help/suggestions.
    The first task is to write a query that actually works properly; that one doesn't. Your business rule was 'more than 2 orders' but that query uses '>=2'.
    And there is NOTHING in that query having anything to do with 'orders'.
    That query returns 'customers with two or more rows' in the last 60 days. Why aren't you using 'ORDER_ID' in the query?
    First write a query that returns the correct results. There is no need for an alternative unless you can demonstrate that the query actually HAS a performance problem.

Maybe you are looking for