Pivot in SQL

Hi,
How to display the following data in row based result,
Actual Table Result:
Cust_Name Prod1 Prod2 Prod3
A 5 10 11
I want the result like following,
Product Name Value
Prod1 5
Prod2 10
Prod3 11
Please help me to get know about this.
TIA

As per your need!!!
with t
as
select 'A' Cust_Name ,5 Prod1 , 10 Prod2 , 11 Prod3   from dual
union all
select 'B' Cust_Name ,7 Prod1 , 12 Prod2 , 15 Prod3   from dual
select  Cust_Name,cols, vals
from t
UnPivot include nulls (Vals for cols in(Prod1 , Prod2  ,Prod3))
CUST_NAME COLS        VALS
A         PROD1          5
A         PROD2         10
A         PROD3         11
B         PROD1          7
B         PROD2         12
B         PROD3         15
Hi Dhiva, Kindly close this thread if answered.....
This track records doesn't look good Total Questions:      29 (27 unresolved)
Only 2 threads are closedRegards,
Lokanath Giri
Edited by: Lokanath Giri on ३ फ़रवरी, २०१२ २:५७ अपराह्न

Similar Messages

  • Help on Pivot in SQL

    Hi Gurus,
    I have req where i need to do comparison between regular bill vs trial bill. I need to get the trial of the current_month and 2 months previous and last months regular bills. Here is th sample data.
    Account#    Invc #       Start_Date       End_Date    regular_Amoun   t Trial_Amount
    1233456    26470000  01-Jun-2013      01-Jul-2013        $ 19                    $27
    1347589    26470000  01-Aug-2013     current_date       $ 45                   $67
    1115756    26470000  01-May-2013     01-June-2013     $ 39                    $27
    Here is the desired output
    Account#    Invc #   June 2013  July 2013  Aug 2013
    1233456    26470000    $39         $19       $67
    Any input is appreciated

    Maybe
    with
    t as
    (select 1233456 Account,
            26471000 Invc,
            to_date('01-Jun-2013','dd-Mon-yyyy') Start_Date,
            to_date('01-Jul-2013','dd-Mon-yyyy') End_Date,
            19 regular_amount,
            27 Trial_Amount
       from dual union all
    select 1233456,26472001,to_date('01-Jul-2013','dd-Mon-yyyy'),to_date('01-Aug-2013','dd-Mon-yyyy'),45,67
      from dual union all
    select 1233456,26473002,to_date('01-May-2013','dd-Mon-yyyy'),to_date('01-June-2013','dd-Mon-yyyy'),39,27
      from dual union all
    select 1233457,26474003,to_date('01-Apr-2013','dd-Mon-yyyy'),to_date('01-May-2013','dd-Mon-yyyy'),22.9,51
      from dual union all
    select 1233457,26471110,to_date('01-Jun-2013','dd-Mon-yyyy'),to_date('01-Jul-2013','dd-Mon-yyyy'),500,500
      from dual union all
    select 1233457,26472112,to_date('01-Jul-2013','dd-Mon-yyyy'),to_date('01-Aug-2013','dd-Mon-yyyy'),100,100
      from dual union all
    select 1233457,26473114,to_date('01-May-2013','dd-Mon-yyyy'),to_date('01-Jun-2013','dd-Mon-yyyy'),2345,2344
      from dual union all
    select 1233457,26473116,to_date('01-Apr-2013','dd-Mon-yyyy'),to_date('01-May-2013','dd-Mon-yyyy'),12000,12000
      from dual
    ) --  select * from t
    select Account,Invc,p2m prior_2_month,p1m prior_1_month,cm current_month,sysdate
      from (select max(Account) keep (dense_rank first order by invc) Account,
                   Invc,
                   sum(case when start_date = add_months(trunc(sysdate,'mm'),-2) then regular_amount end) p2m,
                   sum(case when start_date = add_months(trunc(sysdate,'mm'),-1) then regular_amount end) p1m,
                   sum(case when trunc(end_date,'mm') = trunc(sysdate,'mm') then trial_amount end) cm
              from t
             group by Invc
    where coalesce(prior_2_month,prior_1_month,current_month) is not null
    order by Account,Invc
    ACCOUNT
    INVC
    PRIOR_2_MONTH
    PRIOR_1_MONTH
    CURRENT_MONTH
    SYSDATE
    1233456
    26471000
    19
    08/19/2013
    1233456
    26472001
    45
    67
    08/19/2013
    1233457
    26471110
    500
    08/19/2013
    1233457
    26472112
    100
    100
    08/19/2013
    Regards
    Etbin

  • Dynamic PIVOT in T-SQL or somewhere else?

    In a
    recent thread on dynamic SQL, the issue of dynamic PIVOT came up.
    Dynamic PIVOT can be implemented in T-SQL with dynamic SQL:
    Pivots with Dynamic Columns in SQL Server 2005
    http://sqlhints.com/2014/03/18/dynamic-pivot-in-sql-server/
    http://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query
    http://www.sqlusa.com/bestpractices2005/dynamicpivot/
    Alternatives:
    1. Enhancement by MS to the current static PIVOT which has limited use since it is not data-driven
    2. Use SSRS which has built-in dynamic columns
    3. Use client app
    What is your opinion? Is it OK to use dynamic SQL for dynamic PIVOT?
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

    solution elsewhere, for instance with Tablix in SSRS. Pivoting a result set in C# should not be too difficult
    But I could do it much faster in T-SQL with dynamic SQL. 
    If your shop has a reporting system like SSRS and the request is for permanent report, then by all means, SSRS is the choice with built-in dynamic columns.
    C#? I used to know C/C++. So that is not a choice for me. On the other hand, if you are competent in C#, then it may be a better choice, or it may not.
    I'm not sure why people tend to shy away from dynamic SQL, for me, I've never had any problems or issues. Is there any specific reason for this?
    Maybe the multiple single quotes like '''' in concatenation of the dynamic SQL string? When one looks at the following example, it is easy to see that dynamic SQL adds a new level of complexity to static SQL. Yet, the complexity pays off with incredible
    dynamic SQL programming power.
    BLOG: Building Dynamic SQL In a Stored Procedure
    Code example from the blog:
    /* This stored procedure builds dynamic SQL and executes
    using sp_executesql */
    Create Procedure sp_EmployeeSelect
    /* Input Parameters */
    @EmployeeName NVarchar(100),
    @Department NVarchar(50),
    @Designation NVarchar(50),
    @StartDate DateTime,
    @EndDate DateTime,
    @Salary Decimal(10,2)
    AS
    Set NoCount ON
    /* Variable Declaration */
    Declare @SQLQuery AS NVarchar(4000)
    Declare @ParamDefinition AS NVarchar(2000)
    /* Build the Transact-SQL String with the input parameters */
    Set @SQLQuery = 'Select * From tblEmployees where (1=1) '
    /* check for the condition and build the WHERE clause accordingly */
    If @EmployeeName Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (EmployeeName = @EmployeeName)'
    If @Department Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Department = @Department)'
    If @Designation Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Designation = @Designation)'
    If @Salary Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Salary >= @Salary)'
    If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
    Set @SQLQuery = @SQLQuery + ' And (JoiningDate
    BETWEEN @StartDate AND @EndDate)'
    /* Specify Parameter Format for all input parameters included
    in the stmt */
    Set @ParamDefinition = ' @EmployeeName NVarchar(100),
    @Department NVarchar(50),
    @Designation NVarchar(50),
    @StartDate DateTime,
    @EndDate DateTime,
    @Salary Decimal(10,2)'
    /* Execute the Transact-SQL String with all parameter value's
    Using sp_executesql Command */
    Execute sp_Executesql @SQLQuery,
    @ParamDefinition,
    @EmployeeName,
    @Department,
    @Designation,
    @StartDate,
    @EndDate,
    @Salary
    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
    Return(0)
    ErrorHandler:
    Return(@@ERROR)
    GO
    LINK:
    http://www.codeproject.com/Articles/20815/Building-Dynamic-SQL-In-a-Stored-Procedure
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • IR with dynamic pivot

    Hi all
    We have been designing resource management system and want to provide flexible way to extend resource properties at runtime. So we are storing resource properties in a single table, e.g.
    select * from _kv
    ID K V
      1  name Bob
      1  age  30
      1  gender male
      2  name Susan
      2  status married
    convert to
    +-----+-------+--------+----------+
    | key | color | height | whatever |
    +-----+-------+--------+----------+
    | 1   | green | 15     | ---      |
    | 2   | ---   | ---    | lol      |
    +-----+-------+--------+----------+
    example of dynamic pivot Dynamic SQL Pivoting &amp;#8211; Stealing Anton&amp;#8217;s Thunder&lt;/title&gt; //&lt;title&gt;AMIS Technology Blog…
    Is it possible to create interactive report with dynamic columns updated when _kv will be changed?
    Is it possible to create add/edit dynamic form depends on key set if we add value type description?
    Thanks

    make sure you put some thought into your database design before you go too far.
    There are many horror stories about EAV based schema designs. (which this design seems to be heading.)
    Read up on them before you go too far.
    -- back to being on topic --
    AFAIK you can not do dynamic SELECT with an Interactive Report.
    However, you can with a Basic Report.  But, it is non-trivial. (ie it is difficult to do)
    Basic Report can use a "function returning SELECT statement".
    You will also need to name the columns based on a different function.
    In order to 'synchronize' the column names with the SELECT statement, you will need a 3rd function that properly generates them.
    This 3rd function MUST have an 'ORDER BY' clause.
    Both the generateSELECT() function and the generateCOLUMN_NAMES() function will call that 3rd function.
    From a code management standpoint, you will need to create a package that contains all three functions.
    are you sure you want to go this route?
    are you ready to go this route?
    Again, think about your table designs.
    MK

  • PIVOT example

    I need to some help to PIVOT my dataset, need to move the description column to individual columns, so I have 1 row for eaach id
    below is an example
    declare @Pivot table (
    S_dates datetime
    , id varchar(10)
    , code varchar(100)
    , description varchar(max))
    insert @Pivot select '2014-03-24 00:00:00.000','1580/7','07611819360532','Matrix 90° L-Plate, medium, 2+2 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert @Pivot select '2014-03-24 00:00:00.000','1580/7','07611819362369','Matrix Screw Ø 1.85 mm, self-drilling, length 4 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-24 00:00:00.000','1580/7','07611819362383','Matrix Screw Ø 1.85 mm, self-drilling, length 5 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-24 00:00:00.000','1580/7','07611819362437','Matrix Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','7551/4','07611819362406','Matrix Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','7551/4','07611819362437','Matrix Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','7551/4','07611819377523','Matrix Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','4820/4','07611819360709','Matrix Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','4820/4','07611819362222','Matrix Screw Ø 1.85 mm, self-tapping, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','4820/4','07611819377523','Matrix Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','3738/4','07611819360709','Matrix Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','3738/4','07611819362222','Matrix Screw Ø 1.85 mm, self-tapping, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','3738/4','07611819362406','Matrix Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','3738/4','07611819362437','Matrix Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert @Pivot select '2014-03-21 00:00:00.000','3738/4','07611819377523','Matrix Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','6578/1','07611819360709','Matrix Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert @Pivot select '2014-03-21 00:00:00.000','6578/1','07611819362406','Matrix Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert @Pivot select '2014-03-12 00:00:00.000','7666/1','07611819381100','Drill Bit Ø 1.5 mm with Stop, length 50/6 mm, 2-flute, for J-Latch Coupling, sterile'
    select * from @Pivot

    Hi,
    Please see the Query below hope it helps. Its dynamic with data. I have used Temp Tables instead of table variables. Hope its helps.
    Create
    table #Pivot (
          S_dates
    datetime
    , id varchar(10)
    , code varchar(100)
    , description
    varchar(max))
    insert #Pivot
    select '2014-03-24 00:00:00.000','1580/7','07611819360532','Matrix
    90° L-Plate, medium, 2+2 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert #Pivot
    select '2014-03-24 00:00:00.000','1580/7','07611819362369','Matrix
    Screw Ø 1.85 mm, self-drilling, length 4 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-24 00:00:00.000','1580/7','07611819362383','Matrix
    Screw Ø 1.85 mm, self-drilling, length 5 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-24 00:00:00.000','1580/7','07611819362437','Matrix
    Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','7551/4','07611819362406','Matrix
    Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','7551/4','07611819362437','Matrix
    Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','7551/4','07611819377523','Matrix
    Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','4820/4','07611819360709','Matrix
    Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','4820/4','07611819362222','Matrix
    Screw Ø 1.85 mm, self-tapping, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','4820/4','07611819377523','Matrix
    Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','3738/4','07611819360709','Matrix
    Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','3738/4','07611819362222','Matrix
    Screw Ø 1.85 mm, self-tapping, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','3738/4','07611819362406','Matrix
    Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','3738/4','07611819362437','Matrix
    Screw Ø 2.1 mm, self-tapping, length 4 mm, Titanium Alloy (TAN), sterile, pack of 1 unit in Clip'
    insert #Pivot
    select '2014-03-21 00:00:00.000','3738/4','07611819377523','Matrix
    Drill Bit Ø 1.4 mm with Stop, length 44.5/8 mm, for J-Latch Coupling, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','6578/1','07611819360709','Matrix
    Anatomic L-Plate, short, 3+3 holes, reversible, thickness 0.8 mm, Pure Titanium, sterile'
    insert #Pivot
    select '2014-03-21 00:00:00.000','6578/1','07611819362406','Matrix
    Screw Ø 1.85 mm, self-drilling, length 6 mm, Titanium Alloy (TAN), sterile, pack of 4 units in Clip'
    insert #Pivot
    select '2014-03-12 00:00:00.000','7666/1','07611819381100','Drill
    Bit Ø 1.5 mm with Stop, length 50/6 mm, 2-flute, for J-Latch Coupling, sterile'
    select
    * from #Pivot
    Declare @SQL 
    Varchar(max)
    ,  @Var  
    Varchar(max)
    SELECT
            @Var=coalesce(@Var+',','')+'['
    + code +']'
    FROM
    (Select
    DISTINCT code from #Pivot)
    as A
    Select @Var
    SET @SQL
    = ''
    SET @SQL
    = '
    SELECT id, S_dates,'
    +
    @Var
    +'
    FROM
    (SELECT *
        FROM #Pivot) AS SourceTable PIVOT
    MAX(description)
    FOR Code IN ('+ @Var
    + ')
    ) AS PivotTable;'
    Exec
    (@SQL)
    Regards, PS

  • Pivoting string values( ,comma seperator) to separate columns(usingREG_EXP)

    <pre>
    I have column PART which contains string as shown below
    PART
    BMW PART,TEST PART,TEST PART,HYPER PART,HYPER PART,TESTINGNEWONE,TESTINGNEWONE,TESTINGNEWONE
    string has got comma separated, i need to project as individual columns like
    P1 P2 .... P8
    BMW PART TEST PART TESTINGEWONE
    how can it be done... (i think by using regular expressions it can be done but i am not aware of it can any one help)
    </pre>
    Thanks
    Naren

    Hi,
    Narendar.Vishwanatham wrote:
    thanks a lot for your query....
    cant we make it dynamically instead of hardcoding each columnThe number of columns produced by a query must be hard-coded into the query.
    Dynamic SQL means that you have some program (often another query) write all or part of the main query for you.
    The following thread has an example of a dynamic pivot in SQL*Plus:
    Help for a query to add columns
    The following thread discusses different options for dealing with an unknown number of columns after the pivot:
    Re: Report count and sum from many rows into many columns
    (and i guess the answer you gave is not possible to get with out using regular expresssions right !!!)No, you can get the same results using INSTR and SUBSTR.
    Whenever you ask for help, post a little sample data (CREATE TABLE and INSERT statements), and the results you want from that data.
    Always mention what version of Oracle (and any other relevant tools) you're using. This is especially important with pivots; recent versions of Oracle have helpful new features.

  • Very Complex SQL Query

    Hi,
    I have three tables
    master(m_id m_name)
    1 m1
    2 m2
    3 m3
    t1(m_id, int_type)
    1 INT(2)
    1 INT(3)
    t2(m_id, char_type)
    1 CHAR(3)
    1 CHAR(4)
    master table has m_id as a primary key, while this is foreign key in tables t1 and t2. Now i want to get the result like this.
    m1 INT(2) INT(3) CHAR(3) CHAR(4)
    [All these rows should be converted into columns]
    Thanx in Advance

    Hi,
    Here's a sample based on pivoting:TEST.SQL>CREATE TABLE MASTER
      2  (
      3       M_ID NUMBER PRIMARY KEY,
      4       M_NAME VARCHAR2(5)
      5  );
    Table created.
    TEST.SQL>INSERT INTO MASTER VALUES (1,'m1');
    1 row created.
    TEST.SQL>INSERT INTO MASTER VALUES (2,'m2');
    1 row created.
    TEST.SQL>INSERT INTO MASTER VALUES (3,'m3');
    1 row created.
    TEST.SQL>
    TEST.SQL>CREATE TABLE T1
      2  (
      3       M_ID NUMBER REFERENCES MASTER(M_ID),
      4       INT_TYPE VARCHAR2(10)
      5  );
    Table created.
    TEST.SQL>
    TEST.SQL>INSERT INTO T1 VALUES (1,'INT(2)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (1,'INT(3)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (2,'INT(1)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(1)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(2)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(4)');
    1 row created.
    TEST.SQL>INSERT INTO T1 VALUES (3,'INT(8)');
    1 row created.
    TEST.SQL>
    TEST.SQL>CREATE TABLE T2
      2  (
      3       M_ID NUMBER REFERENCES MASTER(M_ID),
      4       CHAR_TYPE VARCHAR2(10)
      5  );
    Table created.
    TEST.SQL>
    TEST.SQL>INSERT INTO T2 VALUES (1,'CHAR(3)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (1,'CHAR(4)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (3,'CHAR(9)');
    1 row created.
    TEST.SQL>INSERT INTO T2 VALUES (3,'CHAR(10)');
    1 row created.
    TEST.SQL>
    TEST.SQL>
    TEST.SQL>COMMIT;
    Commit complete.
    TEST.SQL>
    TEST.SQL>SELECT M_NAME, MAX(V1), MAX(V2), MAX(V3), MAX(V4), MAX(V5), MAX(V6)
      2  FROM
      3       (
      4       SELECT
      5            M_NAME,RN,DECODE(RN,1,VAL) V1,DECODE(RN,2,VAL) V2,DECODE(RN,3,VAL) V3,DECODE(RN,4,VAL) V4,DECODE(RN,5,VAL) V5,DECODE(RN,6,VAL) V6
      6       FROM
      7                 (
      8                 SELECT M_NAME, VAL, ROW_NUMBER() OVER (PARTITION BY M_NAME ORDER BY VAL) RN
      9                 FROM
    10                           (
    11                           SELECT MASTER.M_NAME, T1.INT_TYPE VAL
    12                           FROM MASTER, T1
    13                           WHERE T1.M_ID=MASTER.M_ID
    14                           UNION ALL
    15                           SELECT MASTER.M_NAME, T2.CHAR_TYPE VAL
    16                           FROM MASTER, T2
    17                           WHERE T2.M_ID=MASTER.M_ID
    18                           ) SRC
    19                 )
    20       ) TS
    21  GROUP BY M_NAME
    22  ;
    M_NAM MAX(V1)    MAX(V2)    MAX(V3)    MAX(V4)    MAX(V5)    MAX(V6)
    m1    CHAR(3)    CHAR(4)    INT(2)     INT(3)
    m2    INT(1)
    m3    CHAR(10)   CHAR(9)    INT(1)     INT(2)     INT(4)     INT(8)Note that this will work only if the max number of columns is 6.
    It's possible (I already did it) to build a function that returns a dynamix result set from this query build dynamically. I'll let you do so!
    Regards,
    Yoann.

  • Can't Install Powe Pivot

    Hello,
    I have Office 2010,32 bit, Standard Version and I need to install Power Pivot for Excel 2010. I have tried to install de 32 bit version  (SQL Server 2008), but it says that I need to install Office Shared Features and Excel 2010, which are already installed.
    I have tried with other versions of Power Pivot, but I can't neither. I have also installed Visual Studio Tools for Office Runtime (also 32 bit), but I still can't install Powe Pivot, for SQL Server 2012 version. 
    Even more, I've been searching the solution in this forum, with persons that have the same problem as me, and when I try to implement the solution, it doesn't result. 
    Please help me to solve this problem.

    Hi,
    In regarding of the issue, please provide us more information to assist you better.
    DO you mind telling us your Hardware/Software information?
    Where did you download the Power Pivot for Excel 2010?
    Would you like to share us the whole error message?
    Based on the information that you mentioned above, we need to use the 32bit "Microsoft SQL Server 2008 R2 - PowerPivot for Microsoft Excel 2010 - RTM" with 32bit Excel
    2010. We could see the information in "Install Instruction", I copied here:
    >>Important : If you choose a custom installation of the Microsoft Office 2010, you must install both Excel 2010 and Office Shared Features in order to use PowerPivot for Excel.<< Please go to control panel to check Office Shared
    Features is enabled:
    Then, if you did meet the Prerequisites, then delete the power pivot and re-download it and follow the steps in the link and video:
    http://msdn.microsoft.com/en-us/library/gg413462(v=sql.105).aspx
    http://technet.microsoft.com/en-us/sqlserver/dn151340.aspx
    Tips:
    Please installed the prodcut release by runnuing the CMD line as admin.
    Similar issue:
    https://social.technet.microsoft.com/Forums/en-US/451eef18-5bf2-44bd-90e3-5507a9e834f2/installing-power-pivot?forum=sqlkjpowerpivotforexcel
    Hope it's helpful.
    Regards,
    George Zhao
    TechNet Community Support
    It's recommended to download and install
    Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in
    Office programs.

  • Incorrect data value selected when clicking on slicer entry in Excel Services

    We have uploaded various Excel 2013 files containing pivot tables to Sharepoint 2013 and viewed the pivot tables in a browser. The referenced pivot table data is normally a SSAS tabular model. This has been working fine for over 12 months apart
    from one irritating feature.
    Sometimes a clicked slicer entry does not result in the correct slicer value being selected. This typically occurs after using the mouse scroll button to scroll down a list of entries in a slicer. When using the keyboard arrow keys to highlight a slicer
    value and pressing Return to select an entry, the correct value does always seem to be selected - so it appears to point to a mouse scrollwheel issue. Also, after scrolling through slicer entries using the mouse scroll wheel, there appears to be some sort
    of slicer refresh problem that results in a blank entry/entries being displayed either at the top or bottom of a slicer. However, sometimes the correct 'tooltip' is shown for this blank slicer entry. One other point - when using the slicer scrollbar the scroll
    down a list of slicer entries using Firefox, the problem does not seem to occur. IE does not seem to display the same type of slicer scrollbars (only active when the mouse hovers over the slicer) and so this is more difficult to replicate in Ie.
    The worrying factor is that the user is often unaware that the incorrect slicer value has been selected. The problem can be replicated in both IE11 and Firefox. Also our client systems accessed via Citrix display the same problems.
    Any help greatly appreciated. Thanks.
    Graham Goodwin Email: [email protected]

    Hi Graham,
    i built the same version, please correct me if should your environment, is sharepoint 2013 with march 2013 update and your office also the same update. in my labbox seems the issue is not re-producible.
    after some discussion with my colleague, seems there was issue similar like this back in 2010 Excel and the remedy is to update the excel services and to re-install the power pivot at SQL server. back then the issue is because when we scroll the selection,
    it should be locked, so then when we scroll it may have the position to update and refresh the data. one of the workaround is by clicking the item and press enter, by doing this it will refresh the page correctly. the link was:http://www.microsoft.com/en-us/download/details.aspx?id=29074
    i tried to look at our update for the excel services 2013, there are updates in every cumulative updates but there is no particlular update regarding this issue, perhaps you may try this to check in your development environment, if should it may fix the
    issue.
    to re-install the power pivot, you may need to do this on your sql side. 
    http://technet.microsoft.com/en-us/library/ee210708.aspx
    http://technet.microsoft.com/en-us/library/ee210671.aspxi may suggest you to check the ULS log, if should there are issues with powerpivot also, to make sure if should there are no issue
    regarding power pivot. if there is no issues on the powerpivot, then you may need to open a ticket to report this issue, so that we can have a detailed checking to your environment remotely.
    Regards,
    Aries
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

  • Top N Analysis with multiple columns

    Hi
    I am using Oracle 9i. I do have a table which contains datewise promotional material types for an organisation.
    The structure is as follows:
    CREATE TABLE TEST
    (CDATE DATE,
    BROCHURE VARCHAR2(1),
    WEBSITE VARCHAR2(1),
    DIRECT_MAIL VARCHAR2(1),
    PRESS_RELEASE VARCHAR2(1),
    JOURNAL_AD VARCHAR2(1)
    and the sample data is as follows:
    CDate          Brochure     Website     Direct_Mail     Press_Release Journal_Ad
    01/04/1996     Y Y Y N N
    02/04/1996     Y Y N N N
    23/06/1996     Y N Y Y N
    13/09/1996     Y Y N N N
    01/04/1997     Y Y N N N
    02/04/1997     Y Y Y N Y
    23/06/1997     N Y N N Y
    13/09/1997     Y Y N N N
    01/04/1998     Y Y Y N N
    02/04/1998     Y N N Y N
    23/06/1998     N Y N N Y
    13/09/1998     Y Y N N Y
    01/04/1999     Y Y Y N Y
    02/04/1999     Y N N Y N
    23/06/1999     N Y N N N
    13/09/1999     Y Y Y N N
    I want to have year wise top 4 promotional types in terms of count of 'Y' only. The result should be like as follows:
    YEAR:1996
    TYPE     COUNT
    BROCHURE 4
    WEBSITE 3
    DIRECT_MAIL 2
    PRESS_RELEASE 1
    JOURNAL_AD 0
    YEAR:1997
    TYPE     COUNT
    WEBSITE 4
    BROCHURE 3
    JOURNAL_AD 2
    DIRECT_MAIL 1
    PRESS_RELEASE 1
    Please suggest a solution for the same. I am not able to sort it for multiple columns.
    Regards
    MS

    One of the questions that must be asked when you have a requirement to only show the top N ranked items in a list, is "what about a tie in the ranking?".
    Oracle has two ranking functions that allow you to deal with either requirement - RANK and DENSE_RANK. Both operate as either analytic or aggregate functions, so either will work for your requirements. The previous posting by Miguel demonstrated how to decode your Y/N flags and pivot the data.
    In this example, I've taken the liberty of adding some data to year 2000 that will show the difference between RANK and DENSE_RANK as well as how to use them to filter your results.
    First, here's the decoded/pivoted data:
    SQL>WITH test AS
      2  (         SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      3  UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      4  UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      5  UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      6  UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      7  UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      8  UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      9  UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    10  UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    11  UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    12  UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    13  UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    14  UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    15  UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    16  UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    17  UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    18  UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    19  UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    20  UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    21  UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    22  )
    23  SELECT cyear
    24        ,ctype
    25        ,RANK()       OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
    26        ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
    27  FROM (SELECT TRUNC(CDATE,'Y') CYEAR
    28             ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
    29        FROM test
    30        GROUP BY TRUNC(CDATE,'Y')
    31        UNION ALL
    32        SELECT TRUNC(CDATE,'Y') CYEAR
    33             ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
    34        FROM test
    35        GROUP BY TRUNC(CDATE,'Y')
    36        UNION ALL
    37        SELECT TRUNC(CDATE,'Y') CYEAR
    38             ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
    39        FROM test
    40        GROUP BY TRUNC(CDATE,'Y')
    41        UNION ALL
    42        SELECT TRUNC(CDATE,'Y') CYEAR
    43             ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
    44        FROM test
    45        GROUP BY TRUNC(CDATE,'Y')
    46        UNION ALL
    47        SELECT TRUNC(CDATE,'Y') CYEAR
    48             ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
    49        FROM test
    50        GROUP BY TRUNC(CDATE,'Y')
    51       )
    52* order by cyear desc, ranking
    53  /
    CYEAR                         CTYPE             RANKING DENSE_RANKING
    01-Jan-2000 00:00:00          BROCHURE                1             1
    01-Jan-2000 00:00:00          WEBSITE                 1             1
    01-Jan-2000 00:00:00          DIRECT_MAIL             3             2
    01-Jan-2000 00:00:00          PRESS_RELEASE           4             3
    01-Jan-2000 00:00:00          JOURNAL_AD              5             4
    01-Jan-1999 00:00:00          BROCHURE                1             1
    01-Jan-1999 00:00:00          WEBSITE                 1             1
    01-Jan-1999 00:00:00          DIRECT_MAIL             3             2
    01-Jan-1999 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1999 00:00:00          JOURNAL_AD              4             3
    01-Jan-1998 00:00:00          BROCHURE                1             1
    01-Jan-1998 00:00:00          WEBSITE                 1             1
    01-Jan-1998 00:00:00          JOURNAL_AD              3             2
    01-Jan-1998 00:00:00          DIRECT_MAIL             4             3
    01-Jan-1998 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1997 00:00:00          WEBSITE                 1             1
    01-Jan-1997 00:00:00          BROCHURE                2             2
    01-Jan-1997 00:00:00          JOURNAL_AD              3             3
    01-Jan-1997 00:00:00          DIRECT_MAIL             4             4
    01-Jan-1997 00:00:00          PRESS_RELEASE           5             5
    01-Jan-1996 00:00:00          BROCHURE                1             1
    01-Jan-1996 00:00:00          WEBSITE                 2             2
    01-Jan-1996 00:00:00          DIRECT_MAIL             3             3
    01-Jan-1996 00:00:00          PRESS_RELEASE           4             4
    01-Jan-1996 00:00:00          JOURNAL_AD              5             5You can see that in year 2000 there is a tie for first place (ranking #1). The RANK function will name the second highest count 3 (skipping the rank of 2 due to the tie), while the DENSE_RANK function will not skip a ranking.
    Now, to filter on the ranking, wrap your query in another in-line view like this - but use which ever ranking function YOUR requirements call for:
    SQL>WITH test AS
      2  (         SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      3  UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      4  UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      5  UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      6  UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      7  UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      8  UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      9  UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    10  UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    11  UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    12  UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    13  UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    14  UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    15  UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    16  UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    17  UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    18  UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    19  UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    20  UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    21  UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    22  )
    23  SELECT * FROM (
    24      SELECT cyear
    25            ,ctype
    26            ,RANK()       OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
    27            ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
    28      FROM (SELECT TRUNC(CDATE,'Y') CYEAR
    29                 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
    30            FROM test
    31            GROUP BY TRUNC(CDATE,'Y')
    32            UNION ALL
    33            SELECT TRUNC(CDATE,'Y') CYEAR
    34                 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
    35            FROM test
    36            GROUP BY TRUNC(CDATE,'Y')
    37            UNION ALL
    38            SELECT TRUNC(CDATE,'Y') CYEAR
    39                 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
    40            FROM test
    41            GROUP BY TRUNC(CDATE,'Y')
    42            UNION ALL
    43            SELECT TRUNC(CDATE,'Y') CYEAR
    44                 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
    45            FROM test
    46            GROUP BY TRUNC(CDATE,'Y')
    47            UNION ALL
    48            SELECT TRUNC(CDATE,'Y') CYEAR
    49                 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
    50            FROM test
    51            GROUP BY TRUNC(CDATE,'Y')
    52           )
    53      )
    54  where RANKING <= 4
    55* order by cyear desc, ranking
    56  /
    CYEAR                         CTYPE             RANKING DENSE_RANKING
    01-Jan-2000 00:00:00          WEBSITE                 1             1
    01-Jan-2000 00:00:00          BROCHURE                1             1
    01-Jan-2000 00:00:00          DIRECT_MAIL             3             2
    01-Jan-2000 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1999 00:00:00          BROCHURE                1             1
    01-Jan-1999 00:00:00          WEBSITE                 1             1
    01-Jan-1999 00:00:00          DIRECT_MAIL             3             2
    01-Jan-1999 00:00:00          JOURNAL_AD              4             3
    01-Jan-1999 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1998 00:00:00          BROCHURE                1             1
    01-Jan-1998 00:00:00          WEBSITE                 1             1
    01-Jan-1998 00:00:00          JOURNAL_AD              3             2
    01-Jan-1998 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1998 00:00:00          DIRECT_MAIL             4             3
    01-Jan-1997 00:00:00          WEBSITE                 1             1
    01-Jan-1997 00:00:00          BROCHURE                2             2
    01-Jan-1997 00:00:00          JOURNAL_AD              3             3
    01-Jan-1997 00:00:00          DIRECT_MAIL             4             4
    01-Jan-1996 00:00:00          BROCHURE                1             1
    01-Jan-1996 00:00:00          WEBSITE                 2             2
    01-Jan-1996 00:00:00          DIRECT_MAIL             3             3
    01-Jan-1996 00:00:00          PRESS_RELEASE           4             4

  • Please help to generate the table from column to rows

    Hello -
    I have one table with more than 100 columns there are 70 column start with HC1.....HC70 (they are not in sequence) and has some value 0 or 1
    Table structure:
    HICN_ID HC1 HC2 HC4 HC5 HC6.................................HC70
    1234A 0 1 1 0 1 1
    3456D 1 0 0 1 0 0
    Now What i want is like this..
    HICN_ID HC
    1234A 2
    1234A 4
    1234A 6
    3456D 1
    3456D 5
    Can you please help me on this
    thanks
    nick

    Please look at the same scenario from the below link.
    You can accomplish this by a "pivot" query. Please look at the small testcase that I prepared below:
    SQL> desc t1
    Name Null? Type
    NAME VARCHAR2(10)
    YEAR NUMBER(4)
    VALUE NUMBER(4)
    SQL>
    SQL> select * from t1;
    NAME YEAR VALUE
    john 1991 1000
    john 1992 2000
    john 1993 3000
    jack 1991 1500
    jack 1992 1200
    jack 1993 1340
    mary 1991 1250
    mary 1992 2323
    mary 1993 8700
    9 rows selected.
    SQL> -- now, try out the pivot query
    SQL> select year,
    2 max( decode( name, 'john', value, null ) ) "JOHN",
    3 max( decode( name, 'jack', value, null ) ) "JACK",
    4 max( decode( name, 'mary', value, null ) ) "MARY"
    5 from
    6 (
    7 select name, year, value
    8 from t1
    9 )
    10 group by year ;
    YEAR JOHN JACK MARY
    1991 1000 1500 1250
    1992 2000 1200 2323
    1993 3000 1340 8700
    SQL>
    Hope that helps.
    Source : http://p2p.wrox.com/oracle/11931-sql-query-convert-columns-into-rows.html
    Thanks,
    Balaji K.

  • How to Create a Dynamic Crosstab Query?

    Hi friends,
    I have a table like:
    DateOfSale   SalesPerson    SalesAmount
    Which may have
    any number of sales' staff (old/new workers) who have sold goods on different dates which may range at
    any length of time.
    Any suggestion for an appropriate query / method to derive a crosstab / pivot result as
    SalesPerson   Year1   Year2   Year3
    With field SalesPerson representing
    each of the staff member and all the amounts added
    in terms of years, whatever the range may exist in time, instead of providing names and years in a hard-coded manner?
    Looking forward for your expert advices.
    Thanx in advance.
    Best Regards, Faraz A Qureshi

    like this
    ;With CTE
    AS
    SELECT ROW_NUMBER() OVER (PARTITION BY SalesPerson ORDER BY DATEDIFF(yy,0,DateOfSale)) AS Seq,
    DATEADD(yy,DATEDIFF(yy,0,DateOfSale),0) AS YrDate,
    SalesPerson,
    SUM(SalesAmount) AS SalesAmount
    FROM Table
    GROUP BY DATEDIFF(yy,0,DateOfSale),SalesPerson
    SELECT SalesPerson,
    [1] AS Year1,
    [2] AS Year2,
    [3] AS Year3,
    [4] AS Year4
    FROM CTE
    PIVOT (MAX(SalesAmount) FOR Seq IN ([1],[2],[3],[4]))p
    if you want to make this dynamic see
    http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • A query in AdventureWorks2012

    Hi,
    i have faced with a problem when i was working on a query in AddentureWorks2012 db
    the query is as a follow :
    show the name of product next to its sum(OrderQty) in each years
    LIKE:      Year          |      Name   | Quantity
                  2005                 x1            90
                  2005                 x2            120
                 2006                 x1             
    100
                 2006                 x2              
    40
    that would be your kindness, if you answer my question; Thanks

    Sounds like this to me
    SELECT Name,[1],[2],[3],[4],[5],..
    FROM (
    SELECT ROW_NUMBER OVER (PARTITION BY Name ORDER BY Year) AS Seq,Name,Year,Quantity
    FROM Table
    )t
    PIVOT (SUM(Quantity) FOR Seq IN ([1],[2],[3],[4],..))p
    If you want to make it dynamic based on available years 
    see
    http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Select with transposition and join

    Hi Guys!
    I'm using Oracle 9i and I'm facing the following problem:
    My table looks like this:
    Stripe
    Unit
    Value
    1
    1
    a1
    1
    2
    a2
    1
    3
    a3
    2
    1
    b1
    2
    2
    b2
    2
    3
    b3
    2
    4
    b4
    4
    1
    c1
    4
    2
    c2
    4
    3
    c3
    4
    4
    c4
    4
    5
    c5
    My result should look like this:
    Unit
    Stripe1 Value
    Stripe2 Value
    Stripe3 Value
    Stripe4 Value
    1
    a1
    b1
    c1
    2
    a2
    b2
    c2
    3
    a3
    b3
    c3
    4
    b4
    c4
    5
    c5
    I tried it with one select for each stripe and full joins, but in this example I would only see the first 3 units....
    Any ideas?

    Another way, but only if your on database version 11gR1 and onwards, is to use the PIVOT operator:
    SQL> -- generating sample date:
    SQL> with t as (
      2  select 1 stripe, 1 unit, 'a1' value from dual union
      3  select 1, 2, 'a2' from dual union
      4  select 1, 3, 'a3' from dual union
      5  select 2, 1, 'b1' from dual union
      6  select 2, 2, 'b2' from dual union
      7  select 2, 3, 'b3' from dual union
      8  select 2, 4, 'b4' from dual union
      9  select 4, 1, 'c1' from dual union
    10  select 4, 2, 'c2' from dual union
    11  select 4, 3, 'c3' from dual union
    12  select 4, 4, 'c4' from dual union
    13  select 4, 5, 'c5' from dual
    14  )
    15  --
    16  -- actual query:
    17  --
    18  select *
    19  from ( select unit
    20         ,      stripe
    21         ,      value
    22         from   t
    23       )
    24  pivot (max(value) for (stripe) in ( 1 as stripe1value
    25                                    , 2 as stripe2value
    26                                    , 3 as stripe3value
    27                                    , 4 as stripe4value
    28                                    )
    29        )
    30  order by unit;
          UNIT ST ST ST ST
             1 a1 b1    c1
             2 a2 b2    c2
             3 a3 b3    c3
             4    b4    c4
             5          c5
    5 rows selected.
    ORACLE-BASE - PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1

  • 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

Maybe you are looking for

  • How do you temporarily disable a page in InDesign without deleting it completely from the document?

    I ask this because I need to create pages that will be relevant at a later time, but I don't want to have to remember the page range everytime I export a pdf because the order of my document is changing constantly. Is ther a way to tag pages as disab

  • What is the best way to create a Playlist?

    I've tried 2 options but none of it got the job done perfectly : #1 - I've created playlists with Media Player on my pc and got it synced the old fasion way. All I got was empty playlist with the right names. #2 - I've copied a folder with some track

  • Oracle application server 10g and fusion middleware 11

    Hi I am confused about architecture of application server 10g and oracle fusion middleware. I have installed oracle application server 10g that includes infrastructure tier ( database repository,SSO,OID,DAS,OCA) and mid tier (OC4j,forms,reports,disco

  • Flex 2 TabNavigator & AccordianNavigator

    I am just starting to learn Flex 2 and have a questions for developrs with Flex experience. One of my current ColdFusion MX7 applications uses a set of custom tags to provide me with Tabbed Data Entry forms with several Accordians on each tabbed page

  • Kernel panic sometime after 10.4.9 software-updated, SMB?

    Apparently samba-networking related - makes sense: it happened as I saved a BBEdit file to an already-SMB-mounted Window2000 server volume (across wired ethernet). Earlier I'd rebooted a couple of times after the update (once then it asked me to, a s