Sum based on group by and one condition

I have a tableof following structure
Group SubGroup sequence length 
A         x               1            10
A         X        
    2            20
A         X               2            30
A         X               3            50 
B         Y               4  30
B         Y               5            40
I needa new column with a sum of lengths grouping Group,sub group and sequence but the condition is sum should be calculated based on the sequence of each row.Sum is sum of lengths where sequence is greater than the current row sequence number and grouping
group nad sub group
following is the output i am looking for
Group SubGroup sequence length   Sum
A         x               1            10          100(Sum of all the lengths where sesquence > current rows sequence
                                                         and group by group,sub
group and sequnce) 
A         X               2            20          50
A         X   3 30 NULL
B         Y               4  40 50
B         Y      5 50 NULL

Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. 
And you need to read and download the PDF for: 
https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
>> I have a table of following structure <<
Your posting is crap! Did you know that GROUP and SEQUENCE are reserved words in SQL? Did you really want to use a CREATE SEQUENCE? Did you know a table has a name and key?? That having a separate “<something>_sub_group” column is so bad a design flaw
it has a name (attribute splitting)? That “<something>_length” cannot exist without being the length of something in particular? 
CREATE TABLE Rude_Postings
(rudeness_grp CHAR(2) NOT NULL
 CHECK (rudeness_grp LIKE '[A-Z][A-Z]'),
 foobar_seq INTEGER NOT NULL
 CHECK (foobar_seq > 0),
 PRIMARY KEY (rudeness_grp, foobar_seq),
 foobar_length INTEGER NOT NULL
 CHECK (foobar_length > 0)
The foobar_seq simply looks wrong. It was not unique, so it could not be a sequence by definition. I will guess that it is unique within a rudeness_grp. 
INSERT INTO Rude_Postings
VALUES
('AX', 1, 10),
('AX', 2, 20),
('AX', 3  30),
('AX', 4,  50),
('BY', 1 , 30),
('BY', 2 , 40);
>> I need a new column with a sum of lengths grouping Group,sub group and sequence but the condition is sum should be calculated based on the sequence of each row. Sum [sic: SUM is a reserved word!] is sum of lengths where sequence is greater than the
current row [sic: tables are sets not files, so there is no ordering and no concept of a current row] sequence number and grouping group and sub group << 
Here is my guess:
SELECT rudeness_grp, foobar_seq, foobar_length 
   SUM( foobar_length) 
       OVER (PARTITION BY  rudeness_grp 
               ORDER BY  foobar_seq DESC
               ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
     AS foobar_length_running_tot
  FROM Rude_Postings;
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • When I send a group txt and one of the people reply it creates a new message.   How do I keep all the replies in the same message?   I used to not have this problem but it started last month.

    When I send a group txt and one of the reciepients replies it creates a new message.   How do I keep all the replies in the same message?

    If you think getting your web pages to appear OK in all the major browsers is tricky then dealing with email clients is way worse. There are so many of them.
    If you want to bulk email yourself, there are apps for it and their templates will work in most cases...
    http://www.iwebformusicians.com/Website-Email-Marketing/EBlast.html
    This one will create the form, database and send out the emails...
    http://www.iwebformusicians.com/Website-Email-Marketing/MailShoot.html
    The alternative is to use a marketing service if your business can justify the cost. Their templates are tested in all the common email clients...
    http://www.iwebformusicians.com/Website-Email-Marketing/Email-Marketing-Service. html
    "I may receive some form of compensation, financial or otherwise, from my recommendation or link."

  • Calculate percrentage based on 2 column and one filter

    Hi,
    I am looking for a way to compute percentage based one two column and 1 filter but i have a problem when every thing is checked on my filter.
    The table as follow is my example:
    ID
    cat1
    cat2
    number
    total
    pct
    1
    a
    aa
    5
    40
    13%
    1
    a
    ab
    20
    40
    50%
    1
    a
    ac
    15
    40
    38%
    1
    b
    ba
    12
    47
    26%
    1
    b
    bb
    20
    47
    43%
    1
    b
    bc
    15
    47
    32%
    2
    a
    aa
    9
    45
    20%
    2
    a
    ab
    16
    45
    36%
    2
    a
    ac
    20
    45
    44%
    2
    b
    ba
    10
    50
    20%
    2
    b
    bb
    20
    50
    40%
    2
    b
    bc
    20
    50
    40%
    I create a power table as follow:
    Filter: column ID
    Axis: cat1 and cat2
    values: pct
    The problem is when I want to select all my data in my filter, a sum/mean is done for pct but the results are wrong. Did I missed something ? Is there a way to correct the problem ?

    Hi mi jo,
    It appears that you have created your percentage as a Calculated Column and what you actually want is a Calculated Field.
    You can create a Calculated Field by clicking on the Power Pivot ribbon and using a DAX formula similar to the following:
    Percentage:=DIVIDE(SUM(TableName[number]), SUM(TableName[total]))
    I have formatted this Calculated Field as a percentage and used it to create a Pivot Table:
    Is this what you were after or have I misunderstood the problem?
    Edit: Just took a quick look at the workbook and can see that the pct column is coming in as a fixed value for a row. The above solution should help. A best practice would be to first create a Calculated Field for each of the number and
    total columns such as...
    Sum of Number:=Sum(TableName[number])
    And...
    Sum of Total:=Sum(TableName[total])
    And then finally update this DAX formula:
    Percentage:=DIVIDE(SUM(TableName[number]), SUM(TableName[total]))
    To this...
    Percentage:=DIVIDE([Sum of Number], [Sum of Total])
    Regards,
    Michael Amadi
    Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to vote it as helpful :)
    Website: http://www.nimblelearn.com
    Blog: http://www.nimblelearn.com/blog
    Twitter: @nimblelearn

  • Dequeue based on correlation ID and dequeue condition

    My version is :
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    PL/SQL Release 10.2.0.3.0 - Production
    "CORE     10.2.0.3.0     Production"
    TNS for Linux: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    I am executing a Stored Procedure by specifying the dequeue condition and correlation ID.But I get ORA-25240.
    IN another thread Problem with ORA-25240 and msgId:=NULL I see that the problem was solved by specifying the correlation id in dequeue condition.
    I am trying to set it as follows : deqopt.deq_condition := 'CORRID = AAA'; but i get ORA-00904 error with this
    Can someone tell me how to do that?

    A_non
    I solved the issues as per ur advise.
    just hard coding.
    begin
    IF ai_flag='E' THEN
    dopt.deq_condition := 'xmltype.getRootelement(user_data)='''||'Confirmation'||'''';
    ELSif ai_flag='O' THEN
    dopt.deq_condition := 'xmltype.getRootelement(user_data)='''||'order'||'''';
    END IF;
    END;
    Now based on parameter corresponding Root element get dequeued.
    fine
    But... my techspec talks me more technical
    "The dequeue process must not be allowed to multi-thread because if we receive order/Business/Confirmation is quick succession we need them to be processing serially. There is a queue lock strategy to ensure single-threading."
    Hmm... This is to be more technical Give me ur advise
    S
    Message was edited by:
    AswinGousalya
    Message was edited by:
    AswinGousalya

  • Af:query and or conditions

    Is it possible to use af:query for and or conditions , i am able to create view criteria with groups of and or conditions , but in run time it only uses the first group. looks like it is not possible , but wondering if anybody successful with work around
    I was hoping to implement functionality similar to what's documented in below link for 10.1.3 in version 11 using af:query
    http://my.opera.com/dominionspy/blog/2007/02/26/an-advanced-custom-search-form-for-adf
    Thanks

    Frank
    Thanks for your reply
    I found this in developer guide after posting my question here
    Note: Query search forms do not support view criteria defined by
    nested expressions (multiple groups of view criteria items). Therefore
    avoid creating a view criteria with multiple groups in its expression
    when you intend for the UI designer to be able to create a query
    search form.
    The main reason i want use the qf:query is to make use of the save search feature with mds
    our users need advanced search filters for this application and they need different filter based on which customer data they are searching , but for any particular customer the filter will stay the same and can be used when they need to search data for that customer again in future. thought save search will help a lot here
    i can code the same way as the above example i posted for 10.1.3 in 11g , but i need the save search feature and i do not know how to code for that on on my own
    Regards

  • Remove group by and sum using Itab

    Hello,
    I am changing one report or performance problem.
    It has a select clause on BKPF which is retrieving SUM (wrbtr ) AS wrbtr in the field list and USING group by 6-7 fields...
    I want to remove this group by clause, take all records ( without sum ) into another internal table,  sort it by these 6 fields ( will define them at the start of structure),
    loop through it and then use AT NEW for these 6 fields and use collect for summing the wrbtr.
    Will it be more efficient.? Data volume is very large.
    Please advice.
    Thanks,
    Rupalee.

    Hi Rupali,
    BKPF is not having field 'WRBTR'. Also just a suggestion, whenever you are fetching data from BKPF try using indexes in where condition if your condition includes the fields that have indexes. Also note that whenever you are using an index then include those fields in same sequence as mentioned in index. For example, if you want to fetch data based on Reference Transaction and Reference Key then in where condition put AWTYP first and then AWKEY. ( See index no. 4 when you click 'Indexes' button on Application toolbar. )
    BKPF contains huge volume of data and if selection is based on primay key or indexes then searching will be much faster.
    Regards,
    Saba

  • In system settings 15 item are not accessible, get the message: cannot open it, is not possible on intel based MAC. Items like security, i cloud user and groups network and more

    in system settings 15 item are not accessible, get the message: cannot open it, is not possible on intel based MAC. Items like security, iCloud user and groups network and more.
    Genius bar told me to erase the harddisk and install OSX Maverick again. This has not changed the problems with the system settings

    You need to Repartition the drive as One partition which will erase all data from it and then reinstall OS X. then with the initial setup system you chose the language on the first screen that comes up. That sets the language for the whole system.
    There is no other way of completely removing certain information created by a previous owner. this hold strue whether you are selling or give a Mac to someone.
    To do the repartitioning you need to use the Online Internet Recovery system, if the Mac came with Lion 10.7 or above, or from the original system reinstall discs that came with it when it was new.

  • Customers, Sales Orders Restrictions based on Sales Office and Sales Groups

    hi all,
    We have a typical scenario for one of our customers. They are SAP ERP, ECC 6.0.
    They have sales employees/managers assigned to customers. In order to maintain the link between the sales employees and the customers, their earlier implementation partner followed the following method for the linkage.
    Each sales employee and each sales managers are given a SAP User ID like 101, 012, 103 etc.. Sales groups OR Sales offices are also maintained with the same numbers/IDs. At the customer master level, each customer's sales group is given the sales employee ID (which is same as that of the sales group). The example is as follows.
    Customer No.             Sales Group                 Sales Office                                                                               
    CU1                              101                             108
    CU2                              101                             108
    CU3                              102                             108
    CU4                              103                             109
    CU5                              104                             110
    The sales group names and the sales offices names are given the same names as that of the user IDs of the sales employees/sales managers. In the above example, the sales employee 101 (sales group 101) is assigned to customers CU1, CU2. The sales manager 108 (Sales office 108) is managing the sales employees 101 and 102.
    Now the customer has some authorization issues. He wants us to provide authorizatinos in such a way that -
    if the sales employee 101 logs on to the system he should be able to Display the CUSTOMERS and the Sales Orders (XD03, VD03, VA03) that are assinged only to him (In other words with the sales group 101) and similarly for other sales employees.
    if the sales manager 108 logs on to the system he should be able to display the customers and the sales orders that are assigned to his sales employees (101, 102). In other words with the sales office as the manager's ID 108.
    I tried to use the authorization object V_VBKA_VKO but this doesnt see to be serving the purpose as this object is not being checked at all in any of the above VA03, XD03 and VD03 transactions.
    Similarly I did not find any other authorization object which makes use of Sales group field and Sales office field which also has a linkage to VA03, XD03 and VD03 transactions.
    The customer is evaluating our contract with them based on this solution.
    Any help is greatly and highly appreciate.
    Thanks,
    Niran.

    Hi,
    Please use the following authorization objects for your purpose. For more details please go through the documentation of the objects:
    V_KNA1_BRG     Customer: Account Authorization for Sales Areas
    V_KNA1_VKO     Customer: Authorization for Sales Organizations
    FYI:    V_VBKA_VKO     SD     Sales Activities: Authoriz.for org.data and sales activ.type
    contains the following Fields which are relavant to your query
    VKORG      Sales Organization
    VTWEG      Distribution Channel
    SPART      Division
    VKBUR      Sales Office
    VKGRP      Sales Group
    KTAAR      Sales Activity Type for Sales Support
    ACTVT      Activity
    Regards,
    Dipanjan

  • Calculate SUM based in condition in iTAB

    Hi all,
    I have the ITAB as follows.
    wa_mseg-mblnr = '5000000130'.
    wa_mseg-mjahr = '2008'.
    wa_mseg-bwart = '901'.
    wa_mseg-dmbtr = '00000005000'.
    wa_mseg-bpmng = '00000000100'.
    wa_mseg-ebeln = '1059200855'.
    wa_mseg-ebelp = '00010'.
    APPEND wa_mseg to itab_mseg.
    wa_mseg-mblnr = '5000000131'.
    wa_mseg-mjahr = '2008'.
    wa_mseg-bwart = '902'.
    wa_mseg-dmbtr = '00000002500'.
    wa_mseg-bpmng = '00000000050'.
    wa_mseg-ebeln = '1059200855'.
    wa_mseg-ebelp = '00010'.
    APPEND wa_mseg to itab_mseg.
    wa_mseg-mblnr = '5000000132'.
    wa_mseg-mjahr = '2008'.
    wa_mseg-bwart = '901'.
    wa_mseg-dmbtr = '00000002500'.
    wa_mseg-bpmng = '00000000050'.
    wa_mseg-ebeln = '1059200855'.
    wa_mseg-ebelp = '00010'.
    APPEND wa_mseg to itab_mseg.
    now i want to add the BPMNG for the BWART = 901
                                   DMBTR   for the BWART = 901
    ans same thing  for the BWART = 902.
    how do i calculate the SUM based on condition.

    now i want to add the BPMNG for the BWART = 901
    DMBTR for the BWART = 901
    ans same thing for the BWART = 902.
    how do i calculate the SUM based on condition.
    Hi, you can loop de internal table and do a control cut by "bwart". For doing that the order of the fields of your internal table must change.
    If you have:
    1st.mblnr
    2nd.mjahr
    3rd.bwart
    you have to change to:
    1st.bwart
    2nd.mblnr
    3rd.mjahr
    So in the loop you will can do like this.
    loop at itab.
    aux_sum = itab-BPMNG + aux_sum.
    at end of bwart.
    * Here you will have de SUM for BWART.
    * Then you clear aux_sum for the next different BWART.
    endat.
    endloop.
    hope this help you.
    Andrew83

  • HT4528 My friend switched from an iphone to a non apple phone. All my texts to him in groups and one on one still come up as imessages. How do I get rid of the imessage when i talk to him? I have tried deleting his number, deleting all messages he was apa

    My friend switched from an iphone to a non apple phone. All my texts to him in groups and one on one still come up as imessages. How do I get rid of the imessage when i talk to him? I have tried deleting his number, deleting all messages he was apart of

    Did you try to unregister the phone?
    Check this discussion

  • Problem in creating group above AND group left in one report!

    Hi all,
    I need a report that is a combination of group above and group
    left.
    Suppose I have 3 table (Emp, Sales, Product):
    Emp Table has 2 column
    - Emp_PK
    - Emp_Name
    Sales Table has 4 column
    -Sales_PK
    -Sales_Date
    -Emp_FK
    -Produck_FK
    -Quantity
    Product Table has 3 column
    -Product_PK
    -Product_Name
    -Product_Price
    I want to make Employee Sales Report For The Month that will
    look like this:
    Emp Number___: Emp_PK
    Emp Name_____: Emp_Name
    Sales Date______Product_Name____Quantity \(2 sales
    ________________Product_Name____Quantity /the date)
    Sales_Date______Product_Name____Quantity
    As you can see this consist of group above (The Employee) and
    group left (The Sales and Product).
    I create this by using 2 queries and link them (the Emp_PK from
    1st query and Emp_FK from 2nd query) on the report builder using
    data link.
    The first query is:
    Select Emp_PK, Emp_Name from Emp
    The second query is
    Select Emp_FK, Sales_Date, Product_Name, Quantity
    From Sales, Product
    Where Product_PK=PRODUCT_FK
    I then create the layout for second query and choose group left
    for Sales_Date using wizard and I create additional layout to
    for the employee.
    The problem is that when I run this report, it will print ALL
    the employee record first (including employee who has NOT sale
    anything) and then on the last page it will print the record of
    Last employee on the Emp table and ALL sales record (including
    those that is done by other employee).
    This report will run correctly if I choose an exact employee
    (For example by adding Where Emp_PK=1111 in the first query) to
    report all of the sales done by this person (employee with emp
    number of 1111). However I need the report to run and print
    ONLY those employee who has Sales Records!
    I thought that by linking the 2 queries in Data Model, it will
    have the same effect as linking using the WHERE clause in query.
    If suppose I create a query like this:
    SELECT Emp_PK, Emp_Name, Sales_Date, Product_Name, Quantity
    FROM Emp, Sales, Product
    WHERE Emp_PK=Emp_FK AND Product_PK=Product_FK
    The report will run OK but I can only choose EITHER group above
    or group left for this ONE query methods in report Wizard.
    Sorry if it is a long question but I hope you can see what I am
    trying to do.
    Thanks in advance for any tip.

    hello,
    of course you can create group left and group above blocks in a
    single report.
    you might want to use the INSERT REPORT BLOCK instead of the
    report wizard, as the report wizard only allows you to use one
    report layout per report, where the report block wizard (invoked
    by INSERT REPORT BLOCK) allows you to choose on a per-block
    basis.
    of course you can create the blocks completely from scarth by
    hand, if you want to.
    regards,
    the oracle reports team --pw                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Transportation Group and Shipping condition fields in Route determination

    Hello Guys,
    Please, I heard that if I leave Transportation Group and Shipping conditions field empty Route determination would consider whatever value I put in Material/Custumer Master.
    But the determination just happen if I put the exact value in either the determination and the Material Master for Transportation Group field for example.
    Have you ever determined Routes leaving the fields empty?
    Thank you,
    Regards,

    Hi,
    Those fields Transportation Group and Shipping conditions available in Route Determination are essential to trigger the correct route in Sales Order level.
    If blank the available routes need to be picked manually at Sales Order level.
    Kindly check .
    Regards,
    SRK

  • Is it possible to have two Overhead rates-one based on Material and one based on direct labor?  I am implementing a SAP Business 1 System.

    Is it possible in SAP business one to have two Overhead Rates-- one based material cost and one for direct labor dollars?

    Hi Dick,
    Business one experts will be available on Business One forum and not  here (Accounting and financial close). Click on the below link and start a discussion. Good luck.
    SAP Business One Application
    Thanks and regards
    Sam

  • How do I fix a problem group messaging with one non-iphone user and several iphone users?

    My brother switched from an iphone to another smart phone several months ago. The rest of our family all have iphones. When he made the switch, my sister suddenly had problems with group messages going through for him--they went through as imessages for her instead of SMS or MMS. I didn't have that problem until a few days ago. I upgraded to an iphone 6 about a week ago, and group messaging my brother worked fine at first, and then a couple of days ago, suddenly stopped working. If I am texting him singly, it's fine. But in group texts, it automatically sets at imessage.
    He has called apple and his number is completely deregistered with imessage. Does anyone know what else we can try? I have deleted all previous messages that were imessage and deleted and re-entered him as a contact.

    I'm assuming neither of you heard back from anyone?  I'm having the same issue - just in the last few days.  The non-iphone user is not receiving texts from the group message because it is sending as an imessage.  It flipped to imessage randomly last week.  I've tried to delete the group message and restart it; same issue.  Would love to hear how to resolve, Apple!

  • How to select certain xml group based on attribute id and return as xmltype

    hi,
    i have an xmltype column name "NM" in table "XMLTEST", the content of the xml in NM is
    <ct>
    <sp id="ALEX">
    <sp id="JOHN">
    <sp id="MIKE">
    <sp id="CALEB"/>
    <sp id="PAUL"/>
    </sp>
    </sp>
    <sp id="MAX">
    <sp id="WAYNE"/>
    </sp>
    <sp id="MAX"/>
    </sp>
    </ct>
    The group i want to select is ALEX except group "MIKE" and "MAX", which return
    <ct>
    <sp id="ALEX">
    <sp id="JOHN"/>
    <sp id="SEAN"/>
    </sp>
    </ct>
    Is it possible? i'm processing this using stored procedure PL/SQL, my environment is 11g R2 Win 32bit.
    please advice,
    wesley

    Hi,
    Not sure where "SEAN" comes from... Typo?
    Try with deleteXML :
    SELECT deleteXML(t.nm, '/ct/sp[@id="ALEX"]//sp[@id="MIKE" or @id="MAX"]')
    FROM xmltest t
    ;

Maybe you are looking for

  • What resolution - JPG's for 500x s.s.  ...?

    Ok; I've finally got my slide-show collection together. They're all jpg's. Now how do I control/determine their resolution so as to limit their file sizes..? I'd set them up in PhotoShop(CS2). One of who's options is jpg quality when saving. This isn

  • IPhone not functioning.

    I was playing on my iPhone 4S, when the screen sudenly shut off completely. The phone was completely unresponsive, and still is. I have plugged it in to my computer and tried to do a reset, but I get an error message "Error has occurred during restor

  • Using an Ipad Air is there a way to change the page format in Ibooks to show what chapter you are in?

    Anyone have an answer?

  • RTP interface, update profile and value in ES31

    Hi All, Tcode: ES31 My requirement is to update the profile and constant value for RTP interface in mass. I tried creating BDC, but while selecting the profile role SY-UCOMM is '%GC 132 22' and losing the BDC controls. Even I tried to look for any BA

  • PLD Purchase Order

    Hi All, The need is to get the number of items on page header. For ex. if i have 20 items on a purchase order, i would like to show on the lay out..Total items = 20.  How do we achieve this? Thanks, Joseph