Calculate total from a Union query

Hi
I want to UNION 3 query and need to create a total column. Below My table
ID    Date           NoofVisit
1    4/21/2014    1
1    4/22/2014    1
1    4/23/2014    1
1    4/24/2014    1
Another query
ID    Date           NoofVisit
1    4/21/2014    2
1    4/24/2014    1
1    4/26/2014    1
Another query
ID    Date           NoofVisit
1    4/22/2014    2
1    4/26/2014    1
1    4/28/2014    1
My query is below
SELECT Id, Date, NoofVisit
FROM dbo.vw_MO_All_FirstVisit
UNION
SELECT Id, Date, NoofVisit
FROM vw_MO_All_SecondVisit
UNION
SELECT Id, Date, Noofvisit
FROM vw_MO_All_ThirdVisit
The result is
ID    Date           NoofVisit
1    4/21/2014    1
1    4/22/2014    1                      Its should be 3
1    4/23/2014    1
1    4/24/2014    1                     Its should be 2
1    4/26/2014    1                     Its should be 2
1    4/28/2014    1
Now what should the query is?
Please help.
Thanks in advance

You can also get this output using JOIN
SELECT COALESCE(t1.ID,t2.ID,t3.ID) AS ID,
COALESCE(t1.[Date],t2.[Date],t3.[Date]) AS [Date],
COALESCE(t1.NoOfVisit,0) + COALESCE(t2.NoOfVisit,0) + COALESCE(t3.NoOfVisit,0) AS TotalVisits
FROM dbo.vw_MO_All_FirstVisit t1
FULL JOIN dbo.vw_MO_All_SecondVisit t2
ON t2.ID = t1.ID
AND t2.[Date] = t1.[Date]
FULL JOIN dbo.vw_MO_All_ThirdVisit t3
ON t3.ID = t1.ID
AND t3.[Date] = t1.[Date]
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • Best Way to calculate totals from query

    Could someone point me in the right direction to add up my
    data and distinctly show it in my query?
    I have a table with the following fields:
    id, team_id, compname, teamname, totallost
    I want to add up the "totallost" row where the "team_id" and
    "compname" fields are the same...then show the compname with the
    sum of the totallost once in my table and determine who is
    winning.

    Thank you for the great help. This code works well, but is
    there a way to display the highest totallost and differentiate
    between competition names? My example is for one compname, but the
    table will have multiple compname's and I want to build a table
    showing only the highest totallost for each compname.
    You guys have been a great help. I learned something new
    today already.

  • Sub total from group by query?

    Hi,
    im using oracle 9i.
    Im getting results fine for the following kind of query,
       select a,b,c,sum(qty) d,sum(val) e from my_table group by a,b,c
       Now the output look like this
    a     b     c     d     e
    1     tt     rr     10     15
    1     tt     rr     10     25
    1     tt     rr     50     50
    1     tt     rr     10     20
    2     tt     rr     10     20
    2     tt     rr     20     40
    2     tt     rr     30     90
    2     tt     rr     40     80
    3     tt     rr     50     100
    3     tt     rr     60     180
    3     tt     rr     70     140but i want the output with subtotal based on column a. finally i want grant total. My output should b
    a     b     c     d     e
    1     tt     rr     10     15
    1     tt     rr     10     25
    1     tt     rr     50     50
    1     tt     rr     10     20
                   80     110
    2     tt     rr     10     20
    2     tt     rr     20     40
    2     tt     rr     30     90
    2     tt     rr     40     80
                   100     230
    3     tt     rr     50     100
    3     tt     rr     60     180
    3     tt     rr     70     140
                   180     420
                        760I tried with rollup,cube and grouping sets. But im not able to get exact result.
    Good help will be appreciated.
    Regards
    Sankar MN
    Edited by: SankarMCA on Oct 5, 2010 6:02 AM

    This may help you
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT empno,sal,SUM(sal) OVER (order by sal RANGE BETWEEN UNBOUNDED PRECEDING and CURRENT ROW) cumulative_sum
      2* FROM emp
    SQL> /
         EMPNO        SAL CUMULATIVE_SUM
          7369        800            800
          7900        950           1750
          7876       1100           2850
          7521       1250           5350
          7654       1250           5350
          7934       1300           6650
          7844       1500           8150
          7499       1600           9750
          7782       2450          12200
          7698       2850          15050
          7566       2975          18025
         EMPNO        SAL CUMULATIVE_SUM
          7788       3000          24025
          7902       3000          24025
          7839       5000          29025
    14 rows selected.

  • Navigating from a UNION Query

    I have a report (say Report1) where five queries are UNIONed together to give a result set. I have a navigation on one particular column (say Customer ID) for navigating to a different report (Report2) where Customer ID IS PROMPTED in Report2. But when I click on Customer ID = A11 in Report1, it takes me to Report2 but shows all rows whereas I expect only the rows corresponding to Customer ID A11. To verify the behavior, I created another report (Report3) wherein I had five UNION queries and this feature works fine in the new report. Report1 and Report3 are similar in design but Report3 is much smaller and simpler than Report1. Is there anything I can do to make this thing work in the report. It would take a lot of time recreate the report from scratch. Thanks.

    I figured it out. Navigation does not work in Pivot Table. So, if you click on a customer name in the Pivot Table view, and the customer name is "Is Prompted" in the target report, the filter value will not be applied in the target report.

  • Need help in restricting a result set from a UNION in MERGE

    Hello,
    Would really appreciate if anybody could help me out with the issue I am facing with the below statements (I am new to Oracle ):
    merge into table_name_1 p
    using
      select p_key, value_1, value_2
      from some_tables
      UNION
      select p_key, value_1, value_2
      from some_tables
      UNION
    )t
    on (p.p_key = t.p_key)
    when matched then
      update table_name_1 with value_1 and value_2
    when not matched then
      insert table_name_1 with p_key, value_1, value_2;
    Now, the union of all those selects gives me distinct values and it works most of the times but when I get values like below, the merge fails:
    p_key-----value_1-----value_2
    100-----25-----50
    100-----NULL-----50
    I browsed the net and understood the reason behind this: the result set becomes ambiguous and merge doesn't know which row to insert first and which one to update.
    Now, my requirement is: I could have any of the below scenario/result sets from the union and I need only 1 row per p_key -
    result_set_1
    p_key-----value_1-----value_2
    100-----25-----50 ***************need this row
    100-----NULL-----50
    100-----NULL-----NULL
    result_set_2
    p_key-----value_1-----value_2
    100-----25-----NULL ***************need this row
    100-----NULL-----NULL
    result_set_3
    p_key-----value_1-----value_2
    100-----25-----NULL ***************need this row (p_key = 100)
    100-----NULL-----NULL
    200-----NULL-----75 ***************need this row (p_key = 200)
    200-----NULL-----NULL
    300-----90-----95 ***************need this row (p_key = 300)
    So, I basically need a way to restrict the values that I will get from the UNION of all those selects to fit the requirement above, hope I was able to explain the issue I am facing.
    Any help would be greatly appreciated.
    Thanks,
    Dpunk

    In all cases the goal is to find an order by value that will make the row you want be first.
    The query I gave is calculating a priority for each row by adding up values showing whether each column is null or not null. The case statements check whether each column is null and need to be added up to give a total priority value.
    Value_1   Value_2   Priority
    Not Null  Not Null  2 + 1 = 3
    Not Null  Null      2 + 0 = 2
    Null      Not Null  0 + 1 = 1
    Null      Null      0 + 0 = 0
    The priority value ends up being a bitmap showing whether each value is null or not null. I think that reflects my mathematics background.
    Another way of getting the same result (suggested to me by your asking why it needs the "+") would be to use two CASE expressions as separate order by items:
    select p_key, value_1, value_2 from
    (select p_key, value_1, value_2, row_number() over
              (partition by p_key
               order by case when value_1 is null then 0 else 1 end DESC,
                        case when value_2 is null then 0 else 1 end DESC
              ) as rn
      from (your UNION query here)
    where rn = 1
    A third way is to use a more complex case statement:
    select p_key, value_1, value_2 from
    (select p_key, value_1, value_2, row_number() over
              (partition by p_key
               order by case when value_1 is NOT null and value_2 is NOT null then 1
                             when value_1 is NOT null and value_2 is     null then 2
                             when value_1 is     null and value_2 is NOT null then 3
                             when value_1 is     null and value_2 is     null then 4
                         end  ASC
              ) as rn
      from (your UNION query here)
    where rn = 1

  • Customised sub-total via UNION query - how to exclude from totals?

    Hi,
    I have a client requirement that a couple of companies have their own sub-total in a report, a treatment which applies to no other companies in their group.
    I have achieved this via a union all, but they also want a total of companies.
    Is there anyway to emulate a Grand Total, other than another union query such that the double count is not double counted?
    thanks for any ideas.
    Robert.

    only solution which i can think of is to create another union request for the grand total.. this way you wont add up your sub totals twice..

  • Query to get the difference between 2 totals from 2 different queries

    I wanted to know if it is possible to get the difference between 2 totals from 2 different queries. I would explain with an example:
    1st query  - Select sum(homepass) from table 1
    2nd query – select sum(homepass) from table2
    Is it possible to display the difference like-
    Select sum(homepass) from table 1 - sum(homepass) from table2
    I know the above query would gives syntax error but is there a better way or a trick to get the above task accomplished from a single query.
    I hope, my question is clear.
    Please revert with the reply to my query.
    Regards

    tomernitin wrote:
    Try this one also:
    WITH adtn1
    AS
    SELECT 110 val1 FROM dual UNION ALL
    SELECT 100 FROM dual UNION ALL
    SELECT 90  FROM dual UNION ALL
    SELECT 10 FROM dual
    adtn2
    AS
    SELECT 10 val2 FROM dual UNION ALL
    SELECT 20 FROM dual UNION ALL
    SELECT 30  FROM dual UNION ALL
    SELECT 100 FROM dual
    SELECT(Sum(a1.val1)-Sum(a2.val2)) Diff FROM adtn1 a1,adtn2 a2;
    let me know if you still have any doubt.
    Um.... I don't think so. Not with the cartesian join between a1 and 2.

  • Calculate total while designing query

    Hi All,
    I have a keyfigure purchase value . I am taking it in one column.
    Now I want to calculate total of purchase value for particular month.
    I can restrict time characteristic with variable but m not getting how to calculate total of this keyfigure.
    Please advise.
    Regards
    Shweta Goel

    what type of calculations?
    when you restrict a query by month you get the total in the result.
    if you want to use the purchase value for a month in a calculation, remove dates or order number from the query, have a aggregated result on which you can do the calculation and users can drill down if they need to see detail.
    regards
    Ramesh

  • Sql query to calculate total credit having more than one tables

    i have these 9 tables and want to calculate totalcredit from these tables alse want to
    See more: SQL-server-2008R2
    i have these 9 tables and want to calculate totalcredit from these tables alse want to calculate totaldebit of all account no in which each accountno will show its own total credit and total debit 
    parties
    purchase 
    purchase body
    purchase return
    purchase return body
    sale 
    sale body
    sale return
    sale return body

    If you want to suggest you accurate solution, please post CREATE TABLE+ INSERT INTO + sample data + DESIRED RESULT
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • SQL query to retrieve totals from "supporting Details"

    We've had a strange issue where the totals from the supporting details aren't adding up to the value submitted in essbase.
    In effect our RDMS is out of sync with Planning.
    In order to remedy this, I'm trying to build a SQL query to retrieve the value of the supporting details.
    Here is my Sql statement so far :
    SELECT
    HO1.OBJECT_NAME SCENARIO,
    HO2.OBJECT_NAME ACCOUNT,
    HO3.OBJECT_NAME ENTITY,
    HO4.OBJECT_NAME Month,
    HO5.OBJECT_NAME Version,
    HO6.OBJECT_NAME Currency,
    HO7.OBJECT_NAME Year,
    HO8.OBJECT_NAME Project,
    HO10.OBJECT_NAME CC,
    HO12.OBJECT_NAME Grplcl,
    HO13.OBJECT_NAME Product,
    HO15.OBJECT_NAME SalesRegion,
    HO16.OBJECT_NAME ContractAnalysis,
    HO17.OBJECT_NAME IC,
    SUM(NVL(HCDI.VALUE,0)) AMOUNT
    FROM hsp_column_detail HCD,
    hsp_column_detail_ITEM HCDI,
    HSP_OBJECT HO1,
    HSP_OBJECT HO2,
    HSP_OBJECT HO3,
    HSP_OBJECT HO4,
    HSP_OBJECT HO5,
    HSP_OBJECT HO6,
    HSP_OBJECT HO7,
    HSP_OBJECT HO8,
    HSP_OBJECT HO10,
    HSP_OBJECT HO12,
    HSP_OBJECT HO13,
    HSP_OBJECT HO15,
    HSP_OBJECT HO16,
    HSP_OBJECT HO17
    WHERE hcd.detail_id = hcdi.detail_id
    AND hcd.dim1 = ho1.object_id
    AND hcd.dim2 = ho2.object_id
    AND hcd.dim3 = ho3.object_id
    AND hcd.dim4 = ho4.object_id
    AND hcd.dim5 = ho5.object_id
    AND hcd.dim6 = ho6.object_id
    AND hcd.dim7 = ho7.object_id
    AND hcd.dim8 = ho8.object_id
    AND hcd.dim10 = ho10.object_id
    AND hcd.dim12 = ho12.object_id
    AND hcd.dim13 = ho13.object_id
    AND hcd.dim15 = ho15.object_id
    AND hcd.dim16 = ho16.object_id
    AND hcd.dim17 = ho17.object_id
    and hcdi.generation = 0
    GROUP BY HO1.OBJECT_NAME, HO2.OBJECT_NAME, HO3.OBJECT_NAME, HO4.OBJECT_NAME,
    HO5.OBJECT_NAME,
    HO6.OBJECT_NAME,
    HO7.OBJECT_NAME,
    HO8.OBJECT_NAME,
    HO10.OBJECT_NAME,
    HO12.OBJECT_NAME,
    HO13.OBJECT_NAME,
    HO15.OBJECT_NAME,
    HO16.OBJECT_NAME,
    HO17.OBJECT_NAME
    ORDER BY HO1.OBJECT_NAME, HO3.OBJECT_NAME, HO2.OBJECT_NAME

    This query works using PL SQL and has been used on Oracle 9.2.3.
    Cheers,
    Jeremie
    Here is the Query:
    set serveroutput on;
    set serveroutput on size 1000000;
    --set buffer_size 10000000;
    --set line_size 2000;
    --DBMS_OUTPUT.size = 2000000;
    DECLARE
    CURSOR BUDGET_HEADERS_CU IS
    SELECT
    hcd.detail_id,
    HO1.OBJECT_NAME SCENARIO,
    HO2.OBJECT_NAME ACCOUNT,
    HO3.OBJECT_NAME ENTITY,
    HO4.OBJECT_NAME Month,
    HO5.OBJECT_NAME Version,
    HO6.OBJECT_NAME Currency,
    HO7.OBJECT_NAME Year,
    HO8.OBJECT_NAME Project,
    HO10.OBJECT_NAME CC,
    HO12.OBJECT_NAME Grplcl,
    HO13.OBJECT_NAME Product,
    HO15.OBJECT_NAME SalesRegion,
    HO16.OBJECT_NAME ContractAnalysis,
    HO17.OBJECT_NAME IC,
    hcd.dim1 SCENARIO_id,
    hcd.dim2 ACCOUNT_id,
    hcd.dim3 ENTITY_id,
    hcd.dim4 Month_id,
    hcd.dim5 Version_id,
    hcd.dim6 Currency_ID,
    hcd.dim7 Year_ID,
    hcd.dim8 Project_ID,
    hcd.dim10 CC_ID,
    hcd.dim12 Grplcl_ID,
    hcd.dim13 Product_ID,
    hcd.dim15 SalesRegion_ID,
    hcd.dim16 ContractAnalysis_ID,
    hcd.dim17 IC_ID
    FROM hsp_column_detail HCD, /*hsp_column_detail_item HCDi,*/
    HSP_OBJECT HO1,
    HSP_OBJECT HO2,
    HSP_OBJECT HO3,
    HSP_OBJECT HO4,
    HSP_OBJECT HO5,
    HSP_OBJECT HO6,
    HSP_OBJECT HO7,
    HSP_OBJECT HO8,
    HSP_OBJECT HO10,
    HSP_OBJECT HO12,
    HSP_OBJECT HO13,
    HSP_OBJECT HO15,
    HSP_OBJECT HO16,
    HSP_OBJECT HO17
    WHERE /*hcd.detail_id = hcdi.detail_id
    AND*/ hcd.dim1 = ho1.object_id
    AND hcd.dim2 = ho2.object_id
    AND hcd.dim3 = ho3.object_id
    AND hcd.dim4 = ho4.object_id
    AND hcd.dim5 = ho5.object_id
    AND hcd.dim6 = ho6.object_id
    AND hcd.dim7 = ho7.object_id
    AND hcd.dim8 = ho8.object_id
    AND hcd.dim10 = ho10.object_id
    AND hcd.dim12 = ho12.object_id
    AND hcd.dim13 = ho13.object_id
    AND hcd.dim15 = ho15.object_id
    AND hcd.dim16 = ho16.object_id
    AND hcd.dim17 = ho17.object_id
    and HO5.OBJECT_NAME = 'Working'
    --and hcdi.generation != 0
    --and hcdi.label like 'JRTEST-%'
    --and hcd.detail_id = 253102
    /*GROUP BY HCD.Dim1, HCD.Dim2, HCD.Dim3, HCD.Dim4,
    HCD.Dim5,
    HCD.Dim6,
    HCD.Dim7,
    HCD.Dim8,
    HCD.Dim10,
    HCD.Dim12,
    HCD.Dim13,
    HCD.Dim15,
    HCD.Dim16,
    HCD.Dim17*/;
    CURSOR BUDGET_DETAILS_CU(
    /*scenario_p in varchar2,
    account_p in varchar2,
    entity_p in varchar2,
    month_p in varchar2,
    version_p in varchar2,
    currency_p in varchar2,
    year_p in varchar2,
    project_p in varchar2,
    cc_p in varchar2,
    grplcl_p in varchar2,
    product_p in varchar2,
    salesregion_p in varchar2,
    contractanalysis_p in varchar2,
    ic_p in varchar2*/
    detail_id_p in number
    IS
    SELECT
    /* HO1.OBJECT_NAME SCENARIO,
    HO2.OBJECT_NAME ACCOUNT,
    HO3.OBJECT_NAME ENTITY,
    HO4.OBJECT_NAME Month,
    HO5.OBJECT_NAME Version,
    HO6.OBJECT_NAME Currency,
    HO7.OBJECT_NAME Year,
    HO8.OBJECT_NAME Project,
    HO10.OBJECT_NAME CC,
    HO12.OBJECT_NAME Grplcl,
    HO13.OBJECT_NAME Product,
    HO15.OBJECT_NAME SalesRegion,
    HO16.OBJECT_NAME ContractAnalysis,
    HO17.OBJECT_NAME IC, */
    label,
    NVL(HCDI.VALUE,0) AMOUNT,
    HCDI.Position,
    HCDI.operator,
    HCDI.generation
    FROM /*hsp_column_detail HCD,*/
    hsp_column_detail_ITEM HCDI
    /* HSP_OBJECT HO1,
    HSP_OBJECT HO2,
    HSP_OBJECT HO3,
    HSP_OBJECT HO4,
    HSP_OBJECT HO5,
    HSP_OBJECT HO6,
    HSP_OBJECT HO7,
    HSP_OBJECT HO8,
    HSP_OBJECT HO10,
    HSP_OBJECT HO12,
    HSP_OBJECT HO13,
    HSP_OBJECT HO15,
    HSP_OBJECT HO16,
    HSP_OBJECT HO17*/
    --WHERE hcd.detail_id = hcdi.detail_id
    WHERE hcdi.detail_id = detail_id_p
    /*AND hcd.dim1 = ho1.object_id
    AND hcd.dim2 = ho2.object_id
    AND hcd.dim3 = ho3.object_id
    AND hcd.dim4 = ho4.object_id
    AND hcd.dim5 = ho5.object_id
    AND hcd.dim6 = ho6.object_id
    AND hcd.dim7 = ho7.object_id
    AND hcd.dim8 = ho8.object_id
    AND hcd.dim10 = ho10.object_id
    AND hcd.dim12 = ho12.object_id
    AND hcd.dim13 = ho13.object_id
    AND hcd.dim15 = ho15.object_id
    AND hcd.dim16 = ho16.object_id
    AND hcd.dim17 = ho17.object_id*/
    --and hcdi.generation != 0
    --and label like 'JRTEST-%'
    /*AND HCD.Dim1 = scenario_p
    AND HCD.Dim2 = account_p
    AND HCD.Dim3 = entity_p
    AND HCD.Dim4 = month_p
    AND HCD.Dim5 = version_p
    AND HCD.Dim6 = currency_p
    AND HCD.Dim7 = year_p
    AND HCD.Dim8 = project_p
    AND HCD.Dim10 = cc_p
    AND HCD.Dim12 = grplcl_p
    AND HCD.Dim13 = product_p
    AND HCD.Dim15 = salesregion_p
    AND HCD.Dim16 = contractanalysis_p
    AND HCD.Dim17 = ic_p*/
    ORDER BY hcdi.position;
    -- Variable Declaration
    running_total_ln number :=0;
    prev_running_total_ln number :=0;
    add_amount number :=0;
    multiply_amount number :=1;
    prev_generation number :=0;
    prev_add_amount number :=0;
    prev_multiply_amount number :=1;
    running_total_gen0 number :=0;
    running_gen0_op number :=0;
    running_total_gen1 number :=0;
    running_gen1_op number :=0;
    running_total_gen2 number :=0;
    running_gen2_op number :=0;
    running_total_gen3 number :=0;
    running_gen3_op number :=0;
    running_total_gen4 number :=0;
    running_gen4_op number :=0;
    running_total_gen5 number :=0;
    running_gen5_op number :=0;
    output_file utl_file.file_type;
    -- Begin PL/SQL processing
    BEGIN
    --DBMS_OUTPUT.
    DBMS_OUTPUT.ENABLE(1000000);
    DBMS_OUTPUT.PUT_LINE('Begin Processing');
    output_file := utl_file.fopen ('C:\temp', 'test.txt', 'W');
    -- Begin Header For Loop
    FOR BUDGET_HEADERS_CV IN BUDGET_HEADERS_CU
    LOOP
    --Reset Running Totals
    running_total_ln:=0;
    prev_running_total_ln:=0;
    add_amount:=0;
    multiply_amount:=1;
    prev_generation:=0;
    prev_add_amount:=0;
    prev_multiply_amount:=1;
    running_total_gen0:=0;
    running_gen0_op:=0;
    running_total_gen1:=0;
    running_gen1_op:=0;
    running_total_gen2:=0;
    running_gen2_op:=0;
    running_total_gen3:=0;
    running_gen3_op:=0;
    running_total_gen4:=0;
    running_gen4_op:=0;
    running_total_gen5:=0;
    running_gen5_op:=0;
    -- Begin Detail For Loop
    FOR BUDGET_DETAILS_CV IN BUDGET_DETAILS_CU(
    BUDGET_HEADERS_CV.Detail_ID
    /*BUDGET_HEADERS_CV.SCENARIO_ID,
    BUDGET_HEADERS_CV.ACCOUNT_ID,
    BUDGET_HEADERS_CV.ENTITY_ID,
    BUDGET_HEADERS_CV.Month_ID,
    BUDGET_HEADERS_CV.Version_ID,
    BUDGET_HEADERS_CV.Currency_ID,
    BUDGET_HEADERS_CV.Year_ID,
    BUDGET_HEADERS_CV.Project_ID,
    BUDGET_HEADERS_CV.CC_ID,
    BUDGET_HEADERS_CV.Grplcl_ID,
    BUDGET_HEADERS_CV.Product_ID,
    BUDGET_HEADERS_CV.SalesRegion_ID,
    BUDGET_HEADERS_CV.ContractAnalysis_ID,
    BUDGET_HEADERS_CV.IC_ID*/
    LOOP
    -- Null;
    add_amount :=0;
    multiply_amount :=1;
    IF BUDGET_DETAILS_CV.OPERATOR = 1 THEN
    add_amount := BUDGET_DETAILS_CV.Amount;
    End if;
    IF BUDGET_DETAILS_CV.OPERATOR = 2 THEN
    add_amount := BUDGET_DETAILS_CV.Amount * -1;
    End if;
    IF BUDGET_DETAILS_CV.OPERATOR = 3 THEN
    multiply_amount := BUDGET_DETAILS_CV.Amount;
    End if;
    IF BUDGET_DETAILS_CV.OPERATOR = 4 THEN
    multiply_amount := 1/BUDGET_DETAILS_CV.Amount;
    End if;
    IF BUDGET_DETAILS_CV.Position = 0 then
    running_total_gen0 := add_amount*multiply_amount;-- we are dealing with the first line
    Else
    if BUDGET_DETAILS_CV.GENERATION = prev_generation then
    -- run whatever total we are on up
    if BUDGET_DETAILS_CV.GENERATION = 0 then
    running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 1 then
    running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 2 then
    running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 3 then
    running_total_gen3 := (running_total_gen3 + add_amount)*multiply_amount;
    end if;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = prev_generation +1 then
    -- we are going up a generation
    -- (we cannot go up to gen0
    if BUDGET_DETAILS_CV.GENERATION = 1 then
    running_total_gen1:=0;-- reset gen1 counter
    running_gen0_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
    running_total_gen0 := running_total_gen0/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 0 total
    running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 2 then
    running_total_gen2:=0;-- reset gen1 counter
    running_gen1_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
    running_total_gen1 := running_total_gen1/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 1 total
    running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 3 then
    running_total_gen3:=0;-- reset gen1 counter
    running_gen2_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
    running_total_gen2 := running_total_gen2/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 2 total
    running_total_gen3 := (running_total_gen3 + add_amount)*multiply_amount;
    end if;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = prev_generation - 1 then
    -- we are Down a generation
    if BUDGET_DETAILS_CV.GENERATION = 0 then
    --we need to "add" back the previous gen 1 parent
    IF running_gen0_op = 1 THEN
    running_total_gen0 := running_total_gen0 + running_total_gen1;
    End if;
    IF running_gen0_op = 2 THEN
    running_total_gen0 := running_total_gen0 - running_total_gen1;
    End if;
    IF running_gen0_op = 3 THEN
    running_total_gen0 := running_total_gen0 * running_total_gen1;
    End if;
    IF running_gen0_op = 4 THEN
    running_total_gen0 := running_total_gen0 / running_total_gen1;
    End if;
    -- we need to add the current member to the gen0
    running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
    END IF;
    if BUDGET_DETAILS_CV.GENERATION = 1 then
    --we need to "add" back the previous gen 2 parent
    IF running_gen1_op = 1 THEN
    running_total_gen1 := running_total_gen1 + running_total_gen2;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen1 := running_total_gen1 - running_total_gen2;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen1 := running_total_gen1 * running_total_gen2;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen1 := running_total_gen1 / running_total_gen2;
    End if;
    -- we need to add the current member to the gen1
    running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = 2 then
    --we need to "add" back the previous gen 3 parent
    IF running_gen1_op = 1 THEN
    running_total_gen2 := running_total_gen2 + running_total_gen3;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen2 := running_total_gen2 - running_total_gen3;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen2 := running_total_gen2 * running_total_gen3;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen2 := running_total_gen2 / running_total_gen3;
    End if;
    -- we need to add the current member to the gen2
    running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
    end if;
    end if;
    if BUDGET_DETAILS_CV.GENERATION = prev_generation - 2 then
    -- we are Down 2 generations
    if BUDGET_DETAILS_CV.GENERATION = 0 then
    --we need to "add" back the previous gen 2 parent
    IF running_gen1_op = 1 THEN
    running_total_gen1 := running_total_gen1 + running_total_gen2;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen1 := running_total_gen1 - running_total_gen2;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen1 := running_total_gen1 * running_total_gen2;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen1 := running_total_gen1 / running_total_gen2;
    End if;
    --we need to "add" back the previous gen 1 parent
    IF running_gen0_op = 1 THEN
    running_total_gen0 := running_total_gen0 + running_total_gen1;
    End if;
    IF running_gen0_op = 2 THEN
    running_total_gen0 := running_total_gen0 - running_total_gen1;
    End if;
    IF running_gen0_op = 3 THEN
    running_total_gen0 := running_total_gen0 * running_total_gen1;
    End if;
    IF running_gen0_op = 4 THEN
    running_total_gen0 := running_total_gen0 / running_total_gen1;
    End if;
    -- we need to add the current member to the gen0
    running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
    END IF;
    if BUDGET_DETAILS_CV.GENERATION = 1 then
    --we need to "add" back the previous gen 3 parent
    IF running_gen1_op = 1 THEN
    running_total_gen2 := running_total_gen2 + running_total_gen3;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen2 := running_total_gen2 - running_total_gen3;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen2 := running_total_gen2 * running_total_gen3;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen2 := running_total_gen2 / running_total_gen3;
    End if;
    --we need to "add" back the previous gen 2 parent
    IF running_gen1_op = 1 THEN
    running_total_gen1 := running_total_gen1 + running_total_gen2;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen1 := running_total_gen1 - running_total_gen2;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen1 := running_total_gen1 * running_total_gen2;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen1 := running_total_gen1 / running_total_gen2;
    End if;
    -- we need to add the current member to the gen1
    running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
    END IF;
    if BUDGET_DETAILS_CV.GENERATION = prev_generation - 3 then
    -- we are Down 3 generations
    if BUDGET_DETAILS_CV.GENERATION = 0 then
    --we need to "add" back the previous gen 3 parent
    IF running_gen1_op = 1 THEN
    running_total_gen2 := running_total_gen2 + running_total_gen3;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen2 := running_total_gen2 - running_total_gen3;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen2 := running_total_gen2 * running_total_gen3;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen2 := running_total_gen2 / running_total_gen3;
    End if;
    --we need to "add" back the previous gen 2 parent
    IF running_gen1_op = 1 THEN
    running_total_gen1 := running_total_gen1 + running_total_gen2;
    End if;
    IF running_gen1_op = 2 THEN
    running_total_gen1 := running_total_gen1 - running_total_gen2;
    End if;
    IF running_gen1_op = 3 THEN
    running_total_gen1 := running_total_gen1 * running_total_gen2;
    End if;
    IF running_gen1_op = 4 THEN
    running_total_gen1 := running_total_gen1 / running_total_gen2;
    End if;
    --we need to "add" back the previous gen 1 parent
    IF running_gen0_op = 1 THEN
    running_total_gen0 := running_total_gen0 + running_total_gen1;
    End if;
    IF running_gen0_op = 2 THEN
    running_total_gen0 := running_total_gen0 - running_total_gen1;
    End if;
    IF running_gen0_op = 3 THEN
    running_total_gen0 := running_total_gen0 * running_total_gen1;
    End if;
    IF running_gen0_op = 4 THEN
    running_total_gen0 := running_total_gen0 / running_total_gen1;
    End if;
    -- we need to add the current member to the gen0
    running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
    END IF;
    end if;
    END IF;
    End IF;
    END LOOP; -- End of Detail Loop
    --DBMS_OUTPUT.PUT_LINE(BUDGET_HEADERS_CV.SCENARIO||';'||BUDGET_HEADERS_CV.ACCOUNT||';'||BUDGET_HEADERS_CV.ENTITY||';'||BUDGET_HEADERS_CV.Month||';'||BUDGET_HEADERS_CV.Version||';'||BUDGET_HEADERS_CV.Currency||';'||BUDGET_HEADERS_CV.Year||';'||BUDGET_HEADERS_CV.Project||';'||BUDGET_HEADERS_CV.CC||';'||BUDGET_HEADERS_CV.Grplcl||';'||BUDGET_HEADERS_CV.Product||';'||BUDGET_HEADERS_CV.SalesRegion||';'||BUDGET_HEADERS_CV.ContractAnalysis||';'||BUDGET_HEADERS_CV.IC||';'||running_total_gen0);
    utl_file.put_line(output_file,BUDGET_HEADERS_CV.SCENARIO||';'||BUDGET_HEADERS_CV.ACCOUNT||';'||BUDGET_HEADERS_CV.ENTITY||';'||BUDGET_HEADERS_CV.Month||';'||BUDGET_HEADERS_CV.Version||';'||BUDGET_HEADERS_CV.Currency||';'||BUDGET_HEADERS_CV.Year||';'||BUDGET_HEADERS_CV.Project||';'||BUDGET_HEADERS_CV.CC||';'||BUDGET_HEADERS_CV.Grplcl||';'||BUDGET_HEADERS_CV.Product||';'||BUDGET_HEADERS_CV.SalesRegion||';'||BUDGET_HEADERS_CV.ContractAnalysis||';'||BUDGET_HEADERS_CV.IC||';'||running_total_gen0);
    /*BUDGET_HEADERS_CV.SCENARIO, BUDGET_HEADERS_CV.ACCOUNT, BUDGET_HEADERS_CV.ENTITY, BUDGET_HEADERS_CV.Month, BUDGET_HEADERS_CV.Version,BUDGET_HEADERS_CV.Currency, BUDGET_HEADERS_CV.Year, BUDGET_HEADERS_CV.Project, BUDGET_HEADERS_CV.CC, BUDGET_HEADERS_CV.Grplcl, BUDGET_HEADERS_CV.Product, BUDGET_HEADERS_CV.SalesRegion, BUDGET_HEADERS_CV.ContractAnalysis, BUDGET_HEADERS_CV.IC */
    END LOOP; -- End of Header Loop
    --DBMS_OUTPUT.PUT_LINE('Total');
    -- DBMS_OUTPUT.PUT_LINE('End Processing');
    utl_file.fclose(output_file);
    END;
    Edited by: JeremieR on Apr 18, 2011 5:02 AM

  • How to Calculate Duration from Date Columns

    Hello,
    I would like to calculate duration from date columns. Using the following query
    SELECT DISTINCT
    EVENT_ID,
    To_Char(RU.START_TIME, 'HH24:MI') START_TIME,
    To_Char(RU.END_TIME, 'HH24:MI') END_TIME
    FROM ROOM_USAGE RU
    What I need is to subtract START_TIME from END_TIME and result should be in minutes.
    Many Thanks,

    But, i think you need to calculate the number of days if the days are not same -
    satyaki>
    satyaki>
    satyaki>
    satyaki>with ru
    2 as
    3 (
    4 select '05-JAN-2007 15:10:20' start_time, '07-JAN-2007 19:20:20' end_time from dual
    5 union all
    6 select '08-JAN-2007 05:10:20' start_time, '07-JAN-2007 07:07:20' end_time from dual
    7 )
    8 select ru.start_time, ru.end_time, SUBSTR(numtodsinterval(to_date(RU.END_TIME,'DD-MON-YYYY HH24:MI:SS')-to_date(RU.START_TIME,'DD-MON-YYYY HH24:MI:SS'), 'DAY'), 12, 8) duration
    9 from ru;
    START_TIME END_TIME DURATION
    05-JAN-2007 15:10:20 07-JAN-2007 19:20:20 04:09:59
    08-JAN-2007 05:10:20 07-JAN-2007 07:07:20 22:03:00
    satyaki>
    Or, Am i missing anything?
    Regards.
    Satyaki De.
    is it possible to to add the duration? making the total hours to 26:12 (twentysix hours and twelve minutes)

  • Sum of columns in union query

    hi, i am using oracle 10g database..
    how to get the sum of column in union query ?
         select * from (select 100 records from dual), (select 50 available from dual)
    union
    select * from (select 200 records from dual), (select 150 available from dual)
    display should be like
                     records         available
                      100               50
                      200               150
    total            300               200thanks ...

    Peter vd Zwan wrote:
    try this:Grouping by records will not produce correct results:
    SQL> with a as
      2  (
      3  select * from (select 100 records from dual), (select 50 available from dual)
      4  union
      5  select * from (select 100 records from dual), (select 100 available from dual)
      6  union
      7  select * from (select 200 records from dual), (select 150 available from dual)
      8  )
      9  select
    10    case when grouping(records) = 0 then null else 'total' end tot
    11    ,sum(records)   records
    12    ,sum(available) available
    13  from
    14    a
    15  group by
    16  rollup (records)
    17  ;
    TOT      RECORDS  AVAILABLE
                 200        150
                 200        150
    total        400        300
    SQL> select  case grouping(rownum)
      2     when 1 then 'Total'
      3   end display,
      4          sum(records) records,
      5          sum(available) available
      6    from  (
      7            select * from (select 100 records from dual), (select 50 available from dual)
      8           union
      9            select * from (select 100 records from dual), (select 100 available from dual)
    10           union
    11            select * from (select 200 records from dual), (select 150 available from dual)
    12          )
    13    group by rollup(rownum)
    14  /
    DISPL    RECORDS  AVAILABLE
                 100         50
                 100        100
                 200        150
    Total        400        300
    SQL> SY.

  • You Can't Calculate Totals on that!...SO #@!%#! WHAT, YOU GREAT PILE OF !@#

    In Access you have an asterix which represents every field in the table. Useful if you want to see all the data in a table for a single record. I've done it a million times before.
    So why is it telling me that I can't calculate totals on the *, so I should remove it from the query grid and replace it with the fields I want and calculate totals then.
    I DON'T WANT TO CALCULATE ANY #!@%#@!! TOTALS!!
    I AM PUTTING THE FIELDS I #@!%$#! WANT - I WANT ALL OF THEM!! I JUST DON'T WANT TO DOUBLE CLICK 30 TIMES WHEN ONCE WILL DO!
    It's driving me insane because I'm going through hundreds of Oracle tables and it takes so long to browse to the record I'm looking for and it should work to use the asterix, I've done it so many times before.
    How do I override this utterly unhelpful behaviour? Am I stuck in some sort of "Queries will only calculate Totals! mode that I don't know about?
    I know, I know, Access is a micro$loth invention, not Oracle, but we have to use an ODBC link to the SQL database via Acces and it's going to kill me.
    Unless I slam my PC into my neighbour's head first!
    Any help greatly apprciated.
    Sorry for the rant!
    Cheers
    IsNull

    IsNull wrote:
    Cheers,
    I am serious and I know this isn't an Access site, and I know there are differences between SQL in Access and SQL in Oracle, and I'm trying to get funding to do some proper SQL and VBA training, but if anyone can help I'd be very grateful.What? with the funding? Sorry, there's a credit crisis doncha know! ;)
    In the Access/ODBC/Oracle query design view you can create a query by linking fields in tables and then choosing fields you want to output in the query results. At the top of each table is an asterix which uses all the fields from the entire table in a query and outputs the data from all the fields. I'm looking through various tables to find useful fields and using one particular record as my example so I get consistent data output.
    Now, I can look in the actual linked tables and see what they contain, but I want the data for a single record, not the general table contents and there are hundreds of thousands of records. So I'm using the query design to search for the data for a single record and using only one table per query. But if I double click on the asterix to indicate "Use All Fields from this Tabel" I get the "But you can't calculate totals on that!" message. If I add them all individually I get an "Illegal use of LONG data".I once looked at Access, many moons ago. Didn't like it.
    However, from what you describe, it would seem that you need to somehow join the tables as you're doing, but also apply some restrictions on the data so that you're not trying to retieve all the rows back. As for it's error complaining about the totals, I would expect something like you've got an aggregate function in there and things aren't being grouped together. Either that or there's some "setting" somewhere that automatically tries to total things, which hopefully you could turn off. The best thing is to take the error message/number and lookup what that means via some website/documentation that lists Access errors and how to resolve them.
    You'll be lucky to find anyone here who will know (or care about) Access.

  • Calculate total and average for same key figure

    Hi Experts,
    I have a requirement where I need to calculate total and average for same key figure no of employees.
    eg:                
    If I enter 03,2009 as Input the reuslt should give from financiual year starting to current month.
         11.2008      12.2008     1.2009     2.2009     3.2009        average
             11          10       12       10        10         10.6
             10         10       11       12        10         10.6
    total         21         20       23       22        20          21.2
    we have only one characteristic in rows... companycode.
    Waiting for your Inputs.
    Regards
    Prasad Nannuri

    no it will work for u
    you have to use variable on 0calmonth or fiscal period depending on what Time characteristic u are using.
    lets say that variable is zcalmonth
    it is based on 0calmonth for e.g.
    now u restrict keyfigure with this variable zcalmonth with time char. = 0CALMONTH
    copy and paste the restricted keyfigure
    now set offset for variable in  variable selection screen dialog box = -1
    repeat this until u want
    make this variable mandatory
    now at query execution user will select any value for month/year
    and u will see all 5 months in result set
    now there can be maximum 12 months in a year, so u end up creating only 12 restricted keyfigures.
    use YEAR in restricted keyfigure too, and restrict it with YEAR VARIABLE processing by CUSTOMER EXIT = CURRENT YEAR
    in this case it will automatically removes any additional values...
    for e.e.g
    YEAR = 2008 only
    User entered 6/2008
    so lets say ur financial year starts in  april 2007 and ends in april 2008
    so u expect to see
    4/08
    5/08
    6/08
    but u created 12 restricted keyfigures , so it will show upto
    4,5,6 months only

  • Summary column in a union query....

    hi,
    i've a report. the datamodel is like ....
    select inv_total,inv_id from invoices
    union all
    select -pymt_total,pymt_id from payments
    union all
    select misc_total,misc_id from transactions
    where misc_code='D'
    union all
    select -misc_total,misc_id from transactions
    where misc_code='C'
    this will give me output for all the customers. i want to print total for
    inv_total+pymt_total+misc_total+misc_total which will reset at customer id.
    Can i create a summary column that will reset at customer id column? i'm using reports 3.0.8
    thanks
    ---himanshu

    1) Add customer_no to the SELECT as column3.
    2) Create 2 groups of the query: G1 with customer_no and G2 with the other 2 columns.
    3) Create a summary column (Use the toolbar on the left, the symbol has the Sigma sum symbol). Choose column 1 as the column to sum. Set it to re-set at G1 (I think default is Report).
    John Alexander www.SummitSoftwareDesign.com

Maybe you are looking for

  • Credit Management (Open Items and Oldest Open Items)

    I have 2 questions. 1)What is the difference between Open Items check and Oldest open items 2) I want to activate a Credit management check which should work in way that the Order will be blocked on the basis of Payment term check Let say if I am usi

  • Hard drive died.  How to sync my Ipod/music to new drive ??

    Hello, Sad day yesterday. Click click... HD died. Took to Mactown and They are putting in new one. They said that if I sync my iPod Classic with my new drive, all my music and audiobooks will be erased. Can anyone help me?

  • [FM2-A55M-E33] BETA UEFI BIOS [E7721AMS.Axx]

    E7721AMS.A12 Warning! BIOS update is risky, proceed at your own risks! For BIOS Update >>Use the MSI HQ Forum USB flasher<< *NOTE: Please unplug the power cord from PSU, or switch it off completely and >>Clear CMOS Guide<< after flashing.

  • I have 2 firefox menu icons, it tells me one is a ink file, what do I do with it?

    On my menu for desktop I have 2 icons for Mozilla Firefox. I thought one might be a short cut or just a duplicate. I have been afraid to delete or uninstall one, but it has bother me for some time, which one I should use. They seem to be the same. Ho

  • Can't iPhoto '11 export video?

    I have videos taken from my iPhone that are in the new iPhoto that I just upgraded to... but iPhoto doesn't seem to export video to QT or anywhere else as it used to. Did Apple really make a new iPhoto version where you can't even get iPhone video ou