calculation of ratios and rounding - how the sum of last record to give 1

I'm putting in a single query to calculate the reports for an unknown number of records. I want to turn the ratios to 5 decimal places and I want the total reports of equal to 1.

for example

If I have 3 folders with Qty total overal 4,5,6 is 15
I note 3 lines
Round (4/15.5),
Round (5/15.5),
Round (6/15.5)

but I want to ensure total = 1
so I will try to do something like

Round (4/15.5),
Round (5/15.5),
1 - round(4/15,5) - round(5/15,5)

can someone help show me the sql construction? I've been trying and failing using where clause

of course this example would add to 1 but other unknown ratios may not match up to 1.

Published by: user12154024 on November 2, 2009 22:32

Hello 12154024,

Welcome to the forum. May not be a more elegant solution, but it shows how it works step by step

WITH t AS (
SELECT 4 n FROM dual
UNION ALL
SELECT  5 n FROM dual
UNION ALL
SELECT 6 n FROM dual
)
,t1 AS (
SELECT  n
       ,ROUND(RATIO_TO_REPORT(n) OVER (),5) AS rr
       ,COUNT(n) OVER () AS c
       ,ROW_NUMBER() OVER (ORDER BY n) AS rn
FROM    t
)
,t2 AS (
SELECT  n
       ,rr
       ,SUM(rr) OVER (ORDER BY n) s
       ,c
       ,rn
FROM t1
)
SELECT  n
       ,rr
       ,CASE WHEN c = rn THEN
            rr + (1 - s)
        ELSE
            rr
        END rr2
       ,s
FROM    t2
ORDER BY rn;

N   RR        RR2       S
--- --------- --------- --------
4   0.26667   0.26667   0.26667
5   0.33333   0.33333   0.6
6   0.4       0.4       1        

3 rows selected

The part "WITH t AS" generates data
T1 calculate the ratio, counts the number of records and signs a current issue
T2 to calculate a cumulative ratio
the last part takes these data and for the last record (number = count running) it allows to adjust the ratio.

You can see if you are using other data

WITH t AS (
SELECT 4.78 n FROM dual
UNION ALL
SELECT  5 n FROM dual
UNION ALL
SELECT 6 n FROM dual
)
...

N    RR       RR2      S
---- -------- -------- --------
4.78 0.30292  0.30292  0.30292
5    0.31686  0.31686  0.61978
6    0.38023  0.38022  1.00001  

3 rows selected

Concerning
Marcus

Tags: Database

Similar Questions

  • How the SUM cells including the text and the number?

    Hello world

    Someone can tell me how the SUM cells include the text and the number?

    1 Bob 20

    2 30 Jane

    3 50 Jame

    Total 100

    Thank you

    Hi Jordan,.

    If the numbers are not always two digits, FIND the position of the space between the number and name:

    Formula in B2 (fill down)

    = FIND("",A2)

    Then use the LEFT function to get the number in the column C:

    Formula in C2 (fill down)

    = LEFT (A2, B2)

    This will give a text string (Tip: left-aligned).

    Convert to a numeric value in the D column with the VALUE function:

    Formula in D2 (fill down)

    VALUE = (C2)

    (Tip: propped right)

    Table 1 has a header row and footer of the line, and the SUM is easy. Formula to the D8 footer line:

    = SUM (D)

    Kind regards

    Ian.

    Edit: You can hide columns B and C

    Ian.

  • Then I reinstalled FCPX and some how the scale option is missing in the effects window. Can anyone help?

    Then I reinstalled FCPX and some how the scale option is missing in the effects window. Can anyone help?

    Try resetting your preferences. Organize your Opt - Cmd launches the application. Post screen shots of what you see.

  • How to find inserted last record in the table.

    Version: Oracle 10g

    I have a table called 'Manufacturing' and 3 columns as mfno, itemname, quantity.
    How to find inserted last record in the table 'manufacturing '.

    As I got to know that the Rowid is not a result perfect result. Please provide your inputs.

    user13416294 wrote:
    Version: Oracle 10g

    This is not a version. It's a product name. A version is 10.1.0.2 or 10.2.0.4, etc.

    I have a table called 'Manufacturing' and 3 columns as mfno, itemname, quantity.
    How to find inserted last record in the table 'manufacturing '.

    Not possible as your data model do not answer for him. As simple as that.

    If there is a need to determine an order or associate some time to an entity, then that should be part of the data model - and a relationship, or one or several attributes are necessary to represent this information. Your data model in this case is therefore unable to meet your requirements.

    If the requirements are valid, set the data model. In other words - your question has nothing to do with Oracle and nothing to do with the other pseudo columns in Oracle, the rowscn or the rowid. It is a question of pure data modeling. Nothing more.

  • How to store and search for the sum of primes in a loop from 1 to an arbitrary value of N

    I am interested in a labview 8.5 program to find successive prime numbers from 1 to N arbitrary number given as input and find out their sum as put on front.

    Well well, OK, then. Some honesty. It's actually very refreshing. So what have you tried so far? We can offer tips and advice, but you won't have to do your homework for you. You have an algorithm? Do you know how to determine if a number is a prime number? Start with something simple. Find a way store a running total in a loop. Not too hard. Tip: Shift register (or a more recent version of LabVIEW, Feedback node)

  • How graphic how the sum of several metrics of in a dashboard.

    I'm looking to find out how the graph of the sum of multiples of the metric, as CPU ready, or latency of disk in one graph on a dashboard / report.

    An example would be showing collective management s VM in balloning of CPU and memory of the pool on line on a chart.

    Thank you!

    If you want the chart to reflect everything that you can do the stacking graphics option which will be sleeping all values.

  • How the sum of a series of if Athens?

    Given:

    Image.jpg

    I need adding the cost of reception (PM01) breakfast (Cost01), reception cost (Cost02) and lunch cost of reception (Cost03).

    If the cost of each receipt exceeds each meal allowance, use value meal in sum compensation.

    If the cost of each reception is less than or equal to each value of the meal allowance, then use the cost of reception in all.

    You can use a calculation script customized to the field which displays the sum, something like:

    Custom calculation script

    (function () {}

    Get the field values as numbers

    var v1 = + getField("Cost01").value;

    var v2 = + getField("Cost02").value;

    var v3 = + getField("Cost03").value;

    Add the eligible amounts of breakfast, lunch and dinner

    and set the value of this field

    Event.Value = Math.min (v1, 12) + Math.min (v2, 17) + Math.min (v3, 27);

    })();

  • Columns of the sum of different record count of joined tables

    I have a problem with a query, please help.

    I have two tables:
    create table rec_a (key_code varchar(20),
                    TOT_AMT varchar(20),
                    INV_AMT varchar(20))
    / 
     
    create table rec_B (key_code varchar(20),
                    INVOICE_AMT varchar(20),
                   PAID_AMT varchar(20))
    / 
     
    insert into REC_A values (123123,  1168182.16, 1168182.16);
    
    insert into REC_B values (123123, 205699.04,205699.04);
    insert into REC_B values (123123,130912.78,130912.78);
    insert into REC_B values (123123, 81622.87,81622.87);
    insert into REC_B values (123123, 438032.43,438032.43);
    insert into REC_B values (123123, 159936.17,159936.17);
    insert into REC_B values (123123, 151978.87,151978.87);
     
    Table 1
    KEY_CODE TOT_AMT INV_AMT
    123123 1168182.16 1168182.16


    Table 2
    KEY_CODE INVOICE_AMT PAID_AMT
    123123 205699,04 205699.04
    123123 130912,78 130912.78
    123123 81622,87 81622.87
    123123 438032,43 438032.43
    123123 159936,17 159936.17
    123123 151978,87 151978.87


    I wrote a query to sum up all the areas (tot_amt, inv_amt, invoice_Amt, paid_amt)
    SELECT B.key_code,sum(A.invoice_amt),sum(a.paid_amt),SUM(b.tot_Amt),SUM(b.inv_amt) FROM rec_a B, rec_b A WHERE A.KEY_CODE=B.KEY_CODE AND B.KEY_CODE in ('123123')
    group by B.key_code
    I got this:

    KEY_CODE SUM (A.INVOICE_AMT) SUM (A.PAID_AMT) SUM (B.TOT_AMT) SUM (B.INV_AMT)
    123123 1168182.16 1168182.16 7009092.96 7009092.96

    But I expected this

    KEY_CODE SUM (A.INVOICE_AMT) SUM (A.PAID_AMT) SUM (B.TOT_AMT) SUM (B.INV_AMT)
    123123 1168182.16 1168182.16 1168182.16 1168182.16


    Can someone help me out here?

    Thank you

    Published by: 1003064 on April 29, 2013 03:19

    Hello

    1003064 wrote:
    I have a problem with a query, please help.

    I have two tables:
    Table 1
    KEY_CODE TOT_AMT INV_AMT
    123123 1168182.16 1168182.16

    Table 2
    KEY_CODE INVOICE_AMT PAID_AMT
    123123 205699,04 205699.04
    123123 130912,78 130912.78...
    123123 81622,87 81622.87
    123123 438032,43 438032.43
    123123 159936,17 159936.17
    123123 151978,87 151978.87

    I wrote a query to sum up all the areas (tot_amt, inv_amt, invoice_Amt, paid_amt)

    SELECT B.key_code, sum (a.invoice_amt), sum (a.paid_amt), SUM (b.tot_Amt), SUM (b.inv_amt) FROM rec_a A, rec_b B WHERE A.KEY_CODE = B.KEY_CODE AND B.KEY_CODE in ('123123')
    B.key_code group

    I got this:
    KEY_CODE SUM (A.INVOICE_AMT) SUM (A.PAID_AMT) SUM (B.TOT_AMT) SUM (B.INV_AMT)
    123123 1168182.16 1168182.16 7009092.96 7009092.96

    But I expected this

    KEY_CODE SUM (A.INVOICE_AMT) SUM (A.PAID_AMT) SUM (B.TOT_AMT) SUM (B.INV_AMT)
    123123 1168182.16 1168182.16 1168182.16 1168182.16

    Thus, the last 2 columns must be exactly what is stored in table1? Then either
    b include these columns in the GROUP BY clause and not to use the SUM,
    (b) the GROUP BY on rec_a alone, separately, can't join the results to the rec_b, or
    (c) using MIN or MAX, instead of SUM. (Anyone. Since there is only one value, MIN will be the same as MAX.)

    Here is an example of (a):

    SELECT    B.key_code
    ,       sum (a.invoice_amt)     AS sum_invlice_amt
    ,       sum (a.paid_amt),     AS sum_paid_amt
    ,       b.tot_Amt
    ,       b.inv_amt
    FROM        rec_a          A
    ,        rec_b          B
    WHERE        A.KEY_CODE     = B.KEY_CODE
    AND        B.KEY_CODE      in ('123123')
    group by  B.key_code
    ,            b,tot_amt
    ,       b.inv_amt
    ;
    

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • Ask the deptno 20 last record at the end show

    Oracle 9i Database

    I want to create a query that shows records, Deptno 20 last record at the end.
    application of shot show all records that are in the emp table.
     SQL> select empno,ename,job,deptno, row_number() over (order by deptno) rno from emp;
    
         EMPNO ENAME      JOB           DEPTNO        RNO
    ---------- ---------- --------- ---------- ----------
          7782 CLARK      MANAGER           10          1
          7839 KING       PRESIDENT         10          2
          7934 MILLER     CLERK             10          3
          7369 SMITH      CLERK             20          4
          7876 ADAMS      CLERK             20          5
          7902 FORD       ANALYST           20          6
          7788 SCOTT      ANALYST           20          7
          7566 JONES      MANAGER           20          8
          7499 ALLEN      SALESMAN          30          9
          7698 BLAKE      MANAGER           30         10
          7654 MARTIN     SALESMAN          30         11
    
         EMPNO ENAME      JOB           DEPTNO        RNO
    ---------- ---------- --------- ---------- ----------
          7900 JAMES      CLERK             30         12
          7844 TURNER     SALESMAN          30         13
          7521 WARD       SALESMAN          30         14
    
    14 rows selected.
    
    SQL> 
    I want the result
    EMPNO ENAME      JOB           DEPTNO        RNO
    ---------- ---------- --------- ---------- ----------
    7566 JONES      MANAGER           20          8
    7499 ALLEN      SALESMAN          30          9
    7698 BLAKE      MANAGER           30         10
    7654 MARTIN     SALESMAN          30         11
    7900 JAMES      CLERK             30         12
    7844 TURNER     SALESMAN          30         13
    7521 WARD       SALESMAN          30         14
    Concerning
    with t as
     ( select    7782  e1,'CKING '  e2  ,  ' PRESIDENT '  e3     ,   10  e4    from dual union all
       select    7934, 'MILLER'  ,  'CLERK  '        ,    10       from dual union all
       select    7369, 'SMITH '  ,  'CLERK  '        ,    20       from dual union all
       select    7876, 'ADAMS'   , 'CLERK '        ,     20       from dual union all
       select    7902, 'FORD '   ,  'ANALYST '       ,  20       from dual union all
       select    7788, 'SCOTT'   , 'ANALYST '       ,  20       from dual union all
       select    7788 ,' SCOTT '  ,'ANALYST '  ,       20       from dual union all
       select    7566, 'JONES ',    'MANAGER '   ,     20       from dual union all
       select    7499, 'ALLEN ' ,    'SALESMAN'   ,    30       from dual union all
       select    7698, 'BLAKE ' ,    'MANAGER '    ,   30       from dual union all
       select    7654, 'MARTIN' ,  'SALESMAN '  ,    30       from dual union all
       select    7900, 'JAMES ' ,   'CLERK '      ,       30       from dual union all
       select    7844, 'TURNER' ,  'SALESMAN ' ,     30       from dual union all
       select    7521 ,'WARD ' ,    'SALESMAN ' ,     30       from dual
    )
    SELECT o.*
      FROM (SELECT p.*,
                   MAX (CASE
                           WHEN p.e4 = 20
                              THEN p.rno
                        END) OVER (ORDER BY p.e4) AS mx
              FROM (SELECT a.*, ROW_NUMBER () OVER (ORDER BY e4) rno
                      FROM t a) p) o
     WHERE o.rno >= o.mx
    
  • Weighted average end days: how the sum in different currencies?

    Hello

    I have to introduce a new column named "averages weighted days late" in one of the models.

    It was easily handled by the code in the Oracle report: Aging Ar: calculated as <? If: SUM_PAYMENT < = 0? > 0 <? end if? > <? If: SUM_PAYMENT > 0? > <? SUM_W_DAYS_LATE div SUM_PAYMENT? > <? end if? > UX f

    The customer wants that this weighted means against the different currencies for a single client: How do I implement that?

    Example: Client A - average weighted days late:-16-20 in USD and EUR. = > report displays only the first currency - in this case EUR = >-16 (which is not what is expected)

    Logic: I need to run (-16) +(-20)/2. The logic is simple. However, where should I create this change? Also, this change must be connected to the period during which the report is run for / just for the money? I guess it's related at the same time.

    Thanks in advance

    and then divide it by the number of transactions.

    That's your logic? If yes then ok

    I don't know full logic

    And this must be done in the sql used in the xml file, I guess. Is there an easier option to calculate the end of days average weighted by a modification of the model code? Curious.

    If you already have some fares in the xml file to recalculate, then you can do so by model

    If you do not have the rate, then you can add it to xml format and then calculate by model

    or you can calculate in sql (as an example of data model) and then only show models

    How will be used it is your decision

  • SQL - how the sum on specific lines

    Hello

    Forever, the line, I want to show MySQL for previous lines, only when the previous values are equal = 1,.

    and only for successive lines.

    for example, the result expected on the column 'result '.

    Result of ID value

    100 1-

    130 1 2-> (current 1 + pre 1)

    3 500-

    1 600-

    2-1-700-> (current 1 + pre 1)

    1000-1-3-> (current 1 + precedent1 + 1 previous pre)

    4 1 1200

    How can do us it easily?

    I thank everyone in advance.

    Here's a way...

    with t (id, value) :)

    Select 100.1 in union double all the

    Select 130.1 Union double all the

    Select 500,3 Union double all the

    Select 600,1 Union double all the

    Select 700,1 Union double all the

    Select 1000,1 Union double all the

    Select double 1200,1

    )

    Select

    ID

    value

    case

    When start_of_group = 0 and = 1 value

    then MySQL over (partition by group_num by id order)

    Another null

    end result

    de)

    Select sum (start_of_group) (any order by id) as group_num

    start_of_group

    id

    value of)

    Select

    case

    When lag(value,1,0) (any order by id)! = 1 then 1

    0 otherwise

    end as start_of_group

    id

    value

    t

    )

    )

    order by id

    ;

    Kind regards

    Bob

  • Getting counties and divide by the sum in a single Pivot query

    Hello

    I have a pivot query that gives counties of all codes. I have also separated the number with the total in the same query.
    e.g
    
      col1          col2                   col3
    error_1       05-Jun-2012       parts
    error_1      05_june_2012     parts
    error_1      04_june_2012     consumables
    error_2      04_june_2012     consumables
    error_3      03_june_2012     parts
    .
    .
    .
    Now, the output should have the counts / divided into sum multiplied by 100. Basically, the percentage.


    output will be something like
    error                     June-2012                                 May-2012                              Feb-2012 ....      headers
    error_1                    60%                                          0 %                                         0%
    error_2                    20%                                          0                                             0%
    error_3                    20%                                           0%                                         0%
    Any suggestions please?


    Thank you
    Sun
    with
    t1 as
    (select 'error_1' col1,trunc(sysdate) - 1 col2 from dual union all
     select 'error_1',trunc(sysdate) - 1 from dual union all
     select 'error_1',trunc(sysdate) + 30 from dual union all
     select 'error_2',trunc(sysdate) + 31 from dual union all
     select 'error_3',trunc(sysdate) - 2 from dual union all
     select 'error_3',trunc(sysdate) + 30 from dual union all
     select 'error_1',trunc(sysdate) - 3 from dual union all
     select 'error_2',trunc(sysdate) - 2 from dual union all
     select 'error_3',trunc(sysdate) + 30 from dual union all
     select 'error_4',trunc(sysdate) - 6 from dual
    )
    select col1,
           count(case to_char(col2,'mon') when 'jul' then 1 end) one,
           100 * ratio_to_report(count(case to_char(col2,'mon') when 'jul' then 1 end)) over () x,
           count(case to_char(col2,'mon') when 'jun' then 1 end) two,
           100 * ratio_to_report(count(case to_char(col2,'mon') when 'jun' then 1 end)) over () y
      from t1
     group by col1
     order by col1
    

    Concerning

    Etbin

    Edited by: Etbin on 8.6.2012 14:15
    used month next to keep English and slovene "LUN" same ;)

  • How the sum of these two tables?

    Hello

    I have two tables

    DESC ACTIVITE_EXCEP_FAITE

    NUMBER OF FICHE_ID

    NUMBER OF SERVICES_ID

    NUMBER OF ACTIVITES_EXCEPTIONNELLES_ID

    NUMBER OF TIME

    NUMBER OF ACTIVITE_EXCEP_FAITE_ID

    DESC ACTIVITE_FAITE

    NUMBER OF FICHE_ID

    NUMBER OF ACTIVITES_ID

    NUMBER OF SECTEURS_ID

    LENGTH NUMBER (8.2)

    NUMBER OF ACTIVITE_FAITE_ID

    NUMBER OF TYPE_ACTIVITE_ID

    I want to add the column to the DURATION of the two tables when they get the same FICHE_ID. FICHE_ID is not a join field.

    I've tried this application, but it gives result when two recordings of the two table share the same number FICHE_ID.

    SELECT AF. FICHE_ID, SUM (NVL (AF. LENGTH, 0)) + SUM (NVL (AEF. LENGTH, 0))

    OF AF, ACTIVITE_EXCEP_FAITE AEF ACTIVITE_FAITE

    WHERE AF. FICHE_ID = AEF. FICHE_ID

    GROUP BY AF. FICHE_ID;

    Can you help me write this selection?

    Thank you very much for your help!

    Like this?

    select nvl(af.fiche_id, aef.fiche_id) fiche_id
         , sum(nvl(af.duree,0)) + sum(nvl(aef.duree,0)) duree
      from activite_faite af
      full join
           activite_excep_faite aef
        on af.fiche_id = aef.fiche_id
     group by nvl(af.fiche_id, aef.fiche_id);
    
  • Change the label after you check the sum of calculation for the column

    Hi all

    I have a crosstab report and I checked the 'sum of calculation' for each column and the total at the bottom. The label is 'full report '. How to change this label?

    Thanks in advance!

    On the attributes page of the report, in the region to break the formatting, you can put your text into the field "Display this text when printing of money to report".

  • Get the sum and lastvalue in one request

    I have this table

    ID

    type

    Balance

    1

    one

    100

    2

    b

    450

    3

    b

    600

    4

    one

    200

    5

    b

    50

    And I need the sum and the last value of the column "balance." How can I me below the result with a single query, without nested query?

    sum

    last

    one

    300

    200

    b

    1100

    50

    Try this:

    Select a separate type,

    Sum (balance) over (partition by type).

    LAST_VALUE (balance) over (partition by RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING id type order)

    from my_table;

    Concerning

    Thomas

Maybe you are looking for