Pivot_data with ratio_to_report

I'm on 12 c and trying to get uses the pivot function to return the numbers and percentages.

My SQL for counties works very well:

with pivot_data as (select 'score', 'Observer_LastName', 'profile_id'

FROM MyTable)

SELECT *.

of pivot_data

PIVOT (count ("profile_id")

TO 'score' IN ('0 ', ' 1',' 1.5 ', 2', 3')

)

This returns the correct information:

Observer_LastName 0 1 1.5 2 3

--------------------------------- ---    ---     ---     ---     ---

Shaon                         0     1      0      1       1

Spillman 169 253 1 283 302

For my percentages, I used this SQL:

with pivot_data as (select "Observer_LastName", "score",)

round (100 * RATIO_TO_REPORT (COUNT (*)))

OVER (PARTITION OF 'Observer_LastName')) AS pct

FROM MyTable

Group "Observer_LastName", "score")

SELECT *.

of pivot_data

PIVOT (MIN (pct)

TO 'score' IN ('0 ', ' 1',' 1.5 ', 2', 3')

)

This is not what I'm waiting for:

Observer_LastName 0 1 1.5 2 3

--------------------------------- ---    ---     ---     ---     ---

Shaon                               5               5       5

7 10 0 11 12 Spillman

I want it to be the percentage of the counts of the SQL above.

It would therefore be count for the individual columns (0,1,1.5,2,3)------total X 100 lines

So, for the 'Harry' rank ' 5' should be '33' - which is (1\3) * 100

For the "Spillman' line '7' must be '17' - which is (169\1008) * 100

the '10' must be '25' - who (253\1008) * 100

etc.

What Miss me?

Hello

Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.

Also post the exact results you want from these data.  If your result conly has 2 rows and 6 columns, it is not hard to publish the complete results.

If you whow what you want to do using commonly available tables, such as those of the scott schema, then you don't need to display the sample data; just make clear that you are using the tables and publish full results and explanation as always.

See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

Maybe you need a WHERE clause, such as

WHERE "score" IN ('0 ', ' 1',' 1.5 ', 2', 3')

in the subquery.

Tags: Database

Similar Questions

  • With overall percentages at end Pivot_data

    I thought I had done this (with the help of Frank), but I have a final request from my users.

    For my percentages, I used this SQL:

    with pivot_data as (select "Observer_LastName", "score",)

    round (100 * RATIO_TO_REPORT (COUNT (*)))

    OVER (PARTITION OF 'Observer_LastName')) AS pct

    FROM MyTable

    where "score" IN ('0 ', ' 1',' 1.5 ', 2', 3')

    Group "Observer_LastName", "score")

    SELECT *.

    of pivot_data

    PIVOT (MIN (pct)

    TO 'score' IN ('0 ', ' 1',' 1.5 ', 2', 3')

    )

    Output is correct:

    Observer_LastName 0 1 1.5 2 3

    --------------------------------- ---    ---     ---     ---     ---

    Shaon                               33              33     33

    17 25 0 30 32 Spillman

    They want to now a 'total' at the end of the report, which is the average of each column (0,1,1.5,2,3) General

    Observer_LastName 0 1 1.5 2 3

    --------------------------------- ---    ---     ---     ---     ---

    Shaon                               33              33     33

    17 25 0 30 32 Spillman

    Total 17 28 0 32 33

    I tried to add a UNION to produce, but I can't do this properly. Can anyone help/suggest? TX

    Hello

    So you want totals for each "Observer_LastName" separately, as well as all of the "Observer_LastName" together. This looks like a job for the REPORT GROUP.

    As you post CREDATE TABLE and INSERT statements for your table, I'll use scott.emp to illustrate:

    WITH pivot_data AS

    (

    SOME jobs, deptno

    TOWER (100

    * RATIO_TO_REPORT (COUNT (*))

    COURSES (WORK PARTITION)

    ) AS pct

    FROM scott.emp

    WHERE deptno IN (10, 20, 30)

    GROUP OF ROLLUP (employment), deptno

    )

    SELECT *.

    of pivot_data

    PIVOT (MIN (pct)

    TO deptno IN (10, 20, 30)

    )

    ORDER BY job

    ;

    Output:

    10-20-30 JOBS

    --------- ---------- ---------- ----------

    100 ANALYST

    25 50 25 CLERK

    MANAGER 33 33 33

    PRESIDENT 100

    SELLER OF 100

    21 36 43

  • Using the Pivot operator with percentages

    I use Oracle 12 c DB. I'm trying to use the SQL pivot operator to give me an output of the matrix. I need to display 2 separate outputs, one with the charges and another with percentages. I have the following SQL to get the counties, but I don't know exactly on the SQL for the percentages. Please notify. TX

    with pivot_data as (select 'score', 'Observer_LastName', 'profile_id'

    FROM MyTable

    where substr ("Observer_LastName", 1, 1) s = ')

    SELECT *.

    of pivot_data

    PIVOT (count ("profile_id")

    FOR 'partition' IN ('0', '1', '2',' 3')

    )

    Hello

    Using the table from scott.emp provided by Oracle, which has these counts for deptno and job:

    WORK DEPT_10 DEPT_20 DEPT_30

    --------- ------- ------- -------

    ANALYST 2

    CLERK 1 2 1

    MANAGER 1 1 1

    PRESIDENT 1

    SELLER 4

    You can get out of the percentages, like this:

    WORK DEPT_10 DEPT_20 DEPT_30

    --------- ------- ------- -------

    ANALYST OF 100.00

    THE CLERK 25.00 50.00 25.00

    MANAGER OF 33.33 33.33 33.33

    PRESIDENT 100.00

    SELLER 100.00

    using a query like this:

    WITH pivot_data AS

    (

    SOME jobs, deptno

    100 * RATIO_TO_REPORT (COUNT (*))

    RESUMED (work PARTITION) AS pct

    FROM scott.emp

    Working GROUP, deptno

    )

    SELECT *.

    OF pivot_data

    PIVOT (MIN (pct)

    TO deptno IN (10 AS dept_10

    20 AS dept_20

    30 AS dept_30

    )

    )

    ORDER BY job

    ;

  • try to get the SUM of all of the charges with the having clause

    Hi Oracle users.

    I'm trying to accomplish to get a sum of a series of charges and it should be easy, but I can't get it.

    For example, I have the following.

    Select ACCTLOC,  count(ACCTLOC) AS TOTALP
    FROM BOX_ACCTS_TBL 
     WHERE TIME_REQ > SYSDATE-120
     Group By ACCTLOC
      having (count(ACCTLOC) > 1)
     
     the results are
     
    ACCTLOC   TOTALP
     
    Philly     15970
    NY          8623
    Tacoma        3
    SanFran     195
    Hartford    5
    Miami       4374
    
      
    

    How can I get the SUM of this group? Do I put this in PL SQL and do a procedure to carry out the selection? Is this the way to go on this?
    I appreciate all the comments you can give.

    Thank you!

    with

    query_result as

    (select 'Philly' acctloc, 15970 totalp Union double all the)

    Select "NY", 8623 Union double all the

    Select "Tacoma", 3 double Union all

    Select "SanFran", 195 double Union all

    Select "Hartford", 5 Union double all the

    Select 'Miami', double 4374

    )

    Select acctloc, totalp, sum (totalp) on the_sum, avg (totalp) (on the_avg), ratio_to_report (totalp) (percentages))

    of query_result

    ACCTLOC TOTALP THE_SUM THE_AVG PERCENTAGES
    Philly 15970 29170 4861.66666666666666666666666666666666667 .547480287967089475488515598217346588961
    NY 8623 29170 4861.66666666666666666666666666666666667 .295611930065135413095646211861501542681
    Tacoma 3 29170 4861.66666666666666666666666666666666667 .000102845389098388755570791909496057593
    SanFran 195 29170 4861.66666666666666666666666666666666667 .006684950291395269112101474117243743572
    Hartford 5 29170 4861.66666666666666666666666666666666667 .000171408981830647925951319849160095989
    Miami 4374 29170 4861.66666666666666666666666666666666667 .149948577305450805622214604045251971203

    Concerning

    Etbin

  • Pivot with several aggregate function calls?

    Hello

    You use Oracle 11 g R2, with APEX 4.2.3 I'm taking of the cross tab (pivot) built this query:

    Start
    return ' with pivot_data like)
    SELECT department_id, job_id, salary
    Of oehr_employees
    )
    SELECT *.
    OF pivot_data
    PIVOT)
    Salary
    FOR department_id
    IN ('| v ('P5_DEPT_LIST') |')
    )';
    end;

    That works very well and add in the comm column and also have the sum of pivot and build department_id in database columns...  I've not found no documents showing how have several columns summed up in a pivot query again.

    Any ideas?

    Thank you

    Tony Miller
    Software LuvMuffin
    Ruckersville, WILL

    TexasApexDeveloper wrote:

    I've not found no documents showing how have several columns summed up in a pivot query again.

    Not even THE official documentation?

    http://docs.Oracle.com/CD/E11882_01/server.112/e25554/analysis.htm#DWHSG8731

  • Query with join optimization research and details of the extra column

    I have the following SQL used for a report that comes out some stats (with some research of names). There is a good chance it is probably possible to optimize with better SQL, but I also hope to add an additional column, which I'm not sure.

    I want the extra column at one percent, which is total % of the lines of the value of the units, for the combination of category/group.

    Oracle SQL is v11.2.0

    Here's the SQL code, as it is currently:

    select a.date_adjusted, 
           a.task_name,
           sum(case when a.units_adjusted is not null then a.units_adjusted else a.units_original end) Units, 
           b.group_name, 
           b.category_name
    from   actuals_intake a
    left join
    -- lookups to obtain group and category names from their ID's in the groupings table
           (select c.task_id, 
                   d.group_name, 
                   e.category_name, 
                   c.business_unit_id
            from   task_groupings c,
                   task_groups d,
                   task_categories e
            where  c.group_id = d.id
            and    c.business_unit_id = d.business_unit_id
            and    c.category_id = e.id
            and    c.business_unit_id = e.business_unit_id
    ) b
    on    a.task_id = b.task_id
    and   a.business_unit_id = b.business_unit_id
    where a.business_unit_id = :P10_SELECT_BUSINESS_UNIT
    and   a.date_adjusted between to_date(:P10_DATE_START, 'dd-mon-yyyy') and to_date(:P10_DATE_END, 'dd-mon-yyyy')
    group by a.date_adjusted, a.task_name, b.group_name, b.category_name
    order by a.date_adjusted, b.category_name, b.group_name
     

    This will set up the tables and data:

    CREATE TABLE ACTUALS_INTAKE (
    ID NUMBER,
    DATE_ORIGINAL DATE,
    TASK_NAME VARCHAR2(500 CHAR),
    TASK_ID NUMBER,
    UNITS_ORIGINAL NUMBER,
    BUSINESS_UNIT_ID NUMBER,
    SUB_UNIT_ID NUMBER,
    DATE_ADJUSTED DATE,
    UNITS_ADJUSTED NUMBER
    );
    CREATE TABLE TASK_CATEGORIES (
    ID NUMBER, 
    CATEGORY_NAME VARCHAR2(100 CHAR), 
    BUSINESS_UNIT_ID NUMBER
    );
    CREATE TABLE TASK_GROUPS (
    ID NUMBER, 
    GROUP_NAME VARCHAR2(100 CHAR), 
    BUSINESS_UNIT_ID NUMBER
    );
    CREATE TABLE TASK_GROUPINGS (
    TASK_ID NUMBER, 
    GROUP_ID NUMBER, 
    CATEGORY_ID NUMBER, 
    BUSINESS_UNIT_ID NUMBER
    );
     
     
    INSERT ALL
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (1, '03/15/2014', 'Task One', 1, 200, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (2, '03/15/2014', 'Task Two', 2, 30, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (3, '03/15/2014', 'Task Three', 3, 650, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (4, '03/15/2014', 'Task Four', 4, 340, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (5, '03/14/2014', 'Task Four', 4, 60, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (6, '03/15/2014', 'Task Five', 5, 15, 10, null, '03/15/2014', null)
    INTO ACTUALS_INTAKE (ID, DATE_ORIGINAL, TASK_NAME, TASK_ID, UNITS_ORIGINAL, BUSINESS_UNIT_ID, SUB_UNIT_ID, DATE_ADJUSTED, UNITS_ADJUSTED)
    VALUES (7, '03/15/2014', 'Task Six', 6, 40, 10, null, '03/15/2014', null)
    SELECT 1 FROM DUAL;
     
     
    INSERT ALL
    INTO TASK_GROUPS (ID, GROUP_NAME, BUSINESS_UNIT_ID)
    VALUES (1, 'Group One', 10)
    INTO TASK_GROUPS (ID, GROUP_NAME, BUSINESS_UNIT_ID)
    VALUES (2, 'Group Two', 10)
    INTO TASK_GROUPS (ID, GROUP_NAME, BUSINESS_UNIT_ID)
    VALUES (3, 'Group Three', 10)
    select 1 from dual;
    
     
    INSERT ALL
    INTO TASK_CATEGORIES (ID, CATEGORY_NAME, BUSINESS_UNIT_ID)
    VALUES (1, 'Category A', 10)
    INTO TASK_CATEGORIES (ID, CATEGORY_NAME, BUSINESS_UNIT_ID)
    VALUES (2, 'Category A', 10)
    INTO TASK_CATEGORIES (ID, CATEGORY_NAME, BUSINESS_UNIT_ID)
    VALUES (3, 'Category B', 10)
    select 1 from dual;
    
     
    INSERT ALL
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (1, 1, 1, 10)
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (2, 1, 1, 10)
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (3, 2, 2, 10)
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (4, 2, 3, 10)
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (5, 3, 3, 10)
    INTO TASK_GROUPINGS (TASK_ID, GROUP_ID, CATEGORY_ID, BUSINESS_UNIT_ID)
    VALUES (6, 3, 3, 10)
    select 1 from dual;
     

    Results will look like this. The last column is what I want the extra column to look like:

    Date_Adjusted TaskName Units of GroupName Category_Name Units %
    15/03/2014A task200Group 1Category A87
    15/03/2014Task 230Group 1Category A13
    15/03/2014Task 3650Group twoCategory A100
    15/03/2014Task 515Group threeCategory B27
    15/03/2014Task 640Group threeCategory B73
    15/03/2014Task 4400Group twoCategory B100

    Hope all that makes sense... Anyone able to help me do this effectively?

    Hello

    Use the analytical RATIO_TO_REPORT function to calculate the % of units column.

    If you're serious about performance, please refer to the Forum:

    Re: 3. how to improve the performance of my query? / My query is slow.

    Do you really need an outer join?  Inner joins are faster.  With the given sample data, they produce the same results.

    COALESCE may be a little faster than the CASE.

    Try this:

    WITH got_units AS

    (

    SELECT a.date_adjusted,

    a.Task_Name,

    sum of units (COALESCE (a.units_adjusted, a.units_original));

    b.group_name,

    b.category_name

    of actuals_intake one

    the left join - or just JOINED

    -research for the group names and category of their ID in the table of groupings

    (select c.task_id,

    d.group_name,

    e.category_name,

    c.business_unit_id

    of task_groupings c,.

    task_groups d,

    e task_categories

    where d.id = c.group_id

    and c.business_unit_id = d.business_unit_id

    and c.category_id = e.id

    and c.business_unit_id = e.business_unit_id

    ) b

    On a.task_id = b.task_id

    and a.business_unit_id = b.business_unit_id

    -where a.business_unit_id =: P10_SELECT_BUSINESS_UNIT - if necessary

    - and a.date_adjusted between to_date (: P10_DATE_START, 'Mon-dd-yyyy') and to_date (: P10_DATE_END, ' mon-dd-yyyy "")

    Group of a.date_adjusted, a.task_name, b.group_name, b.category_name

    )

    SELECT u.*

    ROUND (100 * RATIO_TO_REPORT (units) OVER (PARTITION BY groupname)

    category_name

    )

    ) AS units_pct

    OF got_units u

    ORDER BY date_adjusted, category_name, GroupName

    ;

    Thanks for the display of the data of the sample; It is very useful.  Don't try to insert strings, for example, March 15, 2014", in DATE columns.  TO_DATE allows to convert strings to DATEs.

  • using pivot with dynamic?

    Hello
    I have a procedure and I use pivot.
    create or replace
    PROCEDURE YILARALIKLI_HARFDAGILIMI (aranilan_yil in number, birimno in number, sinif number) IS 
    
    a_yil number :=aranilan_yil;   
    
    cursor c_basari is
      WITH PIVOT_DATA AS (
    SELECT   OA.OGRENCI_NO, DK.HARF_KODU,DA.ACILDIGI_YIL
    
    FROM ders_aktif da,ders_tanim dt,ogrenci_akademik oa,ders_kayit dk
    WHERE --da.acildigi_yil='2005'  and
          --oa.ders_baslama_yil=2005 and
          oa.birim like birimno ||'%' and
          (da.acildigi_donem='1'or
          da.acildigi_donem='2')and
          da.ders_kodu like birimno ||'%' and
          (dt.normal_yariyili=sinif or
          dt.normal_yariyili=sinif+1)and
          dt.ders_kodu=da.ders_kodu and
          da.acilan_ders_no=dk.acilan_ders_no and
          oa.ogrenci_no=dk.ogrenci_no and
          dk.harf_kodu is not null
          --GROUP BY DK.HARF_KODU 
          )
          
          SELECT * FROM PIVOT_DATA
         PIVOT(count(distinct ogrenci_no ) SAYI  FOR ACILDIGI_YIL IN(2007,2006,2005,2004,2003));     
          
    BEGIN 
          if sinif=1 then
          a_yil :=aranilan_yil ;
          elsif sinif=3 then
          a_yil :=aranilan_yil -1;
          elsif sinif=5 then
          a_yil:=aranilan_yil-2;
           elsif sinif=7 then  
          a_yil:=aranilan_yil-3;
           end if;
    
    
    
    FOR I IN  c_basari    LOOP 
    
    
       DBMS_OUTPUT.PUT_LINE( I."2007_SAYI" );
       DBMS_OUTPUT.PUT_LINE( I."2006_SAYI" );
       DBMS_OUTPUT.PUT_LINE( I."2005_SAYI" );
       DBMS_OUTPUT.PUT_LINE( I."2004_SAYI" );
       DBMS_OUTPUT.PUT_LINE( I."2003_SAYI" );            
    END LOOP;
    
    
    END YILARALIKLI_HARFDAGILIMI;
    It's ok but I want are 2007,2006,2005,2004,2003 is not static. I use dynamically. How can I do this?

    parameter of the procedure (aranilan_yil) = > 2007 = aranilan_yil
    2006 = aranilan_yil-1
    2005 = aranilan_yil-2
    2004 = aranilan_yil-3
    2003 = aranilan_yil-4

    Published by: esra aktas on 09.Haz.2011 10:07

    Examples:

    CREATE OR REPLACE PROCEDURE DYNAMIC_PIVOT( STARTING_YEAR NUMBER, COLS NUMBER )
    IS
        SQLCOMMAND VARCHAR2(1000);
        I NUMBER;
    BEGIN
    SQLCOMMAND := 'BEGIN FOR c1_rec IN (WITH PIVOT_DATA AS ( ' ||
    'SELECT 1 OGRENCI_NO, 2004 ACILDIGI_YIL FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 2, 2005 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 3, 2006 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 4, 2007 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 5, 2008 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 6, 2005 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 7, 2006 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 8, 2006 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 9, 2007 FROM DUAL ' ||
    'UNION ALL ' ||
    'SELECT 10, 2007 FROM DUAL ' ||
    ') ' ||
    'SELECT * FROM PIVOT_DATA ' ||
    'PIVOT(COUNT (DISTINCT OGRENCI_NO) SAYI FOR ACILDIGI_YIL IN(';
    FOR I IN 1..COLS LOOP
        SQLCOMMAND := SQLCOMMAND || (STARTING_YEAR +1-I);
        IF I < COLS THEN
        SQLCOMMAND := SQLCOMMAND || ',';
        END IF;
    END LOOP;
    SQLCOMMAND := SQLCOMMAND || '))) LOOP ';
    FOR I IN 1..COLS LOOP
        SQLCOMMAND := SQLCOMMAND || ' DBMS_OUTPUT.PUT_LINE( ''' || (STARTING_YEAR+1-I) || ':'' || c1_rec."' || (STARTING_YEAR+1-I) || '_SAYI" );  ';
    END LOOP;
    SQLCOMMAND := SQLCOMMAND || 'END LOOP; END;';
    -- DBMS_OUTPUT.PUT_LINE( SQLCOMMAND ); -- for debug
    EXECUTE IMMEDIATE SQLCOMMAND ;
    END;
    

    out of DYNAMIC_PIVOT EXEC (2008, 5):

    2008:1
    2007:3
    2006:3
    2005:2
    2004: 1

    out of DYNAMIC_PIVOT (2006, 3):

    2006:3
    2005:2
    2004: 1

    I don't know your code, you can adopt this procedure.

    Concerning

    Grosbois

  • How to fill out a sparse table with zero

    Hi all

    I have a sparse table, most of the cells is null, but few of the cells is '1 '.

    I would like to fill the null with zero.

    This table is to rotate a transactional table into a table that describes the attributes and later for data mining


    I think so

    (1) make a user_tab_columns and copy / paste to make a script using NVL

    for example

    Select ' NVL('|| column_name ||) «, 0, » || column_name |'),' of user_tab_columns
    where lower (table_name) = "claims_t1".

    but I have a problem

    for example

    create or replace view claims_t2x
    as
    Select
    NVL ("Diagnostic Imaging' _SPEC_SUM, 0," Diagnostic Imaging' _SPEC_SUM) 'Diagnostic Imaging' _SPEC_SUM
    Of
    claims_t1

    I get the error ORA-00911: invalid character

    as the name of the column got ' ' ' in there.



    or

    Use 2) PL/SQL


    I just do a select and loop through all



    For 2), I don't know how I can get all the column attribute.
    As the attribute in the table operation is not fix so the number of column after swivelling isn't fix


    any idea?


    -Thank you very much for your comments.

    Published by: xwo0owx on April 27, 2011 11:08

    You can use nvl pivot by naming the values in quotes like that.

    CREATE OR REPLACE VIEW emp_v1 AS
    WITH pivot_data
         AS (SELECT deptno,
                    JOB,
                    sal
               FROM emp)
    SELECT JOB,
           NVL ("10", 0) AS "10",
           NVL ("20", 0) AS "20",
           NVL ("30", 0) AS "30",
           NVL ("40", 0) AS "40"
      FROM pivot_data PIVOT (SUM (sal) --<-- pivot_clause
                                      FOR deptno --<-- pivot_for_clause
                                                IN (10, 20, 30, 40) --<-- pivot_in_clause
                                                                   );
    

    G.

  • Problem with my request to percentage

    Hello world

    I'm trying to calculate a percentage, but it seems that something is wrong in my query. I can't seem to get a correct output with a 'good' group by so that the percentage calculation is wrong.

    In fact, I want to calculate the percentage of the number of resources that has HAS_CONSENT = 1, which means that YES and services group.

    Table of resources, sample dataset:

    ID | HAS_CONSENT | CMT
    36444 | 1. GRISOLI\nCOD M-50
    36453 | 0 | M-60 GRISOLI\nCOD
    36455 | 1. GRISOLIE\nCOD M-70
    36479 | 1. BARDOT\nCOD M-70
    36480 | 0 | BARDOT\nCOD M-56
    36481 | 1. PERAGUT\nCOD M-56
    36482 | 1. PERAGUT\nCOD M-56

    What I would like:

    CountRes. HAS_CONSENT | SERVICE | PERCENTAGE_YES
    2. 1. GRISOLI | 66
    1. 0 | GRISOLI |
    1. 1. BARDOT | 50
    0 | 0 | BARDOT |
    2. 1. PERAGUT | 100

    The query I wrote is below, but I don't get what I want. Fact could someone help me?

    SELECT
    County (RES. (' ' ID ') as "CountResource."
    RES. HAS_CONSENT,
    substr (RES. CMT, 0, instr (RES. CMT, Chr (10))) as a "Service."

    case
    WHEN RES. HAS_CONSENT = '1'
    THEN ROUND (100 * RATIO_TO_REPORT (County (RES.) ((' ' ID ')) ON (PARTITION OF RES. » « « « ID »), 2)
    END AS percentage_yes

    Of

    RES THE RESOURCE

    WHERE

    REGEXP_LIKE (RES. ("' CMT ', 'Service', 'i')

    Group of RES. "" THE ID ", RES. HAS_CONSENT, substr (RES. CMT, 0, instr (RES. CMT, CHR (10)))
    order by RES. "" IDENTITY CARD ".

    Hello

    These results, you don't want to GROUP BY id, or you want to PARTITION BY id.
    If each line of output is supposed to represent a combination of service and has_consent, then you should ' GROUP BY service, has_consent.
    If the percentage is believed to be through the service, then use "service PARTITION".

    WITH     got_service     AS
    (
         SELECT     id,
              has_consent,
              SUBSTR ( cmt
                     , 1
                     , INSTR (cmt, CHR (10)) - 1
                     )          AS service
         FROM    resources                    -- RESOURCE is not a good table name
         WHERE     REGEXP_LIKE (cmt, 'Service', 'i')
    )
    SELECT
           COUNT (id)                    AS "CountResource",
           has_consent,
           service                    AS "Service",
           CASE
               WHEN  has_consent  = 1
               THEN  ROUND ( 100 * RATIO_TO_REPORT (COUNT (id)) OVER (PARTITION BY  service)
                     , 2
                     )
           END                         AS percentage_yes
    FROM       got_service
    GROUP BY  service,
           has_consent
    ORDER BY  service,
                 has_consent
    ;
    

    The output you posted does not match the data that you posted, or in your WHERE clause, but I think you want something like that.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

  • How to use the ratio_to_report function

    Hello
    I have a problem using ratio_to_report to create the following report.
    I have a table MYTABLE with three columns A, B and C.
    A, B AND C
    1 150 positive
    1 130 positive
    1 110 negative
    1 150 negative
    1 110 negative
    2 150 negative
    2 110 positive
    2 110 negative
    ...
    I want to get is a report where I have a column reports the percentage of negative situations of a certain group of B value by a column.
    In my case, for example, I would like to provide you a report where the new PERCENTAGE column will contain the negative of the value 150 b within Group 1 rate, then the negative of the rate of 150 in Group 2 and so on for each value of B, which is such a report:
    A PERCENTAGE_NEGATIVE B
    1 150 15%
    1 130 30%
    1 110 0%
    2 150 35%
    2 110 25%
    Pay attention that in the report above, I have not calculated the right percentage value
    Thanks in advance.

    Hello

    Simper is not not to use RATIO_TO_REPORT for that:

    SELECT       a, b
    ,       COUNT ( CASE
                   WHEN  c = 'negative'
                   THEN  1
                END
              ) * 100 / COUNT (*)     AS percentage_negative
    FROM       mytable
    GROUP BY  a, b
    ORDER BY  a, b
    ;
    

    You use RATIO_TO_REPORT, you could do a subquery to calculate RATIO_TO_REPORT for all values of c and then filter all except the "negative in the main query.

    Mark1970 wrote:
    Pay attention that in the report above, I have not calculated the right percentage value

    Why? With only 8 rows of sample data, would be easy enough to manually calculate the correct results? It would be much more useful to people who want to help you.

  • My iPhone 6 installed 10.0.2 stops when it gets to 40% of autonomy.  In addition, it seems to pass power WAY to fast with the new software.  Does anyone else have this problem?

    My iPhone 6 installed 10.0.2 stops when it gets to 40% of autonomy.  In addition, it seems to pass power WAY to fast with the new software.  Does anyone else have this problem?

    Hello brooksm549,
    Thank you for using communities of Apple Support.

    I got your message which, since updating your iPhone 6 to iOS 10.0.2 your iPhone stops when it is 40% and the power to empty very quickly. I understand your concern with the iPhone turn off and drains the battery. I recommend you to review the use of the battery to see what app contributes more to the battery drain. The following article will provide you with steps on how to check the use of the battery:

    On the use of the battery on your iPhone, iPad and iPod touch

    When you know about the soft uses more battery, you can change your settings in order to optimize the battery life:

    Maximize the life of the battery and battery life

    Best regards.

  • I have an ipod touch 128 GB... but I'm almost to reach its maximum.  I want to do is buy a new ipod touch 128 GB and add new music without synchronizing the entire library to it... I have 2 ipods in conjunction with other copies

    I have an ipod touch 128 GB... but I'm almost to reach its maximum.

    I want to do is buy a new ipod touch 128 GB and add new music without synchronizing the entire library to it... I have 2 ipods in conjunction with the other copies of the other.

    is this possible?

    What should I do?

    Matt

    When you get the new iPod, you can use iTunes on your computer to select and synchronize the music you want on it, in the same way that you synchronize your current iPod - your iTunes will recognize them as different devices and will remember your choice of synchronization for each, it will not (unless, for example, you restore the backup of your current on her iPod) put the same content on both.

    (I asked for your post be moved to the iPod Touch forum, where you have posted is the iPad forum use.)

  • I have an IMAC of 2008 with 2 GB of memory plus 300 GB of storage. If I buy 2GB of memory I'll be able to download Sierra?

    I have an IMAC of 2008 with 2 GB of memory plus 300 GB of storage. If I buy 2GB of memory I'll be able to download Sierra?

    Mike,

    No, macOS Sierra is not compatible with your hardware.

    Here is an analytical compatibility.

  • I have a lg 4 k tv (40 "40UH630V TV LG ULTRA HD 4 K) with HDMI 2. 0 and if I buy apple tv 4 can be used with HDMI 2. 0 port or do I HDMI 3 port to use this? Please help me

    I have a lg 4 k tv (40 "40UH630V TV LG ULTRA HD 4 K) with HDMI 2. 0 and if I buy apple tv 4 can be used with HDMI 2. 0 port or do I HDMI 3 port to use this? Please help me

    You can use it with your TV.

  • Voice-over when booting with Sierra

    At the start of my MacBook Pro with Sierra 10.12 it opens with VoiceOver mode. I can spend VoiceOver by clicking on the ' < ' icon but it happen again the next time I reboot.  I checked the system preferences for accessibility, elements of connection and VoiceOver Utility and do not see any setting that he would invoke.

    Voiceover is displayed in the login window?

    System Preferences > users and groups

    To authenticate, click the lock icon. Give the name and the administrator password.

    Select "Connection Options".

    Uncheck the box "use VoiceOver in the login window.

Maybe you are looking for