Group by preserving the order

Say for eg: my data is like this
--order by col3
col1 col2  col3
1       5     1
1      10    2
2      2      3
2      5      4
1     15     5
I hav to get o/p as
col1  sum_col2
1       15
2       7
1      15
I have to group by preserving the order
Pls give me suggestions..
jeneesh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

I have reserched solution which this problem can solve.
Therefore, I created this solution.
Below solution consults "Tabibitozan".
"Tabibitozan" is Japanese language.
"Tabibitozan" is one of math problem. (http://www.manabinoba.com/index.cfm/4,757,73,html?year=2002)
I do not know what "Tabibitozan" is called in English.
with ValTable as(
select 1 ID,5 Val,1 SortOrder from dual
union all select 1,10,2 from dual
union all select 2,2,3 from dual
union all select 2,5,4 from dual
union all select 1,15,5 from dual
union all select 3,25,6 from dual
union all select 3,10,7 from dual
union all select 3,5,8 from dual
union all select 3,15,9 from dual
union all select 4,5,10 from dual)
select ID,sum(Val) as Val
from (select ID,Val,SortOrder,
       Row_Number() over(order by SortOrder)
      -Row_Number() over(partition by ID order by SortOrder) as makeGroup
      from ValTable)
group by ID,makeGroup
order by min(SortOrder);
ID  VAL
1   15
2    7
1   15
3   55
4    5In above solution,
I imagined that there are 5 people called X,A,B,C, and D.
X always walks 1m. (Row_Number() over(order by SortOrder))
When ID = 1, A walks 1m. (Row_Number() over(partition by ID order by SortOrder))
When ID = 2, B walks 1m. (Row_Number() over(partition by ID order by SortOrder))
When ID = 3, C walks 1m. (Row_Number() over(partition by ID order by SortOrder))
When ID = 4, D walks 1m. (Row_Number() over(partition by ID order by SortOrder))
Then,
I derived distance between X and A.
I derived distance between X and B.
I derived distance between X and C.
I derived distance between X and D.
Then, I made group using these distance. (group by ID,makeGroup)
similar threads
SQL - Hierarchical View
Version Key creation
Group by preserving the order
Is it possible to reset agg function row_number()

Similar Messages

  • Sending Mail with attachment and preserving the order of attachment

    Hi Everyone!!!!!!
    My requirement is to send Email with attachment in the same order as they were attached by Sender while composing the mail. I want to preserve order of attachment in the mail and when receiver open a mail, the attached files should be reflected in same order as it was on sender side. In my application,the documents being upload are already in my database. So, user just requires to mark the corresponding check box and enter the Sequence number for each attachment. When he clicks on "SEND" button, the Source file is first written from database to OS level and then files are attached from source available on OS . The attached file name could be anything. Based on the Sequence number file should be attached and send. My current code works fine,but problem it is that the Sequence number is not preserved in my attachment . However, when I traced the output of FOR Loop by inserting it in a table " temp_trace" I found that the Sequence is preserved in the order that I want. So, I think that uploaded files are written on the Operating system in same order but they are not attached in same order. Below is the sample code that is I am using to attach file in email. Here, the user_sequence is the sequence number entered by user.
    for i in (select case when user_sequence is not null
    then trim(to_char(user_sequence ,'09'))||'_'||file_name||'.pdf'
    else ||'_'||file_name||'.pdf' end file_name ,
    file_src,length(file_src) len,cd.doc_id,PRIORITY from
    MIM_CLIENT_DOCS CD,(select DOC_ID ,user_sequence from table P_DOC_AND_user_sequence ) TEMP_TAB
    WHERE CD.DOC_ID = TEMP_TAB.DOC_ID order by case when user_sequence is null then 1 else 0 end, file_name)
    loop
    insert into temp_trace values(i.file_name);
    L_OUT := UTL_FILE.FOPEN(v_oracle_dir,i.file_name,'wb',32760);
    end loop;
    I want my output as
    1_first attachment.txt
    2_second_attachment.docx
    3_abc.sql
    _xxx.txt  ------------------ When sequence is not assigned by user That is null at last.
    Unfortunately, I am not getting attachment in mail in this sequence. Can anyone give me suggestion.
    Sorry for stuff essay and thanks in advance!!!!!!!!!!!

    >
    Please update your forum profile with a real handle instead of "974850".
    My requirement is to send Email with attachment in the same order as they were attached by Sender while composing the mail. I want to preserve order of attachment in the mail and when receiver open a mail, the attached files should be reflected in same order as it was on sender side. In my application,the documents being upload are already in my database. So, user just requires to mark the corresponding check box and enter the Sequence number for each attachment. When he clicks on "SEND" button, the Source file is first written from database to OS level and then files are attached from source available on OS . The attached file name could be anything. Based on the Sequence number file should be attached and send. My current code works fine,but problem it is that the Sequence number is not preserved in my attachment . However, when I traced the output of FOR Loop by inserting it in a table " temp_trace" I found that the Sequence is preserved in the order that I want. So, I think that uploaded files are written on the Operating system in same order but they are not attached in same order. Below is the sample code that is I am using to attach file in email. Here, the user_sequence is the sequence number entered by user.Always post code using <tt>\...\</tt> tags as described in the FAQ.
    for i in (select case when  user_sequence is not null
    then trim(to_char(user_sequence ,'09'))||'_'||file_name||'.pdf'
    else  ||'_'||file_name||'.pdf' end file_name ,
    file_src,length(file_src) len,cd.doc_id,PRIORITY from
    MIM_CLIENT_DOCS CD,(select DOC_ID ,user_sequence from table P_DOC_AND_user_sequence ) TEMP_TAB
    WHERE CD.DOC_ID = TEMP_TAB.DOC_ID order by case when user_sequence is null then 1 else 0 end, file_name)                               
    loop
    insert into temp_trace values(i.file_name);
    L_OUT := UTL_FILE.FOPEN(v_oracle_dir,i.file_name,'wb',32760);
    end loop;I want my output as
    1_first attachment.txt
    2_second_attachment.docx
    3_abc.sql
    _xxx.txt  ------------------ When sequence is not assigned by user That is null at last.
    Unfortunately, I am not getting attachment in mail in this sequence. Can anyone give me suggestion.I see no code that attaches anything to email messages. What code is used to do this? Why are the files being written to the file system?

  • Is there a way to make the Form Wizard group things in the order I want it to?

    I am creating a multi-page checklist. Each question has a radio button with the possible answers of yes, no or n/a. When I run the Form Wizard, it detects the boxes I created, in Word using the Developer, but it puts all the yes's in a group, all the no's in a group and leaves the n/a's out. Is there a way to make it so when the Form Wizard creates the radio buttons, it puts a yes, no and n/a in one group? I don't want to have to create a sepearte group for each question. These checklists are long and there are hundreds of questions.

    The auto field detection feature is designed to look for Yes/No text labels next to each radio button group -- not specifically n/a. Do you have a group label to the left or above each group? This is how each group is determined -- if it's not there they'll default to check boxes. For more detail on how the auto field detection feature works, check out the tutorial Designing Forms for auto field detection. Also, there were a few improvements in the 9.2 release, so make sure you're using this point release or later.

  • How do I get Iphoto to preserve the sort order when I transfer an album?

    Here's my problem. I carefully sorted my album of 230 photos manually. Each photo was then numbered from 1 to 230.   Then I used IPhoto Library Manager to transfer this album someplace else -- to my desktop, to a thumb drive, to Dropbox---   In every case, when I did that, the photos themselves transfered just fine,  but the sort order I had so carefully constructed has fallen apart and cannot by retrieved. Even  when I  close and then reopen an Album I have transferred to my Desktop, it loses its sort order.  This is driving my crazy  because I need to transfer this album so that it preserves the slideshow I have designed for it. A slideshow, of course, demands preserving the order of the slides.
    In short, how can I preserve the sort order of an album when I transfer it out of one library to another?

    If you're transferring to another library, why are you exporting to the Finder (which is what Desktop, thumbdrive and DropBox are). This doesn’t understand manual sorting from iPhoto.
    Here's one (of several) way(s) to do what you want:
    Make an Album of the photos, then drag the pics into your preferred order.
    Then Photos Menu: Batch Change -> Set Title to Text "John's Birthday", for instance, and tick the box to append a number to each Photo. Now your photos are titled 'John's Birthday 001, John's Birthday 002 ... etc'
    Then File -> Export and in the Export dialogue set the Filename to "Use Title"
    Sort on Filename in the FInder and you end up with a folder full of images in the same order as the Album in iPhoto.

  • Source Group and preserving IP Adress from the client

    Hi to all
    I have a question regarding source Group. Is there a way when I use Source group to preserve the Ip Adress from the client for the purpose to log the IP Adress of the Internet client

    no. there is no way.
    Gilles.

  • Parse and output XML document while preserving attribute order

    QUESTION: How can I take in an element with attributes from an XML and output the same element and attributes while preserving the order of those attributes?
    The following code will parse and XML document and generate (practically) unchanged output. However, all attributes are ordered a-z
    Example: The following element
    <work_item_type work_item_db_site="0000000000000000" work_item_db_id="0" work_item_type_code="3" user_tag_ident="Step" name="Work Step" gmt_last_updated="2008-12-31T18:00:00.000000000" last_upd_db_site="0000000000000000" last_upd_db_id="0" rstat_type_code="1">
    </work_item_type>is output as:
    <work_item_type gmt_last_updated="2008-12-31T18:00:00.000000000" last_upd_db_id="0" last_upd_db_site="0000000000000000" name="Work Step" rstat_type_code="1" user_tag_ident="Step" work_item_db_id="0" work_item_db_site="0000000000000000" work_item_type_code="3">
    </work_item_type>As you may notice, there is no difference in these besides order of the attributes!
    I am convened that the problem is not in the stylesheet.xslt but if you are not then it is posted bellow.
    Please, someone help me out with this! I have a feeling the solution is simple
    The following take the XML from source.xml and outputs it to DEST_filename with attributes in a-z order
    Code:
    private void OutputFile(String DEST_filename, String style_filename){
         //StreamSource stylesheet = new StreamSource(style_filename);
         try{
              File dest_file = new File(DEST_filename);
              if(!dest_file.exists())
                  dest_file.createNewFile();
              TransformerFactory tranFactory = TransformerFactory.newInstance();
              Transformer aTransformer = tranFactory.newTransformer();
              aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
              Source src = new DOMSource("source.xml");
              Result dest = new StreamResult(dest_file);
              aTransformer.transform(src, dest);
              System.out.println("Finished");
         catch(Exception e){
              System.err.print(e);
              System.exit(-1);
        }

    You can't. The reason is, the XML Recommendation explicitly says the order of attributes is not significant. Therefore conforming XML serializers won't treat it as if it were significant.
    If you have an environment where you think that the order of attributes is significant, your first step should be to reconsider. Possibly it isn't really significant and you are over-reaching in some way. Or possibly someone writing requirements is ignorant of this fact and the requirement can be discarded.
    Or possibly your output is being given to somebody else who has a defective parser which expects the attributes to be in a particular order. You could quote the XML Recommendation to those people but often XML bozos are resistant to change. If you're stuck writing for that parser then you'll have to apply some non-XML processing to your output to fix it up on their behalf.

  • [q] how to understand the 'order' of a list?

    hi,everyone
    there's a test program:
    import java.util.*;
    public class testList
    void test()
    LinkedList ll= new LinkedList();
    ll.add("3");
    ll.add("1");
    ll.add("2");
    ll.add("5");
    ListIterator li = ll.listIterator();
    while (li.hasNext())
    System.out.println(li.next());
    public static void main(String[] args)
    testList tl = new testList();
    tl.test();
    As we know, order is the most important feature of a List.
    But this program's result is " 3 1 2 5 " which is not a sorted list!
    How we understand the phrase 'order' ?
    And , is there any method in Collection which can sort the elements in one collection (list or set)?
    thanx a lot!

    As we know, order is the most important feature of a
    List. Yes, and it preserves the order you have used to put elements in the List. :)
    But this program's result is " 3 1 2 5 " which is not
    a sorted list!Well, what order to you actually like? "1 2 3 5"? Why not "5 3 2 1"? And for more complicated objects "natural" order may be even less obvious. And never forget that List may contain objects that belong to different classes - for example Integer, String and Date - how they are to be ordered? (Of course, such usage is not recommended).
    How we understand the phrase 'order' ?
    And , is there any method in Collection which can
    sort the elements in one collection (list or set)?There are sort-methods in java.util.Collections class.

  • I have two albums (RCA Sweeny Todd 2 discs set and RCA Into the Woods 1 disc) where the songs are listed as being on the same album on the list view, but do not group together on the iPod, so the songs can't play consecutively, or in the proper order.

    I have two albums (RCA Sweeny Todd 2 discs set and RCA Into the Woods 1 disc) where the songs are listed as being on the same album on the list view, but do not group together on the iPod, so the songs can't play consecutively, or in the proper order. The "Apply Sort Field" and "Same album does not work, but ti is only these two RCA albums that do this. All other albums remain intact. Need help with this minor problem. Thanks.

    UPDATE: This behavior (to an extent) still happens when "Repeat Album" is off. Instead of an infinite loop, it goes through all the tracks on the album (unsuccessfully) and kicks me out to the Album overview screen. Going back to list of Albums and tapping the Album again brings up "No Content: You can download music from the iTunes Store" but that screen immediately kicks me back to list of Albums.
    So I am just even more confused now.

  • With using any version of iTunes, whenever I import songs into my library using the "add to library" function, iTunes adds those songs in songs' alphabetical order. This is pretty annoying since it does not preserve the logical track number or album.

    With using any version of iTunes, whenever I import songs into my library using the "add to library" function, iTunes adds those songs in songs' alphabetical order. This is pretty annoying since it does not preserve the logical track number or even the album sort order (for example when adding 2 albums stored in a unique folder). Anyone has any ideas ? Been stumped for a while....!!!

    hi i had the same problem today when i updated my itunes to latest version. however, i have just found my songs in the 'itunes media' folder. this was accessed through 'my music'  then keep clicking through until you find itunes media and all my library songs were in there and i then just added these files to my library and all were restored however, i have lost all my playlists but at least my 700 songs are back. very dissapointed with apple that they have let this happen with their latest update, the previous version was miles better than this one . hope you find them. stevo

  • The order of the columns in GROUP BY doesn`t matter?  why?

    I did a little test and find out that the order of the columns in GROUP BY does not matter to ORACLE.
    Why? How does oracle do GROUP BY exactly?
    Could anyone show me its processing logic? maybe in pseudo-code?
    Thanks in advance.

    I mean,
    I thought oracle would group data based on the order of the columns in the GROUP BY LIST.
    For example: for select deptno, job, count(*) from emp group by deptno, job;
    I thought, oracle first would group the data based on deptno (10,20,30), e.g. in deptno=30, we got 6 employees.
    and then, group this 6 employees based on JOB (CLERK, SALESMAN, MANAGER).and
    for select job, deptno, count(*) from emp group by job, deptno;
    first would group the data based on JOB (CLERK, SALESMAN, MANAGER) e.g., for CLERK, we got 4 employees,
    and then, group this 4 employees based on deptno (10,20, 30).and to me, it seems that in some circumstances, their result could be different.
    So, I wonder if the order of the columns in GROUP BY Clause matter?

  • The order of attributes from an Attribute Group is not reflected in Excel Add-in

    It looks like the order of my attributes in an attribute Group is not reflected in the Filter dialog in the Excel Add-in. Any way that I from server side can handle the attribute order in Excel?
    The order is correct in the Attribute Groups in the Web UI.
    /Stig

    Dear Sai,
    Take help of ABAPer to capture the header or item text in to the print copy of sales order ,delivery and invoice.
    I hope this will help you,
    Regards,
    Murali.

  • Is there a way to change the order that Ctrl+` switches through tab groups?

    I prefer for Ctrl+Tab to switch between tabs in the most-recently used order (the same way Alt-Tab works), so I use that setting in about:config. Is there a way to do the same thing with the Ctrl+` shortcut that switches through tab groups? By default it appears to cycle through all of them, regardless of the order of use, which is very inconvenient for me, because I have tab groups that I don't use very frequently that just get in the way when cycling through them.
    Barring that, is there a way to make it so that Ctrl-Tab cycles through all tabs, regardless of what group they're in?
    I'd really appreciate any information. Thanks!

    You're not alone in wanting this.  Feel free to provide feedback to Apple directly:
    http://www.apple.com/feedback/ipad.html

  • Can I rearrange the order of my calendar groups?

    I am only able to drag a calendar group to the very top. I have several groups of calendars and would like to arrange them in a specific order and it will be frustrating to have to do it from the top down. If you don't know what I am talking about, create 3 or 4 groups and try to put the one on bottom in the second position.
    This wouldn't bug me if I didn't have 30 groups to arrange.

    First (just to make things easier) close all the groups triangles (of course they might open any way as you move a group).
    Now, when you grab the lower group to move it up, watch the mouse arrow. Move the arrow between groups, and to the left, between the triangles and the checkboxes.
    It is hard to see but a placement line does appear (behind the group you are moving) when the arrow is placed in this location (I wish I could send a picture with this email).

  • Change the ordering unit for a group

    Gurus,
    I need  to change the ordering unit for a particular group of materials using the mass changes in material master . The problem here is it is not showing the ord UoM in the selection field. ?

    hi
    pls follow this:
    1.Go to MM17
    2. Click on fields
    3. Search for MARA-BSTME
    You will get ordering unit.
    GANESH....

  • If I mess up an existing song, can I abandon it, or "save as" in order to preserve the original? GarageBand for iPhonr 4S

    For GarageBand, iPhone 4S:  If I open a song, and mess it up beyond repair, can I abandon it without saving, or do a "save as" in order to preserve the original song?
    Thanks!!!

    Your iCloud account and any associated email account/addresses remain active regardless ow what devices or carriers you use.  They are tied to your Apple ID.   All you would need to do is sign back into the account on your new device(s) to continue to use the account.
    If you are upgrading your 4s to a 5s, be sure to save any photo stream photos on your 4s that you want to keep to the camera roll, then back it up.  When setting up your 5s, restore the backup of your 4s to transfer you data, settings, apps and other purchased media (see http://support.apple.com/kb/ht2109).

Maybe you are looking for

  • Training and Event Management booking date

    Hi, I am working on Training and Event Management cube 0PE_C01. One of the requirements of a query is to show the booking date of the course. I have looked in business content for other cubes i have checked info object and 0event, i have also checked

  • No sound at all after 10.4.6 update

    Hi, does the 10.4.6 update have a firmware update included ?, because after restart etc no sound at all eminates from my powerbooks built in sound. No start up chime, system sounds .... nothing [only thing i can see in "sound on the system prefs is a

  • How do I set the URL to be correct in the browser?

    When I click on a link, like the following: "http://127.0.0.1:8100/test/viewpage?cmd=viewpage&id=2808" I then get that link appearing in the browser URL field. However, when I go to the same page via a Servlet (and the action is activated via a Submi

  • Delete file that is used by JVM ?

    Hi all, I saw some comments on this topic but I'm still not sure what the answer.. My program creats a jar file that is used by an application server. I want to delete it in the end of the run. In order to do so I've used delete() and deleteOnExit()

  • Amazon Template for Empty Production 12.1.3 Database

    Dear Everyone, Before I spend a lot of time trying the template listed in the AWS search facility: 936851542197/Oracle E-Business Suite 12.1.3 Database 32-bit - OVM The text says: "The templates are pre-configured for Vision Demo or fresh install dat