Advance Aggregation based on multiple conditions

Hi members,
I have a situation where I need to aggregate data based on multiple conditions. Relevant details of the problem is as follows.
There is a table (let's call X). It has following columns:
Transaction_Time (date)
Transaction_direction (Possible values IN or OUT)
Column_1
Column_2
Based on the columns: Transaction_direction, Column_1, Column_2, the type of the transaction will be derived. For example, if transaction_direction='IN' then transaction type is IN, if 'OUT' then transaction types are Out. Similarly if Column_1=Column_2 then transaction type is Txn_3 otherwise 4.
Based on date and transaction types the aggregation will happen.The sample output would be:
Time, Transaction type (IN, OUT, Txn_3, Txn_4), Sum of transactions
10-June-2013 00:00  IN Transactions  2500
10-June-2013 00:00  Txn_3 Transactions 3590
and so.
IN and Out transactions are easy to be derived using decode() function. However avoiding multiple UNION ALL and write a single SQL for all four conditions is tricky one.
Hope I clarified.
Neeraj

What version of Oracle are you using?
If you're on 11.x you can use the UNPIVOT feature as follows:
with t (Transaction_Time, Transaction_direction, Column_1, Column_2) as (
select date '2013-06-10', 'IN', 1, 1 from dual union all
select date '2013-06-10', 'IN', 2, 2 from dual union all
select date '2013-06-10', 'IN', 1, 2 from dual union all
select date '2013-06-10', 'IN', 3, 4 from dual union all
select date '2013-06-10', 'OUT', 3, 3 from dual union all
select date '2013-06-10', 'OUT', 3, 4 from dual
select * from (
select
  transaction_time
, sum(case when transaction_direction = 'IN' then 1 end)  as IN_count
, sum(case when transaction_direction = 'OUT' then 1 end) as OUT_count
, sum(case when Column_1 = Column_2 then 1 end)           as Txn_3_count
, sum(case when Column_1 != Column_2 then 1 end)          as Txn_4_count
from t
group by transaction_time
unpivot (
  txn_count for transaction_type in (
     IN_count as 'IN'
  ,  OUT_count as 'OUT'
  ,  Txn_3_count as 'Txn_3'
  ,  Txn_4_count as 'Txn_4'
order by transaction_time, transaction_type
TRANSACTION_TIME TRANSACTION_TYPE TXN_COUNT
2013-06-10       IN               4      
2013-06-10       OUT              2     
2013-06-10       Txn_3            3      
2013-06-10       Txn_4            3      
If you're okay with getting one row per date with the 4 counts you can just use the inner select above, i.e.
select
  transaction_time
, sum(case when transaction_direction = 'IN' then 1 end)  as IN_count
, sum(case when transaction_direction = 'OUT' then 1 end) as OUT_count
, sum(case when Column_1 = Column_2 then 1 end)           as Txn_3_count
, sum(case when Column_1 != Column_2 then 1 end)          as Txn_4_count
from t
group by transaction_time
order by transaction_time
TRANSACTION_TIME IN_COUNT OUT_COUNT  TXN_3_COUNT  TXN_4_COUNT
2013-06-10       4        2          3            3      
If you want to sum transaction amounts then use the same logic, except in the case statements replace 1 with the column you want to sum.
Regards,
Bob

Similar Messages

  • Highligting rows based on multiple conditions

    Hi,
    I have to show a report with exceptions highlighted in RED color. Exceptions are based on some conditions:
    If I have four columns in an answer request, two of which say "Level" and "Status".
    based on the "Level" and "Status" column conditions I have to highlight entire row which satisfies any of the condition.
    Ex: If I have Level=3,4,5 and Status= Y,N then for
    Level=3 or Status=N is one exception.
    Level=4,5 or Status=Y is another exception.
    Based on this two column conditions I have to highlight entire row in the report.
    Can we do this in OBIEE. Highlighting rows based on multiple conditions?
    Thanks in advance!

    Thanks for the reply.
    I have tried that already. I can highlight one specific field but not entire row.
    If I keep on applying the conditional format for the other columns too then I can't apply the conditions.
    Anyways, I have created a dummy column with the conditions in it and then applied that to the other fields and used conditional format to highlight the color.

  • JDBC to IDOC Scenario - select data in jdbc based on multiple conditions

    Hello
         I have a JDBC to IDOC Scenario. I have to select the records in JDBC based on different conditions each time. For example I have to select based on company code '1000' and Employee claasification 'E1' and date range. After I post these records in SAP again I want to select other records for some other company code '2000' and different business area and different dates. Basically I want to extract data multiple times based on different conditions.
    Hiow do I achieve this?
    Another question is in the JDBC to IDOC scenario since the sender adapter is JDBC and the sender adapter polls depending on the duration of time ( say 60 secs ) in the adapter once after I extract the data based on a condition how do I control in such a way that the same data is not extracted again.
    Thanks
    Naga

    Hi Naga,
    I have to select the records in JDBC based on different conditions each time. For example I have to select based on company code '1000' and Employee claasification 'E1' and date range. After I post these records in SAP again I want to select other records for some other company code '2000' and different business area and different dates. Basically I want to extract data multiple times based on different conditions.
    -->
    Such requirements cant be handle through select query of the sender...but you can handle this in the message mapping area.....you can fire a select query in the database to pick up records in a batch of 10K (do not keep any condition on this except for sorting). After the records come into PI you can send the message to your target based on the unique combination of "Company code+ Employee clasification + date range" handling this in the message mapping.
    Another question is in the JDBC to IDOC scenario since the sender adapter is JDBC and the sender adapter polls depending on the duration of time ( say 60 secs ) in the adapter once after I extract the data based on a condition how do I control in such a way that the same data is not extracted again.
    You can use the N--> C logic
    The data records that you pick have a corresponding control table i assume. There should be a field STATUS where the initial status of record should be N.
    After you pick the records this status should be made C so that only those records present in the database with status = N are picked up.
    Mention the condition Status = N in the select query.
    Thanks
    Dhwani

  • Triggering BW Process Chain based on multiple conditions/events

    Hi all,
    We are facing a situation where we need to execute a BW process chain only if another process chain and two different R/3 jobs are completed.
    Could anyone give any inputs as to how to include multiple start conditions for a BW process chain.
    Thanks,
    Sarath

    we did the following for the same challenge :
    create a z table with a description field and a date field.
    eg
    job_1 20100829
    job_2 20100829
    job_3 20100829
    create one program that reads this table and inserts for every job an entry.
    if you already have two other entries in the table, this means the other two jobs have also finished to you can trigger the chain (check the forum on how to trigger a chain using abap).
    if you don't have the entries in the table insert the entry for this job.
    create one event (event_1) to trigger this program.
    at the end of the R/3 job/process chain raise event_1 in BW.

  • Deleting data from another table with multiple conditions

    Hi frnds
    I need to delete some data from a table based on multiple condition I tried following sql but its deleteing some rows which is not meeting the criteria which is really dangerours. When i trying = operator it returns ORa- 01427 single -row subquery returns more than one row
    delete from GL_TXNS
    where TRN_DT in (Select trn_Dt from GL_MAT)
    and BR in (select ac_branch from GL_MAT)
    and CODE in (select CODE T from GL_MAT)
    and (lcy_amt in (select lcy_amt from GL_MAT) or
    fcy_amt in(select fcy_amt from GL_MAT)
    rgds
    ramya

    My answer is the same as Avinash's but I will explain a little bit more.
    ORa- 01427 single -row subquery returns more than one rowmeans that you have a subquery that Oracle is expecting one value from that is returning multiple values. In your case you need one value for the equijoin ("=") and you are getting more than one value back. The error happens even if all the values are the same - multiple values being returned will cause the error.
    The solution is to either allow multiple values to be returned (say, use the IN condition istead of "=") or only return one value if possible (say, forcing one value by using DISTINCT, GROUP BY, or a WHERE clause condition of ROWNUM=1) - but these workarounds must be checked carefully to make sure they work correctkly

  • Multiple Condition Calculation - New to Numbers

    I am new to numbers and/or excel but what Im trying to do ended up being a lot harder than I thought it should be.
    Im trying to create a summary of items based on multiple conditions
    _ID TYPE # DATE_
    a000001 1 5 Jan 1, 2008
    b000002 7 2 Jan 2, 2008
    a000001 7 1 Jan 3, 2008
    a000001 1 6 Jan 4, 2008
    b000002 1 32 Jan 5, 2008
    c000003 7 6 Jan 6, 2008
    c000003 1 6 Jan 7, 2008
    If ((ID == a000001) AND (TYPE == 1)) then add the # column to the current total.
    OR
    If ((ID == a000001) AND (TYPE == 1) AND (DATE == Jan 1, 2008)) then add the # column to the current total.
    Does that make sense? Ive found may versions of conditional on this forum but I cant find any with running totals.
    Thanks all!
    null

    Bo,
    I'm going to give you a method that you can massage to your liking.
    The E-column may be hidden, and will contain your condition logic. Now E2 contains:
    =AND(A2="a000001", B2=1)
    Column A2 of the Formula Table contains:
    =SUMIF(DataTable :: E, TRUE, DataTable :: C)
    The first argument is the test range, your logic, the second argument is the valid condition for the test range, and the third argument is the range to be summed.
    Jerry

  • Writing in to multiple sheets in Excel file based on input condition

    Hi All Experts,
    i need to write in to multiple sheets in one Excel file output.
    is it possible with UTL_FILE ? or Any options there in Oracle to do this ?
    Can anyone please suggest me on this.
    Thanks,
    Ravi

    I have seen all of them they are all just simple SQL pulls from database in to Excel file..No, definitely not
    My requirement is i need to write in to one single Excel file with MULTIPLE sheets based on input condition-> [xml_spreadsheet|http://matzberger.de/oracle/spreadsheet-en.html]
    so for each parameter need to write into separate sheet in one Excel File..-> [xml_spreadsheet|http://matzberger.de/oracle/spreadsheet-en.html]
    I hope you understand my problem atleast now...I think I understood your problem at first sight.
    If you simply want to throw 2 queries at the package and get back an Excel file with 2 worksheets you just have to look at the first page of the [tutorial |http://matzberger.de/oracle/spreadsheet-tut-en.html]. Half way down the page there's an example.
    If you want a special formatting then you can do this too, it's described step by step.
    Regards
    Marcus

  • Single iDOc to multiple files based on a condition

    Hi Friends
    need your help in one scenario which is something like this:
    there is one iDOC as the sender and this iDOC has to be mapped to files based on a condition means let us say if there is a particular field "R" in iDOC(WPDBBYY01) than it should go and create the file "ABC"
    and if the particlar field is P or G or M than it should create another 2 files named "XYZ" and "WUV" .
    Please guide me how can i put this condition ?
    Any help would be rewarded with points
    Thanks

    Have a single receiver and 2 inbound interfaces.
    So you will have 2 different mappings. In the first mappng source is IDOC and the target is file ABC.
    In the second mappping the source is IDOC and the target is 2 files XYZ and WUV.
    In Interface determination, add both the inbound interfaces and put the condition for each interface.
    Regards,
    Jaishankar

  • Error in multiple condition type switch

    Hi,
    I define a multiple condition as a switch for different processing branches for my
    Workflow (all my attributes in the conditions are from the object).
    I defined the Other Values outcome also.
    When one of the conditions is true I received an error SWP103 "Error when starting a SWITCH branch"
    It works fine when my process go to "other" outcome.
    Thanks in Advance,
    Ronit.

    In my workflow the same error occured while using a mutliple condition with type Switch.
    A short explanation of my workflow:
    - container operation: errorcounter = errorcounter + 1
    - multiple condition
      1. errorcounter =< 4
      2. errorcounter = 5
      3. other values
    - activity
    - loop until (depending on outcome activity).
    The first time while executing the loop went perfect, but the second time it failed and based on the message I guess it failed on the multiple condition. What I also notices in the technical view was that the errorcounter wasn't updated.
    I guess the container operation and multiple condition bumped into eachother (somehow?) and caused the error.
    My solution:
    Put a wait stap between step 1 and 2 and it works fine.
    I'm not sure this case is applicable to you, but may be a hint in the right direction. Good luck!

  • Want to update data in a view based on multiple tables

    Hi
    I am facing a problem i want to update data in tables using a view. As that view is based on multiple tables so i am unable to update data. i came to know we can update table from view only if view is based on single table. so if anyone knows any alternative please let me know.
    Thanx
    Devinder

    Devinder,
    The table can be updated through a view based on multiple tables, if and only if the table is a "key preserved" table. Rather than explaining myself, i avoided the burden of typing by finding the material in Oracle Docs and pasting it for you :-)
    If you want a join view to be updatable, all of the following conditions must be
    true:
    1. The DML statement must affect only one table underlying the join.
    2. For an INSERT statement, the view must not be created WITH CHECK
    OPTION, and all columns into which values are inserted must come from a
    key-preserved table. A key-preserved table in one for which every primary
    key or unique key value in the base table is also unique in the join view.
    3. For an UPDATE statement, all columns updated must be extracted from a
    key-preserved table. If the view was created WITH CHECK OPTION, join
    columns and columns taken from tables that are referenced more than once
    in the view must be shielded from UPDATE.
    4. For a DELETE statement, the join can have one and only one key-preserved
    table. That table can appear more than once in the join, unless the view was
    created WITH CHECK OPTION.
    HTH
    Naveen

  • If Statement Multiple Conditions

    Using LiveCycle, FormCalc or JavaScript.
    I am trying to create an if statement with multiple conditions.
    a Sum field calculates a series of numeric input fields. I want the result in the Sum field to determine the expression in a final field. 
    If the Sum is <30MM then "50000" also if the Sum is >30MM but <60MM then "125000" also if the Sum is >60MM but <100MM then "250000" also if the Sum is >100MM but <300MM then "375000" also if the Sum is >300MM then "500000"
    I have only been able to figure out a single if then for the first condition but not able to include the whole set of conditions and the multiple results.
    Help would be much Appreciated!
    Thank you in advance.

    Tried a few variations of this and still seem not
    to be able to find a solution to this issue. I'm surprised my google searches are bringing up better examples because there seems to be an example of everything else.
    Any more detailed advive?

  • How can I pass multiple condition in where clause with the join table?

    Hi:
    I need to collect several inputs at run time, and query the record according to the input.
    How can I pass multiple conditions in where clause with the join table?
    Thanks in advance for any help.
    Regards,
    TD

    If you are using SQL-Plus or Reports you can use lexical parameters like:
    SELECT * FROM emp &condition;
    When you run the query it will ask for value of condition and you can enter what every you want. Here is a really fun query:
    SELECT &columns FROM &tables &condition;
    But if you are using Forms. Then you have to change the condition by SET_BLOCK_PROPERTY.
    Best of luck!

  • Dropdown editable in a table based on a condition

    Hi,
    I would like to know how to solve these problems.
    1. I have a cell (of the type dropdown by key) in a table, which is editable or not, depending on a condition ? Is this is possible? Please let me know if there is a sample code
    2. So basically I have a  table that has 2 entries in it, user details and one of the fields in FLAG showing the values Yand N for the user. I want the table in the webdynpro application to show the 2 user entries and then in the Flag column have a DROPDOWNBYKEY that DEFAULTS to showing what flag the user has, but also allows the user to CHANGE the entry.
    Thanks in advance
    I appreciate for the help.

    is possible
    In your Context node which is binded to the table as DataSource, add an attribute WDY_BOOLEAN type
    In layout bind the newly created attribute to the dropdown UI element Enable property
    while filling your context node, fill the attribute with abap_true and abap_false based on your condition
    Abhi

  • Mail step should be sent to all the users in multiple condition

    hi experts,
                      I have a requirement on invoice posting.Say if invoice amount is 1000 then person A can post the invoice.
    if >= 1000 person B can post the invoice.
    If >=2000 person C can post the invoice.
                                                                      what i have did is, created a multiple condition and then based on the inv amount assigned 3 methods for invoice posting.My problem is if a person posts an invoice amount of 1000.A mail has to be sent to Person B and C informing that an amount of 1000 is posted with user id.Same for person B, ie mail should be sent to person A and C informing regarding invoice posting.Now inside the multiple condition i cannot assign mail step.Coz based on the condition it will go to a particular user for invoice posting but where as mail should be sent to all the users.So how can i achieve this??.

    Hi Priyanka,
    I am getting 1 thing why you need need multiple conditions. No need to create any multiple conditions.
    1. Create a rule using transaction PFAC. (you can use the example as Rule: 0000168).
        a. Create an function module with table parameters as ACTOR_TAB (type SWHACTOR) and AC_CONTAINER (Type
            SWCONT). You can aswell copy the FM used in 168 rule and change it. check the logic mentioned below.
        b. In PFAC transaction, go to container tab and create 2 container elements.  For amount and Mail_indicator (type should be
            same as that of the amount field in your workflow) .
    2. Use this Rule in your workflow to send mail.
    Logic:
    ""Lokale Schnittstelle:
    *"       TABLES
    *"              ACTOR_TAB STRUCTURE  SWHACTOR
    *"              AC_CONTAINER STRUCTURE  SWCONT
    *"       EXCEPTIONS
    *"              NOBODY_FOUND
      INCLUDE <CNTAIN>.
      DATA BEGIN OF ORG_OBJECT OCCURS 0.
              INCLUDE STRUCTURE SWHACTOR.
      DATA END OF ORG_OBJECT.
      DATA: BEGIN OF NEW_AC_CONTAINER OCCURS 3.
              INCLUDE STRUCTURE SWCONT.
      DATA: END OF NEW_AC_CONTAINER.
      DATA: V_AMOUNT TYPE <AMOUNT FIELD>.
      DATA: V_MAIL_INDICATOR TYPE <FLAG>.
      REFRESH: actor_tab.
    Get the org-object under consideration. AMOUNT is the container name defined in your PFTC transaction
      SWC_GET_ELEMENT AC_CONTAINER 'AMOUNT' 'V_AMOUNT'.
    Get the org-object-type and the org-object id.
      SWC_GET_ELEMENT AC_CONTAINER 'MAIL_INDICATOR' V_MAIL_INDICATOR .
    Pass the org-object, the org-object-type and the org-object-id
    to a new container.
    The org object get a new name: ORG_AGENT.
      SWC_SET_ELEMENT NEW_AC_CONTAINER 'AMOUNT' 'V_AMOUNT'.
      SWC_SET_ELEMENT NEW_AC_CONTAINER 'MAIL_INDICATOR' V_MAIL_INDICATOR .
    check for V_AMOUNT and V_MAIL_INDICATOR.
    if the v_amount = 1000 .'
      if  v_mail_indicator = ' '.
        actor_tab-OTYPE = 'US'  .
        actor_tab-OBJID = 'SAPUSER1'.
        append actor_tab
      else.
        actor_tab-OTYPE = 'US'  . 
        actor_tab-OBJID = 'SAPUSER2'.
        append actor_tab
        actor_tab-OTYPE = 'US'  .
        actor_tab-OBJID = 'SAPUSER3'.
        append actor_tab
      endif.
    endif.
    Similarly write the logic for other conditions.
    This way you can use same for different purposes.
    If you pass the indicator that means you are sending mail to other approvers for information.
    Revert back if u need more help.
    Regards,
    Gautham

  • I need your expert opinion on how to create a map with multiple conditions.

    Hello.
    I need your expert opinion on how to create a map with multiple conditions.
    I have a procedure (which i cannot import or re-create in OWB due to the bug), so i am trying to create a map instead :-(
    How can i create a cursors within the map?
    My function creates table and cursor.
    Then it will have to check for duplicates in the tables (the one created and another table) - the criteria for finding duplicates is a number of fields.I then need to place few different conditions (if some attributes are not available) and it has to load cursor based on this conditions. The next step is to fetch the data into the cursor based on what attributes are missing.
    The next thing it will do is insert the data into table (if record doesn't exist), output the error in separate table is record is corrupted, or update the record with changed information.
    In short i need to re-create match / merge but with conditions, iterations etc 'built into' it.
    I can read up on available functions - it's just what would be the best options? and what would be the best approach to do so?
    In my function i use %rowtype - but cannot use it in owb - so what would be the alternative? i don't really want to create a lot of variables and then have a nightmare of maintaing it. are there any tips regarding this?
    having looked through Oracle dedupe - it's not really what i need because it is just DISTINCT.
    I would appreciate any help / advise on this.
    Thank you very much

    thanks a lot for your reply - i will look into this option :-)
    it is a bit more complicated now as i have to re-create the match / merge and then somehow 'tweak' it to achieve the result i need.
    At the moment i am looking to breakdown the package into smaller chunks 'functions' and try creating the map that way.
    Anyway, thank you very much for your suggestion.

Maybe you are looking for

  • Unable to repair acrobat pro 9.4.6

    win7 pro 64 bit. Windows says to repair/update. tried to repair. got file not found 2/3 through.  a data1.cab file

  • Is Structure Layout View a code smell?

    I have an application that was built by using a double layered architecture. I needed to have a model where I store essential data about actions in this system. This system must be able to display these actions on screen and needs to be in a Composit

  • Restart the process chain

    Hi Experts,    Pls let me know how to restart the process chain Regards Vishal

  • SQLJ or JDBC

    I have asked this question in the SQLJ JDBC forum but since nobody answers me I try here instead: Could you point me to some good resources (white papers) where I can read about performance issues when choosing between SQLJ and JDBC? I assume this is

  • You are not setup a buyer

    Hi All, While attempting to open purchase order from Purchasing vision operations (USA), i am getting following message: You are not setup a buyer. To accept this form you need to be a buyer. I have created a buyer, but still i am getting same in my