Add years to query

Hi all,
I have a function collecting years from another table and creating a dynamic sql statement to included all years.
Also I set some variables in the where statement. At the end of the function I use execute immediate.
The issue with this method is time, it's taking almost double the time using execute immediate.
I can provide some data sample if you like.
Thanks in advanced.
Best,

Here is my code:
create table general(
general_id number,
general_type nvarchar2(10),
start_year number,
end_year number,
CONSTRAINT "PK_GENERAL" PRIMARY KEY (general_id)
insert into general(general_id, general_type, start_year, end_year)
values (1, 'data 1', 2009, 2012);
insert into general(general_id, general_type, start_year, end_year)
values (2, 'data 2', 2011, 2015);
create table activity(
activity_id number,
general_id number,
customer_id number,
fy number,
value1 number,
CONSTRAINT "PK_ACTIVITY" PRIMARY KEY (activity_id)
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(1,1,1,2009,10);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(2,1,1,2010,20);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(3,1,1,2011,30);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(4,1,1,2012,40);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(5,1,2,2009,50);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(6,1,2,2010,60);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(7,1,2,2011,70);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(8,1,2,2012,80);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(9,2,1,2011,90);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(10,2,1,2012,100);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(11,2,1,2013,110);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(12,2,1,2014,120);
insert into activity(activity_id,general_id,customer_id,fy,value1)
values(13,2,1,2015,130);
create table rates(
rate_id number,
customer_id number,
fy number,
amount number,
CONSTRAINT "PK_RATES" PRIMARY KEY (rate_id)
insert into rates(rate_id,customer_id, fy, amount)
values(1,1,2009, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(2,1,2010, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(3,1,2011, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(4,1,2012, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(5,1,2013, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(6,1,2014, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(7,1,2015, 0.23);
insert into rates(rate_id,customer_id, fy, amount)
values(8,2,2009, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(9,2,2010, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(10,2,2011, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(11,2,2012, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(12,2,2013, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(13,2,2014, 0.41);
insert into rates(rate_id,customer_id, fy, amount)
values(14,2,2015, 0.41);Here is my select statement:
select
NVL(SUM(CASE WHEN rnk = 1 then nvl(totalAmount, 0) else 0 end),0) year_1, --How to do this line dynamic for all years?
NVL(SUM(CASE WHEN rnk = 2 then nvl(totalAmount, 0) else 0 end),0) year_2,
NVL(SUM(CASE WHEN rnk = 3 then nvl(totalAmount, 0) else 0 end),0) year_3,
NVL(SUM(CASE WHEN rnk = 4 then nvl(totalAmount, 0) else 0 end),0) year_4,
NVL(SUM(CASE WHEN rnk = 5 then nvl(totalAmount, 0) else 0 end),0) year_5,
NVL(SUM(CASE WHEN rnk = 6 then nvl(totalAmount, 0) else 0 end),0) year_6
from (
   select a.fy, a.value1, r.amount, a.value1 * r.amount as totalAmount,
   dense_rank() over (partition by a.general_id order by a.fy ) rnk
   from activity a
   join rates r on r.customer_id = a.customer_id and r.fy = a.fy
   join general g on g.general_id = a.general_id
   where a.general_id = 1
   and a.fy between g.start_year and g.end_year
)Result:
        YEAR_1     YEAR_2     YEAR_3     YEAR_4     YEAR_5     YEAR_6
     22.8              29.2              35.6                42            0             0I hope this help to clarify my issue.
Thanks!
Edited by: user9542267 on Oct 19, 2012 1:20 PM

Similar Messages

  • Get Current year in Query Designer

    Dear all
    I don't know how to determine current year in Query Designer.
    I have a report, i want to see some indexs in bance sheet and income statement in current year and previous year. but, I can only assign with the fixed year (ex, 2010), this is not flexible. I want to get current year on system and calculate previous year.
    Please help me solve this problem
    Thanks

    Hi,
    You can use the SAP Exit variable 0CALYEAR for current year. For Previous year you can offset this by -1.
    You can refer this link for more Information,
    http://help.sap.com/saphelp_bw33/helpdata/en/3f/89533e5ff4d064e10000000a114084/content.htm
    Regards,
    Hari.
    Edited by: Hari Krishnan K on Jul 20, 2010 10:36 AM

  • How to unlock the request for a report and add the same query to new reques

    hi,
         how to unlock the request for a  and add the same query to new reques

    You can unlock in SE03 tcode.
    Goto tcode SE01, give the transport number --> display --> double click on the transport --> in the next screen select all the elements --> delete --> save.
    To attach it to another transport, In RSA1, click on transport connection> Choose Object types> query elements --> here you can find your query/ or you can search, which you can drag to right and attach to the transport (using truck button).

  • Function Add a combined query

    Hello,
    I created two different universes based on a SAP BEx query. I can create reports with WebI based on both universes thats no problem.
    If I want to create a combined query based on that 2 universes it is not possible the button "Add a combined query" is not available. Does anybody know when this will be available? Or am I doing something wrong?
    Thanks for your reply.
    Kind regards,
    Jule
    System Information: BIP 4.0 SP 02 Patch 11

    Ok, could you please try the following:
    1. Import the universe which you want to use.
    2. Go to File>Parameters>SQL tab.
    3. Check the checkbox for "Allow use of union,intersect and minus operator".
    4. Save and export that universe.
    Then if the problem persists, could you please test if the issue occurs with the administrator account?
    Kind Regards,

  • Add Year

    Hi,
    I have a query where I am converting a date to a year field and adding 1 onto the year:
    to_char(s.active_month, 'YYYY')+1
    active_month is in date format DD-MON-YYYY
    I get an error message:
    ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
    00932. 00000 - "inconsistent datatypes: expected %s got %s"
    Can anyone help with converting this field to a number?
    Thanks
    Edited by: sliderrules on 15-Nov-2010 08:56

    Hi,
    If s.active_month is a DATE (which is the only reasonable way to store data that represents a point in time), then the way to get a DATE exactly one year after it is:
    ADD_MONTHS ( s.active_month
            , 12
            )If you want a NUMBER that reflects the year of that date, then use EXTRACT:
    EXTRACT ( YEAR FROM  ADD_MONTHS ( s.active_month
                                    , 12
            )or, more simply:
    EXTRACT (YEAR FROM  s.active_month) + 1You could also use TO_CHAR to convert the DATE to a VARCHAR2, then use TO_NUMBER to convert that VARCHAR2 to a NUMBER, and then add 1.
    If s.active_month is not a DATE, then convert it to a DATE, then convert it to a DATE or a NUMBER.
    If you'd like help, post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    Sometimes, when you use one datatype in a place where another is required, Oracle will automatically convert the type for you. The problem you posted is one case where Oracle is not doing such an iomplicit conversion for you, but even if it were, it is not a good idea to rely on implicity conversions. Always use explict functions (like EXTRACT or TO_NUMBER) to convert from one data type to another when you need to.

  • Matrix table add year over year variance

    I am trying to get add a variance column to a matrix report is ssrs 2008 r2. However I am unable to create the custom code needed. I have been working with many examples online but none seem to fit what I am trying to do. Any help would be great. I am trying
    to calculate the diff at a month level year over year. Like month 1 2013 – month 1 2014
    I have a matrix table with three column grouping
    Quarter
    Month
    Year
    Design view of table
    Quarter
    Month
    Year
    Customer
    Job Location
    Job id
    Example of what I need (Variance)
    1
    1
    variance
    1
    1
    variance
    1
    1
    2
    2
    2013
    2014
    2013
    2014
    Acme
    Acme Philadelphia
    jj123
    100
    150
    50
    150
    100
    -50
    Dataset
    select
    Fisc_year
    ,(case
    when
    datepart(month,dt)
    = 11 then 1
    when
    datepart(month,dt)
    = 12 then 2
    when
    datepart(month,dt)
    = 1 then 3
    when
    datepart(month,dt)
    = 2 then 4
    when
    datepart(month,dt)
    = 3 then 5
    when
    datepart(month,dt)
    = 4 then 6
    when
    datepart(month,dt)
    = 5 then 7
    when
    datepart(month,dt)
    = 6 then 8
    when
    datepart(month,dt)
    = 7 then 9
    when
    datepart(month,dt)
    = 8 then 10
    when
    datepart(month,dt)
    = 9 then 11
    when
    datepart(month,dt)
    = 10 then 12
    end)
    as Mnth
    ,fisc_qrtr
    as Qrt
    ,Customer_Name
    ,Job_Name
    ,sum(REVENUE)
    as REVENUE
    from tbl_whatever
    where Customer_name
    = @Customername
    and Fisc_year
    = @Fyear

    Hi SQLtl,
    To achieve your requirement, we can directly use expression. For more details, please refer to the following steps:
    Right-click the column contains Year field to insert a column with Outside Group-Right.
    Then type the following expression to the corresponding cells:
    =Sum(iif(Fields!Year.Value="2014",Fields!REVENUE.value,0))-Sum(iif(Fields!Year.Value="2013",Fields!REVENUE.value,0))
    Besides, we can also use custom code to achieve the same goal, please see:
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5f44a0b5-144b-4e6e-b81e-d9b025427748/how-to-get-the-difference-between-two-columns-in-a-column-group?forum=sqlreportingservices
    If there are any other questions, please feel free to ask.
    Thanks,
    Katherine Xiong
    If you have any feedback on our support, please click
    here.
    Katherine Xiong
    TechNet Community Support

  • How to calculate first date and end date of this year in Query

    Hi Expert
    I want to calculate following dates with inputted date in query.
    I have no knowledge on ABAP and I just can create a query with simple logic.
    Could you teach me how to calculate following date?
    (If inputted date is April 12 2009)
    - First date of this year (Ex January 1 2009)
    - End date  of this year (Ex December 31 2009)
    - First date of last year (Ex January 1 2008)
    - End date of last year  (Ex December 31 2008)
    - First date of this month (Ex April 1 2009)
    Thank you!
    Take

    HR_JP_MONTH_BEGIN_END_DATE   use this FM to get the begin and end date of the Date you are passing. eg when u give input as 04/12/2009, it will return
    04/01/2009 as begin date
    04/30/2009 as end date
    pass the Year in this FM HR_E_GET_FISC_YEAR_DATES, you will get the Fiscal year Begin and End Dates
    Abh

  • Can we add button in query region  along with go and clear

    Hi Friends,
    i have a requirement as below steps-
    1)i have developed search pgae by using query regiion
    2) in pgae,first we have search items,go and clear(submit buttons), table region.
    3)here go and clear buttons came automatically.
    4) i can able to add button in front of the page and end of the page.
    4) my requirement is i need to add one more button along with go and clear(here go and clear button are in between search items and table region) .
    5)i am not able to add button along with go and clear.
    Can any one know how to do this--
    Thanks in Advance
    vamshi

    Hi Vamshi,
    You can not create extra buttons in Standard Query region.
    Alternatively you can create your custom region for the search and there you can add a button. If you don't need advanced region, you can create the custom region in place of standard one.
    Anoop

  • Field add in abap-query

    Hai all,
    Kindly advise me how to add the new fields in report which has already created via ABAP-Query programming.
    Regards,
    Renuga.A

    What kind of report? Usually, you have to add the field in the table declaration and then also in the SQL query. After that, include the field in what kind of report you are using.

  • Add "cost center" query to a start condition?

    Hi there,
    we got a new requirement for one of our plants.
    We're on SRM 5.0 classic scenario.
    Is it possible to add a "cost center" query to a specific start condition (SWB_PROCUREMENT) of a workflow?
    E.g. if a user uses cost center 4711 for a shopping cart item a specific cost center responsible xyz should approve this item.
    If the user uses another cost center 4712 for a second item in this shopping cart this item should be approved by another cost center responsible abc.
    Is that somehow possible ?
    So far I did not find a suitable expression for cost center.
    Thanks in advance for your answers.
    Best regards,
    Henning

    Hi Masa,
    thanks for your answer. Perhaps you also have a hint for the following:
    I can't really find in the metioned thread or in note 731637 what happens if a SC with several items is partially approved.
    Example:
    SC with 3 items:
    item 1 cc 1000
    item 2 cc 2000
    item 3 cc 1000
    Let's say item 1+3 have been approved by the approver found by badi and WS14500015. Is a PO or a purchase requisition created in backend? Or is it only created after the whole SC has been approved (i.e. also item 2).
    Thanks for a hint and best regards,
    Henning

  • Add calculated field (Query - SQ01)

    Hi to all,
    I would know how can i add a calculated field to a Query (SQ01) ?
    Can you give me an example of abap code to insert in CODING section ?
    thks

    Hi,
    My understanding about your question is you want to display the total for two standard fields in a table,
    1) Personalize the table and make true for the property Total.
    2) If you want to calculate both the column total,
    Add a MessageStyledText via personalziation to the page, and extend the controller
    and in the ProcessRequest
    Get the handle of the AM and then ur attributes VO Attributes and traverse through the VO rows and get the total of both the columns
    and set the value to the newly created MessageStyledText.
    Let me you want any sample code for the second step.
    Thanks.
    With Regards,
    Kali.
    OSSi.

  • Error of ADD ON and Query Saving

    Hi
    Even after uploading the licence i am not able to register ADD ON as well as i cant save query.
    When i register ADD ON i receive error : wrong digital signature for addon installer
    When i save query i receive error:define numbering series in administration module
    Though i have allot licence to SAP ADD ONs and superuser.PL is 0.
    Whats the solution?

    hi,
        you only need to remove the traces from the sari table if u r getting this error on server also.for client system go to the drive in which sap is installed...open prog file..sap b1...then u will find 2 addon local registration file and a folder called addon..remove ur addon from that folder and in the file search for the string where ur addon was installed and remove it......dont try it if u r nt comfortable.......
    much easier way is to format the client system and then installing the addon again.......
    Thanks
    Abhishek

  • Error WLS 10901 - when I add prompt in query from SAP in QAAWS

    Hello,
    I am accessing a Universe who's source is SAP -BEX Query. When I add a charecteristics and key figure I get data, however when I add a prompt or filter condition to my query in QAAWS - I get an error. I tried the same in Live Office and get the same error when I add the prompt  in the query.
    Error: WLS 10901 - getdocument information- A database error occurred - Error in MDData - Get_Leading_Column_data(see long text) --
    Why does this happen when I add a prompt to the query?

    HI Abhi,
    The Issue seem to be not from BO Universe. Can you please go and check the data at Bex report and to apply same condition over there.
    This might help us to track where exactly the error is happening.
    Since the flow is from Cubes to Bex Queries to Universe to QaaWS or Weni Reports.
    So its always better to track the data flow from source. In our scenario its Bex.
    Hope this may help you now and in future.
    Regards,
    AnjaniKumar C.A.

  • How to add selection in Query

    How to add additional fields created  to selection screen too ?
    I have created some additional fields in the infoset . but when i am trying to create a  Query the fields are only appearing as  output fields ...
    normally the fields should appear both as a selection and output field .
    thanks in advance ..
    Naval Bhatt

    Hi,
    Create additional fields in infoset, assign those fields to field group then generate infoset. Change corresponding query where you will see the additional fields and in selection fields window select the additional fields.
    Cheers.
    ...Reward if useful

  • Add table in query.

    Hello experts ,
    I have query with 1 table , if i want to add one more table to my query  and make a join with 2 tables or more than 2 , how can i do this ?
    Thanks for your help,
    Avi.

    hi.
    select kna1~kunnr kna1~sortl zsdo~asmid pa0001~ename
           zsdo~pc_p01_begda zsdo~pc_p01_ded_per
            into corresponding fields of table it_samptb_all
            from zsdo
            inner join kna1 on ( zsdo~kunnr = kna1~kunnr )
            inner join pa0001 on ( zsdo~asmid = pa0001~pernr )
        where zsdo~samp_flag eq 'X'     " Sampling is Active
          and zsdo~ostact = '1'         " Active Outlets only
          and zsdo~pc_p01_cshare ne '0'
          and pa0001~endda eq '99991231'
          and zsdo~ssok in ssok1
          and zsdo~kunnr in kunnr1.

Maybe you are looking for

  • Logging in a custom file (Web Dynpro Java Version: 7.0.11)

    Hi all, Is it possible to redirect the logging (no matther by Category or Location) to be written in some custom log file, not to the one big general log? And if so, please hint me how to do it. I will be very grateful if you don't send me links but

  • AAAGGH! can't copy any files to back-up so I can upgrade so I can copy ...

    I have recently finally gone to OS10.4.9 on a new machine. But I still have my old 9.2.2 G3 imac. I recently inherited a copy of OS10.3.5 on CDs so that I can FINALLY upgrade the imac and not be running into any of these cross platform little problem

  • Cannot attach documents at order operation level in PM order.

    Hi, We have recently upgraded to EHP6 and I have activated the EAM business functions including LOG_EAM_CI_6. In my PM work order, on the Additional Data tab, I can see the "Document Assignment" section at the bottom. Here I attach an existing docume

  • 11/14/2012 AIR 3.5 Runtime and SDK Beta Update

    We're pleased to announce the availability of AIR beta 3.5.0.690. You can download the BETA here: http://labs.adobe.com/downloads/air3-5.html This beta contains the following fixes: Minor improvements in security and stability. Minor improvements in

  • Final Cut HD-Print to video problem

    Hello brainy people and Help! I am experiencing problems with the Print to Video function. The mac switches the machine on and puts the canon into REC mode, the tape goes round and it records. However on playback there is no image. I have tried tweak