How to "group by" over a range?

hi
I have a table like this:
x     y     AMNT
1     120     12
1     120     93
1     125     31
1     260     15
2     56     16
2     115     49
3     45     71
4     19     11
4     16     48
5     94     52
5     98     47
I want to group records on x , y columns and aggregate on amnt column, in which difference between values of y column be less than 10.
The result is like this:
x     y     sum(AMNT)
1     ?     136
1     260     15
2     56     16
2     115     49
3     45     71
4     ?     59
5     ?     99
What query can I use?

Like this
"afiedt.buf" 25 lines, 901 characters
  1  with t
  2  as
  3  (
  4  select 1 x, 120 y, 12 amt from dual union all
  5  select 1, 120, 93 from dual union all
  6  select 1, 125, 31 from dual union all
  7  select 1, 260, 15 from dual union all
  8  select 2, 56 , 16 from dual union all
  9  select 2, 115, 49 from dual union all
10  select 3, 45 , 71 from dual union all
11  select 4, 19 , 11 from dual union all
12  select 4, 16 , 48 from dual union all
13  select 5, 94 , 52 from dual union all
14  select 5, 98 , 47 from dual
15  )
16  select x, case when max(cnt) = 1 then to_char(min(y)) else '?' end y, sum(amt) amt
17    from (
18            select x, y, amt, y_next, case when y - y_next <= 10 then 1 else 0 end y_ind, count(*) over(partition by x,  case when y - y_next <= 10 then 1 else 0 end) cnt
19              from (
20                      select x, y, amt, nvl(lag(y) over(partition by x order by y), y) y_next
21                        from t
22                   )
23         )
24   group by x, y_ind
25*  order by 1,2 desc
SQL> /
         X Y                                               AMT
         1 ?                                               136
         1 260                                              15
         2 56                                               16
         2 115                                              49
         3 45                                               71
         4 ?                                                59
         5 ?                                                99
7 rows selected.
SQL>

Similar Messages

  • How to group value in certain range ?

    Hi all,
    I have an ArrayList of double value (0.143554,0.999741,1.032341,4.976403,5.008995,8.973876,9.006459,
    12.99214,13.03558,16.98777,17.0312,20.98247,21.03674,24.99796,
    25.04136,28.99081,29.06675,32.96105,33.01528)
    and I want to group the value to the closest 5 (starting from the minimum
    value(0) of the list and ending at the maximum value(35) of the list).
    Example :
    0.143554 should become 0 (0.143554 < 0.5)
    0.999741 should become 5 (0.5 <= 0.999741 < 5.5)
    1.032341 should become 5 (0.5 <= 1.032341 < 5.5)
    4.976403 should become 5 (0.5 <= 4.976403 < 5.5)
    5.008995 should become 5 (0.5 <= 5.008995 < 5.5)
    8.973876 should become 10 (5.5 <= 8.973876 < 10.5)
    9.006459 should become 10 (5.5 <= 9.006459 < 10.5)
    12.99214 should become 15 (10.5 <= 12.99214 < 15.5)
    13.03558 should become 15 (10.5 <= 13.03558 < 15.5)
    16.98777 should become 20 (15.5 <= 16.98777 < 20.5)
    17.0312 should become 20 (15.5 <= 17.0312 < 20.5)
    20.98247 should become 25 (20.5 <= 20.98247 < 25.5)
    21.03674 should become 25 (20.5 <= 21.03674 < 25.5)
    24.99796 should become 25 (20.5 <= 24.99796 < 25.5)
    25.04136 should become 25 (20.5 <= 25.04136 < 25.5)
    28.99081 should become 30 (25.5 <= 28.99081 < 30.5)
    29.06675 should become 30 (25.5 <= 29.06675 < 30.5)
    32.96105 should become 35 (30.5 <= 32.96105 < 35.5)
    33.01528 should become 35 (30.5 <= 33.01528 < 35.5)
    How could I do that..? Thanks
    Edited by: azlicn on Jun 8, 2008 9:03 PM

    0.143554 should become 0 (0.143554 < 0.5)
    0.999741 should become 5 (0.5 <= 0.999741 < 5.5)
    1.032341 should become 5 (0.5 <= 1.032341 < 5.5)
    4.976403 should become 5 (0.5 <= 4.976403 < 5.5)
    5.008995 should become 5 (0.5 <= 5.008995 < 5.5)
    8.973876 should become 10 (5.5 <= 8.973876 < 10.5)
    9.006459 should become 10 (5.5 <= 9.006459 < 10.5)
    12.99214 should become 15 (10.5 <= 12.99214 < 15.5)
    13.03558 should become 15 (10.5 <= 13.03558 < 15.5)
    16.98777 should become 20 (15.5 <= 16.98777 < 20.5)
    17.0312 should become 20 (15.5 <= 17.0312 < 20.5)
    20.98247 should become 25 (20.5 <= 20.98247 < 25.5)
    21.03674 should become 25 (20.5 <= 21.03674 < 25.5)
    24.99796 should become 25 (20.5 <= 24.99796 < 25.5)
    25.04136 should become 25 (20.5 <= 25.04136 < 25.5)
    28.99081 should become 30 (25.5 <= 28.99081 < 30.5)
    29.06675 should become 30 (25.5 <= 29.06675 < 30.5)
    32.96105 should become 35 (30.5 <= 32.96105 < 35.5)
    33.01528 should become 35 (30.5 <= 33.01528 < 35.5)You just wrote the logic right there, put it in a switch statement and there ya have it.

  • Group by with date range.

    Hi,
    I am looking for effective usage of Group by against date range.
    I have a transaction table as below.
    Date            customer_no      amount_paid
    01-Dec-13     001                  500
    02-Dec-13     001                  360
    09-Dec-13     001                  200
    02-Nov-13     001                  360
    09-Nov-13     001                  200
    02-Nov-13     001                  360
    09-Oct-13     001                  200
    02-Oct-13     001                  360
    09-Oct-13     001                  200
    02-Sep-13     001                  360
    09-Sep-13     001                  200
    ............... etc.
    I would like to see sum(amount_paid) by past date ranges 1-30 days, 31-60 days, 61-90 days.
    Below are expected results.
    Customer          Duration       amount_paid
    001                    1-30             980
    001                    31-60           450
    001                    61-90          1200
    002                    1-30             300
    002                    31-60           490
    002                    61-90           320
    003                    1-30             450
    ......................etc.
    I have to group by customer no and date range (1-30, 31-60, 61-90..etc).
    Can someone help me getting query for this.
    Thanks...
    Sreeram.

    SQL> with t
      2  as
      3  (
      4  select to_date('01-Dec-13', 'dd-Mon-rr') dt, '001' customer_no, 500 amount_paid from dual
      5    union all
      6  select to_date('02-Dec-13', 'dd-Mon-rr') dt, '001' customer_no, 360 amount_paid from dual
      7    union all
      8  select to_date('09-Dec-13', 'dd-Mon-rr') dt, '001' customer_no, 200 amount_paid from dual
      9    union all
    10  select to_date('02-Nov-13', 'dd-Mon-rr') dt, '001' customer_no, 360 amount_paid from dual
    11    union all
    12  select to_date('09-Nov-13', 'dd-Mon-rr') dt, '001' customer_no, 200 amount_paid from dual
    13    union all
    14  select to_date('02-Nov-13', 'dd-Mon-rr') dt, '001' customer_no, 360 amount_paid from dual
    15    union all
    16  select to_date('09-Oct-13', 'dd-Mon-rr') dt, '001' customer_no, 200 amount_paid from dual
    17    union all
    18  select to_date('02-Oct-13', 'dd-Mon-rr') dt, '001' customer_no, 360 amount_paid from dual
    19    union all
    20  select to_date('09-Oct-13', 'dd-Mon-rr') dt, '001' customer_no, 200 amount_paid from dual
    21    union all
    22  select to_date('02-Sep-13', 'dd-Mon-rr') dt, '001' customer_no, 360 amount_paid from dual
    23    union all
    24  select to_date('09-Sep-13', 'dd-Mon-rr') dt, '001' customer_no, 200 amount_paid from dual
    25  )
    26  select customer_no
    27       , ((grp_val - 1) * 30) + 1 start_val
    28       , grp_val * 30 end_val
    29       , sum(amount_paid) amount_paid
    30    from (
    31            select dt
    32                 , customer_no
    33                 , amount_paid
    34                 , ceil(sum(dt_interval) over(partition by customer_no order by dt)/30) grp_val
    35              from (
    36                      select dt
    37                           , customer_no
    38                           , amount_paid
    39                           , nvl(dt - lag(dt) over(partition by customer_no order by dt), 1) dt_interval
    40                        from t
    41                   )
    42         )
    43   group
    44      by customer_no
    45       , grp_val
    46   order
    47      by grp_val;
    CUS  START_VAL    END_VAL AMOUNT_PAID
    001          1         30         560
    001         31         60         760
    001         61         90         920
    001         91        120        1060
    SQL>

  • Discrete time range over date range  (using CR X1)

    Post Author: sjr
    CA Forum: Formula
    Can anyone show me how to select data over a discrete time range i.e. 6 p.m. till 11 p.m. per day, over a period of a month ?
    Using the data time parameter gives me data from 6p.m. on startdate right through till 11p.m. on endate.

    Here's a more detailed description...
    Create two formula fields to split the date and time from the data records (ds prefixed):
    dsDate
    dsTime
    Create two formula fields to split the date and time from the parameter fields (pm prefixed):
    pmStartDate
    pmStartTime
    pmStartDate
    pmStartTime
    Use your new formula fields in the record selector. Get to this off the Report menu button (in CR 2008):
    Report | Select Expert... | Record.
    You can pick the formula fields in the Forumula Editor to create something like:
    ({@dsDate} > {@pmStartDate} and
    {@dsDate} < {@pmEndDate}) and
    ({@dsTime} > {@pmStartTime} and
    {@dsTime} < {@pmEndTime)
    Tim

  • How do I start over with my mac book pro

    I used disk utility and found my hard drive to be corrupt. How do I start over and clean this up?

    Backup your Users data off the computer to regular storage drive (not TimeMachine), hold c boot off the 10.6 and use Disk Utility to Erase with Zero the entire drive of everything, this will map off any bad sectors and if you have enough spares, the drive shoudl work fine again.
    Reinstall OS X, setup with the same user name, Software Update until clear, install your programs from fresh sources and return files from the storage drive, don't use TM to restore it's also corrupted image, just have to start all over again.
    Read more, especially the section about bad sectors
    https://discussions.apple.com/docs/DOC-3044
    https://discussions.apple.com/docs/DOC-3045
    https://discussions.apple.com/docs/DOC-3046
    https://discussions.apple.com/docs/DOC-3172
    https://discussions.apple.com/community/notebooks/macbook_pro?view=documents

  • I have purchased my first smart phone. iphone 5. I want to use sounds sent to me in text messages as ringtones but don't know how to move them over. Any help would be appreciated.

    I have purchased my first smart phone, an iphone 5.  I would like to use some of the sounds from text messages for ringtones but don't know how to move them over.   Any help would be appericated.

    I'm sorry, had you already pointed out the rules which stated that lines from 2 separate accounts were eligible??? I don't see anywhere in the advertisement specifically stating that lines from 2 separate accounts would be eligible, so THAT is not what's advertised, either.
    As far as I know Verizon's BOGO offers have ALWAYS been for lines on the same account. Possibly someone could chime in where they have received this offer on 2 separate accounts? The ONLY exception to this was someone getting the offer with 2 eligible lines on their account, reactivating the old phone on one of the lines and then giving one of the new phones to someone on another account.
    Sorry if you think simply because someone disagrees with you, they are being condescending. That is certainly not the case. Good luck.

  • I have to create a new iTunes account as I cannot remember the security question answers to my current account, how do I move over my purchases from my current account to my new account?

    I have to create a new iTunes account as I cannot remember the security question answers to my current account, how do I move over my purchases from my current account to my new account?

    fayry wrote:
    ... how do I move over my purchases from my current account to my new account?
    This is not possible...
    Anything Downloaded with a Particular Apple ID is tied to that Apple ID and Cannot be Merged or Transferred to a Different Apple ID.
    Apple ID FAQs  >  http://support.apple.com/kb/HE37

  • How can I show a 0% range in the data value label on a bar chart thanks?

    How can I show a 0% range in the data value label on a bar chart thanks?

    I'm not sure what the question is. 
    I know that if you have a bar chart and one of the categories (X-axis) has bar (Y value) equal to 0%, no bar is plotted for that category. Even the addition of a stroke (line) around the bars doesn't make one appear for 0%.  The only automatic way I know of to make it look like there is data in that category is to add the value labels to the bars. Inspector/Chart/Series, select one of the bars on the chart, click on "value labels". Another method that is a workaround is to fudge the number a little in your table so that instead of 0% it is a very small %.  This will get you a thin line on the chart.
    But if your question is about the value labels (the numbers that display on or in the bars) and you are not getting one for a bar that is supposed to be 0%, it probably means your table doesn't actually have a 0% in the corresponding cell. A blank cell in the table will not get a value label.

  • HT1382 How can I copy over my contact list from my ipod touch 3 to my new ipod touch 5 ?

    How can I copy over my contact list from my ipod touch 3 to my new ipod touch 5 ??

    Sync it with iTunes on your computer.

  • How to find out the Number range object for Incident number

    How to find out the Number range object for Incident number ?
    CCIHT_IAL-IALID
    regards,
    lavanya

    HI, an example.
    data: vl_num type i,
          vl_char(6) type c,
          vl_qty type INRI-QUANTITY,
          vl_rc type INRI-RETURNCODE.
    CALL FUNCTION 'NUMBER_GET_NEXT'
      EXPORTING
        NR_RANGE_NR                   = '01'
        OBJECT                        = 'ZRG0000001'
       QUANTITY                       = '1'
      SUBOBJECT                     = ' '
      TOYEAR                        = '0000'
      IGNORE_BUFFER                 = ' '
    IMPORTING
       NUMBER                        = vl_num
       QUANTITY                      = vl_qty
       RETURNCODE                    = vl_rc
    EXCEPTIONS
       INTERVAL_NOT_FOUND            = 1
       NUMBER_RANGE_NOT_INTERN       = 2
       OBJECT_NOT_FOUND              = 3
       QUANTITY_IS_0                 = 4
       QUANTITY_IS_NOT_1             = 5
       INTERVAL_OVERFLOW             = 6
       BUFFER_OVERFLOW               = 7
       OTHERS                        = 8
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    vl_char = vl_num.
    write vl_char.
    Regard

  • HT4889 Replacing System hard drive with a new one. How to get everything over to the new boot drive?

    Replacing System hard drive with a new one. How to get everything over to the new boot drive? Should I use Carbon Copy or does apple have a better untility to do this?
    I can't get my current system drive (OSX 10.8.3) to start on the first try. I always have to shut down and restart again to finally see the Apple logo.
    Have used disc utility to repair the disc and permissions several times and that works. The next time I boot up, it works fine and I get the apple logo, but then the second time I boot up, it's back to the blank screen again and it only boots after the second try.  I have tried this repair three different times now always with the same result. Works right the first try (after the repair) then from the second time on it doesn't work. I just get the white screen until I reboot a second time.
    Thinking I should change drives but what's the easist and best way to move everything over to the new drive so it will boot correctly with all my data on it. This is the system drive for a Pro Tools 10HD setup. MacPro 3,1 with 16 gigs ram and OSX10.8.3 on it.
    Thanks for any help!

    If you have a time machine back up of your current drive you can do this
    Shut down your computer, install the new drive. While the computer is off plug in the external hard drive that you have your time machine back up on. Hold Option key while the computer turnes on, let go of the option key once you get a grey screen. Shortly after you'll see  a list of bootable drives, select the one that has your time machine back up on it and boot into that drive.
    From there go into disk utility, format your new drive too, osx extended journaled ( I think, double check that, its been awhile since ive had to do this), hit format
    Exit disk utility and then you can use time machine to copy all your exisit data to the new hhd and then your pretty much done.
    There is also a program called Carbon Cloner that will do esentially the same thing however I've never uesed it.

  • How can i loop over treeview selected nodes and then to delete each node if it's a directory or a file ?

    I have this code:
    if (s == "file")
    file = false;
    for (int i = 0; i < treeViewMS1.SelectedNodes.Count; i++)
    DeleteFile(treeViewMS1.SelectedNode.FullPath, file);
    I know it's a file and it is working if it's a single file.
    On the treeView i click on a file right click in the menu i select delete and it's doing the line:
    DeleteFile(treeViewMS1.SelectedNode.FullPath, file);
    And it's working no problems.
    But i want to do that if i selected some files togeather multiple selection then to delete each file.
    So i added the FOR loop but then how do i delete from the SelectedNodes each node ?
    The treeView SelectedNodes dosen't have FullPath like SelectedNode also doing SelectedNodes[i] dosen't have the FullPath property.
    Same as for if i want to delete a single directory or multiple selected directories:
    This is the continue of the code if it"s not a "file" (else) it's null i know it's a directory and doing:
    else
    file = true;
    RemoveDirectoriesRecursive(treeViewMS1.SelectedNode, treeViewMS1.SelectedNode.FullPath);
    Also here i'm using SelectedNode but if i marked multiple directories then i how do i loop over the SelectedNodes and send each SelectedNode to the RemoveDirectoriesRecrusive method ?
    My problem is how to loop over SelectedNode(multiple selection of files/directories) and send each selected file/directory to it's method like i'm doing now ?

    foreach (TreeNode n in treeViewMS1.SelectedNodes)
    // Remove everything associated with TreeNode n here
    I don't think it's any harder than that, is it?
    If you can multi-select both an item and one of its descendents in the tree, then you'll have the situation that you may have deleted the parent folder and all of its children by the time you get around to deleting the descendent.  But that's not such
    a big deal.  A file can get deleted externally to your program too - so you'll just have to deal with it having been deleted already (or externally) when you get around to deleting it yourself.

  • How to use SQL OVER and PARTITION BY in OBIEE Expression Builder??

    Hi there,
    I want to create a new logical coulmn with the following SQL query.
    SUM(Inventory Detail.Qty) OVER(PARTITION BY Inventory Detail.A,Inventory Detail.B,Item.C,Inventory Detail.D,MyDATE )/SUM(Inventory Detail.Qty) OVER(PARTITION BY Inventory Detail.A,Inventory Detail.B,Item.C )
    How to use the OVER and PARTITION BY in OBIEE Expression Builder??
    Thanks in Advance

    hi bipin,
    We cant use by in Expression builder(rpd) .But use the same formula like this in Fx of answers
    SUM(Inventory Detail.Qty) OVER(PARTITION BY Inventory Detail.A,Inventory Detail.B,Item.C,Inventory Detail.D,MyDATE )/SUM(Inventory Detail.Qty) >OVER(PARTITION BY Inventory Detail.A,Inventory Detail.B,Item.C )SUM(Inventory Detail.Qty by Detail,ITEM,Mydate)/SUM(qty by detail,item)
    First check the numerator whether it was giving correct results or not then go with denominator
    compare the results with sql that u have
    Let me know if that does work
    thanks,
    saichand.v
    Edited by: Saichand Varanasi on Jul 27, 2010 9:27 PM
    Edited by: Saichand Varanasi on Jul 27, 2010 9:28 PM

  • How to group photos in folders, in the main photos app in my iphone4 ?

    How to group photos in folders, in the main photos app in my iphone4 ?

    You can update your iPhone to iOS 5, in iOS 5, it will allow you to create albums in your photo app and it also allows you to move the photos from album to another album

  • How to get the Vendor Sub Range details

    Hi
    Can you please let me know, how to get the vendor sub range from SAP tables?
    Thanks
    RK

    Hi,
    For Vendor Sub-Range table, please refer to Table WYT1.
    Regards,
    Nazrin

Maybe you are looking for