Grouping by time buckets

I have an sql that does some counts by hour for the last 24 hours. I wanted to change this to use for the last 4 hours but instead of 24 buckets I wanted 16 buckets of 15 minutes each.
For the last 4 hours in hour buckets seem to work fine. But for the last 4 hours in 16 buckets, doesn't work fine as it seems to give me the totals for the 1 bucket and repeats the same for the rest of the 15. Could some look let me know what I'm doing wrong here, thanks.
create table test(dat date, ct1 number, ct2 number);
declare
begin
FOR i IN 1 .. 16
LOOP
insert into test values((sysdate - 4/24)+i*1/96, i, i*2);
commit;
END LOOP;
end;
with dt_minmax as (SELECT trunc(sysdate-4/24,'hh24') AS min_dt,
               trunc(sysdate,'hh24') AS max_dt
          FROM DUAL),
     dense_days AS
     (SELECT     min_dt + LEVEL / 24 AS dt, min_dt, max_dt, level lvl
            FROM dt_minmax
      CONNECT BY LEVEL <=  4)
SELECT   dt AS period,
         SUM (s.ct1 + s.ct2) AS BYTES,
         COUNT (TRUNC (s.dat, 'HH24')) AS activity
    FROM dense_days dd LEFT OUTER JOIN test s ON dd.dt = TRUNC (s.dat, 'HH24')
GROUP BY dd.dt
ORDER BY dd.dt;
PERIOD                    BYTES   ACTIVITY
15-Dec-2007 23:00:00         66          4
16-Dec-2007 00:00:00        114          4
16-Dec-2007 01:00:00        162          4
16-Dec-2007 02:00:00         48          1
with dt_minmax as (SELECT trunc(sysdate-4/24,'hh24') AS min_dt,
               trunc(sysdate,'hh24') AS max_dt
          FROM DUAL),
     dense_days AS
     (SELECT     min_dt + LEVEL / 96 AS dt, min_dt, max_dt, level lvl
            FROM dt_minmax
      CONNECT BY LEVEL <=  16)
SELECT   dt AS period,
         SUM (s.ct1 + s.ct2) AS BYTES,
         COUNT (TRUNC (s.dat, 'HH24')+lvl/96) AS activity
    FROM dense_days dd LEFT OUTER JOIN test s ON dd.dt = (TRUNC (s.dat, 'HH24')+lvl/96)
GROUP BY dd.dt
ORDER BY dd.dt;
PERIOD                    BYTES   ACTIVITY
15-Dec-2007 22:15:00         18          3
15-Dec-2007 22:30:00         18          3
15-Dec-2007 22:45:00         18          3
15-Dec-2007 23:00:00         18          3
15-Dec-2007 23:15:00         18          3
15-Dec-2007 23:30:00         18          3
15-Dec-2007 23:45:00         18          3
16-Dec-2007 00:00:00         18          3
16-Dec-2007 00:15:00         18          3
16-Dec-2007 00:30:00         18          3
16-Dec-2007 00:45:00         18          3
16-Dec-2007 01:00:00         18          3
16-Dec-2007 01:15:00         18          3
16-Dec-2007 01:30:00         18          3
16-Dec-2007 01:45:00         18          3
16-Dec-2007 02:00:00         18          3
16 rows selected.

I'm not sure if ntile would work in this case, since ntile puts them into equal number of buckets. But we want them to be divided by time range as in 15 minutes of 16 buckets fro 4 hours. But if there are more number of records in say one 15 minute cycle, some of these records will be pushed to the next cycle as it tries tp put them in equal buckets.
SQL> SELECT   bucket, dat
    FROM (SELECT NTILE (16) OVER (ORDER BY dat) bucket, dat
            FROM TEST
           WHERE dat >= trunc(sysdate-4/24,'hh24') and dat <= trunc(sysdate,'hh24')) s
GROUP BY bucket,dat
order by bucket;  2    3    4    5    6
    BUCKET DAT
         1 25-Dec-2007 10:32:02
         2 25-Dec-2007 10:32:03
         2 25-Dec-2007 10:32:04
         3 25-Dec-2007 10:32:05
         3 25-Dec-2007 10:38:48
         4 25-Dec-2007 10:47:02
         4 25-Dec-2007 10:53:48
         5 25-Dec-2007 11:02:02
         5 25-Dec-2007 11:08:48
         6 25-Dec-2007 11:17:02
         6 25-Dec-2007 11:23:48
    BUCKET DAT
         7 25-Dec-2007 11:32:02
         7 25-Dec-2007 11:38:48
         8 25-Dec-2007 11:47:02
         8 25-Dec-2007 11:53:48
         9 25-Dec-2007 12:02:02
         9 25-Dec-2007 12:08:48
        10 25-Dec-2007 12:17:02
        10 25-Dec-2007 12:23:48
        11 25-Dec-2007 12:32:02
        11 25-Dec-2007 12:38:48
        12 25-Dec-2007 12:47:02
    BUCKET DAT
        12 25-Dec-2007 12:53:48
        13 25-Dec-2007 13:02:02
        13 25-Dec-2007 13:08:48
        14 25-Dec-2007 13:17:02
        14 25-Dec-2007 13:23:48
        15 25-Dec-2007 13:32:02
        15 25-Dec-2007 13:38:48
        16 25-Dec-2007 13:47:02
        16 25-Dec-2007 13:53:48
31 rows selected.

Similar Messages

  • A simple list with seven time buckets starting from the date report is run.

    Hi All,
    I am new to ABAP.Recently i have planned to write a progam which will help in planning the delivery of scheduled items.For a given sales Org. it will display all the undelivered,delivered items in a simple list and sort them with delivery dates.it will also provide a summary report at material group.For that i have used selection screen(LIKP-VKORG-Obligatory).when i enter VKORG Details i should get all the delivery docs for which delivery is not done or partially done.For this i have taken one more table LIPS
    (LIPS-MATKL,LIPS-MATNR,LIPS-VBELN,LIPS-POSNR,LIPS-WERKS,LIPS-LFIMG,LIPS-MEINS) for tables LIKP and LIPS VBELN is the key field.when i enter VKORG Data in the selection screen ,how can i get the data from LIPS Table.Please explain?
    Also report should be simple list with seven time buckets starting from the date when the report is run.The amount of quantity to be delivered should be displayed under appropriate bucket i.e within the bucket where its delivery date falls in.for ex:If the report is run on Tuesday 15th march 2010 then the start date 1 should be starting date of the week which is Monday 14th March.
    The report will be summarized at Material Group and Material Number.
    Appreciate your help
    Thanks and Regards,
    Shakeer Hussain

    Sorry, sounds too much like a complete requirement you want done for you by the community.
    Please work on it yourself and search for available information before posting specific problems only.
    Thread locked.
    Thomas

  • Key figure display in planning book with respect to Time bucket profile

    Hi,
    I am loading a key figure to planning area from the info cube for the current month. When I review the key figure in planning book with monthly time bucket profile it shows 85 for the current month. In the same planning book with weekly bucket profile, it shows 55 from the current week and future weeks and the remaining 30 goes into the past weeks of the current month.
    How to make the total quantity 85 to show in the current and future weeks only.
    thanks and regards
    Murugesan

    Hi Murugesan,
    Within the Planning Area, the data is stored at the lowest level granularity that you maintain in storage bucket profile. Then during display, system will decide what data to show depending on what kind of time bucket profile you use in the planning view, and the time based disaggregation that you maintain for Key Figure.
    In this below case, what time characteristic do you have in cube? Is it date, week or month?
    If it's date, check how much KF data is maintained on the dates which belong to week which has days both in this month/last month e.g. if I talk about Dec 2011, how much data is stored 1,2,3 & 4 th of Dec, 2011.
    This data would appear in Dec in monthly view, but in week, it would appear in the week starting 28th November.
    If data is maintained in cube in weeks, then you need to calculate how time based disaggregation would show it to you in months.
    If it's months, then you would need to find out how much data would go to the days in the past week of the month.
    The time based disaggregation may be causing you some issues, but in that case, I would not expect 30 out of 85 to go in the past week, unless you have data in cube in days.
    Data shown in weekly view for week starting 28th Nov should ideally be a small proportion of 85, unless you are using a time stream/fiscal year variant, due to which most of December is in holidays.  The only other exception I can think of is that you have data in teh days mentioned above.
    It would be best to help the business understand this disaggregation logic, rather than thinking of manipulating the data to shift to a later week.
    If this logic doesn't explain your situation, then please provide the date/week/month at which you have data in cube, and what quantity.
    Thanks - Pawan

  • Why a customer or vendor group One Time can't using?

    Why a customer or vendor group One Time can't using with special G/L ? for example Donw payment or chque receipt
    Many thanks for any answer?

    Hi Blue,
    Please refer OSS notes 19638
    Hope this helps.
    Please assign points as way to say thanks

  • Why need to give Function group at time of  table maintence generation

    Dear All,
    why need to give Function group at time of  table maintence generation
    Regards,
    Laxman  Sankhla

    Hi Laxman,
    The F1 help of the field says:
    Name of the function group, to which the generated maintenance modules will belong.
    Means the PBO & PAI modules of the screen will be created under this Functio group.
    Regards
    DKS

  • HT2486 How can you put same contact in a Group multiple times so that you can print the same mailing label multiple times?

    How can you put same contact in a Group multiple times so that you can print the same mailing label multiple times?

    Hello,
    Avery...
    https://discussions.apple.com/thread/2588943?start=0&tstart=0
    Or...
    http://www.belightsoft.com/products/labelsaddresses/overview.php

  • Report grouped by time not grouping by time

    Hi All,
       I have a report with one grouping level of a Time field and have it set to ascending "for each hour". When I run the report, it doesn't group by time instead it repeats by hour (12 am - 12am and then 12 am - 12 am) as if it's grouping by a date level which I do not have in my report. Previously this report had a date group level but I have removed the date grouping from my report.
    Any suggestion why this is happening? I am using CR2008.
    Thank you in advance,

    Are you sure its grouped by hour, go into change group and check, looks like its grouped by AM/PM which is the category next to hour. You might have selected the wrong category by accident.
    Ian

  • Time buckets edit/display mode

    Hi
    We have requirement as first two time buckets of the planning book would be in editable mode to the user and from the 3rd bucket onwards the time buckets should be in display mode.
    How can we make the portion of the future time buckets as editable ?
    Thanks & Regards
    KPN Murthy.
    Edited by: p narasimha murthy konidena on Mar 24, 2011 1:59 PM

    KPN
    Look at the SAP help here:
    http://help.sap.com/saphelp_scm70/helpdata/EN/4b/755bee3bf75a18e10000000a421937/frameset.htm
    Look at SAP standard Macros in Macro book 9AEXAMPLES.
    Rishi Menon

  • Time bucket not displaying in planning book

    When I opened planning book, the time bucket is not displaying as it used to be. I tried to select CVC to be displayed in the planning book and got the following error message:
    @5C\QError@     Insufficient information about read period transferred
    @5C\QError@     Error reading data - Planning book cannot be processed further
    Insufficient information about read period transferred
    Message no. /SAPAPO/TSM231
    This internal error should not occur during normal processing. However if this message is displayed, contact your system administration or the SAP hotline quoting the message number.
    Any idea what went wrong?

    Hi Nic,
    Either someone deleted the time bucket profile, or else the periodicities in the time bucket profile are not in line with the periodicity allowed in your planning area.
    Go to Advanced Planning and Optimization-> Demand Planning -> Environment -> Current Settings -> Maintain Time Buckets Profile for Demand Plng and Supply Network Plng, or directly use the tcode "/SAPAPO/TR30".
    Check that your time bucket profiles ZDP_PBP_9Q & ZDP_PBP_5Q exist by using F4 help, and also select them to see what periods are used in the time bucket profile e.g. weeks, days, etc.
    If you don't find the time bucket profile, then they need to be created (based on names, my guess is that *9Q will contain 9 quarters, and *5Q will contain 5 quarters). I expect that issue lies here itself.
    If time buckets profile exist, then check the following:
    1) Go to /sapapo/msdp_admin, select your planning area. See the storage bucket profile name in the "info" tab.
    2) Go to /SAPAPO/TR32  (Periodicities for Planning Area). Give the name of the Storage bucket profile that you see in 1.
    3) Check what periods are used in storage bucket profile.
    4) Storage bucket profile needs to have lower periods maintained than the time bucket profile. e.g. assuming that your time bucket profile has quarters, then you need to have at least quarter maintained in storage bucket profile. Even if you have day or week or month selected in storage bucket profile, it's ok because these are smaller periods than quarters what you have in time bucket profile.
    Also make a note if "post period" is checked. Note the name of "fiscal year variant" if that is maintained along with this tick. I hope you don;t need to go further but if above don;t resolve your issue, then we need to check the fiscal year variant.
    Thanks - Pawan

  • Invalid time buckets profile reference

    Hi :
    I am getting fololowing error while loading data in planning book : " Invalid time buckets profile reference", and unable to laod any data  to planning book
    I have already done consistency check , initailised version , checked time bucket profile , storage bucket profile , and those are fine .There are no time stream id defined either .

    Hi Virendra,
          Check whether your time bucket profile falls same under storage bucket profile. I mean, check your fiscal year variant in storage bucket profile as well as in Time bucket profile. becz you have assigned the storage bucket profile to planning Area, which having fiscal year variant & if you are using different fiscal year variant in Time bucket profile, this mentined error will appear.

  • Unable to save Time Buckets Profile for SNI

    I am getting this weird behavior with the Time Buckets Profile in SNI.
    Whenever I Edit and Save the changes, the Time Bucket Profile reverts back to Years 0000, Months 0000, Weeks 0000 and so on.
    I am using SCM 7.0 with the SP 06.
    I have maintained the necessary Visibility Profiles for the relevant applications.
    It was working until a couple of days and stopped from today. We recently added a custom view under SNI Details.
    Any thoughts?
    Kedar

    Amit
    this is not a Time bucket profile but a storage bucket profile
    you cannot create or maintain it... its standard SAP structure for storing data in technical buckets
    the use of this bucket is in interactive planning book if you want to see how the data is stored(which you seem to have used). This is useful , say, in explaining how the weeks overlapping two months has got the quantity. Again this is a temporary setting and cannot be saved in the planning book
    SCM5.0 makes this a bit accessible by giving this as an option in the drop down icon for "period settings"
    for a related thread see
    Weekly Buckets Incorrectly Split into incomplete weeks!?!?

  • DP time bucket conversion

    Hello,
    For statistical reason, our statistical forecast is made with monthly time bucket (Gregorian calendar). The business requirement is to have converted the results in fiscal period month. After conversion based on the DP logic we are loosing 5-7 % from our accuracy.
    How we can fix that difference?
    Any idea would be appreciated.
    Thanks, Marius  

    Marius,
    If you are forecasting at an aggregated level (most companies do), and you have chosen the conversion method of disaggregating months down to days, and then re-aggregating back up to fiscal periods, then 5-7% difference is not unreasonable.  You might be able to achieve better precision by tweaking a few things, or by creating a custom ABAP conversion routine.
    Seriously, though, how much effort do you really want to spend on this?  High levels of precision in a forecast are generally inconsequential.  Remember, it is just a forecast, which is a prediction of the future.  When you consider the most common forecast KPI, ( forecast vs actual), a world class forecasting process will only yield 80% accuracy.  In many companies, using a forecast that is 'imprecise' by 5% is generally not worth the time and money to improve.
    Best regards,
    DB49

  • When AD users accessed the group last time

    Suppose We have a Group 'A' which is under 'B' domain in Active Directory. 'B' domain is used by many applications.
    we have a website in which validations are done thro Acitve directory. That is it will allow to enter the site if that user is added to group 'A' under 'B' domain.
    I created a .NET application and it will fetch who all are having access to website & the user details from the AD. Last access date is not showing correctly.
    Tool will shows last logon date as today’s
    date & time when I logon to website now.
    If we are
    not accessed the website today but we connected to the system using 'B' domain today, then tool will display the last logon time as today’s date.
    Using .NET is there any way to filter the last accessed date for a group in a particular domain? Anyone can provide me the C#.NET code will be appreciated

    Please check this thread
    http://forums.asp.net/t/1274419.aspx?Get+Last+Logon+date+from+the+active+directory+using+C+
    There are two attributes for this in Active Directory:
    lastLogon refers to the last logon for the specific server you're querying. This date may be different for different servers (domain controllers), and for some it may be null/empty. This attribute is not replicated.
    lastLogonTimestamp refers to the last logon for all servers. This attribute is replicated, which means that all servers have the same value. Only problem is that the default synchronization is 14 days on this attribute which may be too long.

  • Why do I lose my tab apps and tab groups every time firefox updates to a new version?

    It seems like every time I get a new version of firefox my tab apps and groups disappear. Why does this happen and what can I do about it?

    Hi,
    My settings are as described and have been for a long time and number of versions. However whenever there is a update to firefox everything goes, including app tabs and other opened tabs.
    I think something should be done about this as it's very frustraighting to have to start again setting evverything up, and remembering everything I had pinned & open whenever there is an update.

  • Help: SQL to select closest points within groups of records grouped by time

    Hi,
    I need help to find an SQL for efficient solution for the following problem:
    - I have 20 buses circling on one bus route and their locations are reported every 10 seconds and recorded into a spatial table “bus_location” with “bus_loc” as SDO_GEOMETRY type.
    - Each bus location record there has a msg_datetime column with the time when the location was recorded.
    - Buses circle the bus route 8-12 times, from A to B, then from B back to A, then new loop from A to B, and so on.
    - I have 3 timing points in spatial table “timing_point” with “tp_loc” as SDO_GEOMETRY type.
    - A bus will pass each timing point 8-12 times in one direction and 8-12 times in the other direction, while making loops on the bus route.
    My task is: for each timing point, for each bus, for each trip direction (A-B or B-A), find the closest bus location to that timing point.
    Example:
    Bus...*TimingPoint*......*Time*.....*Distance*
    ...........................................*between*
    ...........................................*bus and TP*
    12......A................14:01:00......250 m
    12......A................14:01:10......150 m
    12......A................14:01:20......12 m <== This is the record closest to the TP for this pass
    12......A................14:01:30......48 m
    12......A................14:01:40......100 m
    12......A................14:01:50......248 m
    12......A................14:29:40......122 m
    12......A................14:29:50......72 m
    12......A................14:30:00......9 m <== This is the record closest to the TP for this pass
    12......A................14:30:10......10 m
    12......A................14:30:20......12 m
    I tried to use SDO_NN and I can get closest bus locations, but how to find the closest bus location for each pass? Or how to identify a groups of bus locations which are together within 2-3 minutes and to find closest record for each of these groups of locations?
    Thank you very much for any help.
    Milan
    Edited by: mburazor on 9-Feb-2012 2:41 PM

    Milan,
    Yes, the problem is one of analytics not spatial. Here is another go that should be independent of year/month/date/hour/minute
    as it does all its math based on an absolute number of minutes since 1970. I round to 5 minutes but you could round/trunc to any
    particular interval (5 minutes, 10 minutes, 15 minutes etc)
    Note that I have created extra records in the same table that I built. (Tip: good idea to provide a sample dataset to the forum when
    asking difficult SQL questions like this.)
    drop table buslocns;
    create table buslocns(
    busno number,
    timingpt varchar2(1),
    pttime   timestamp,
    distance number)
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:00','YYYY/MM/DD HH24:MI:SS'),250);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:10','YYYY/MM/DD HH24:MI:SS'),150);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:20','YYYY/MM/DD HH24:MI:SS'),12);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:30','YYYY/MM/DD HH24:MI:SS'),48);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:40','YYYY/MM/DD HH24:MI:SS'),100);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:50','YYYY/MM/DD HH24:MI:SS'),248);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:29:40','YYYY/MM/DD HH24:MI:SS'),122);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:29:50','YYYY/MM/DD HH24:MI:SS'),72);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:00','YYYY/MM/DD HH24:MI:SS'),9);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:10','YYYY/MM/DD HH24:MI:SS'),10);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:20','YYYY/MM/DD HH24:MI:SS'),12);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:59:40','YYYY/MM/DD HH24:MI:SS'),53);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:59:50','YYYY/MM/DD HH24:MI:SS'),28);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:00:00','YYYY/MM/DD HH24:MI:SS'),12);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:00:10','YYYY/MM/DD HH24:MI:SS'),73);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:44:40','YYYY/MM/DD HH24:MI:SS'),53);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:44:50','YYYY/MM/DD HH24:MI:SS'),28);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:45:00','YYYY/MM/DD HH24:MI:SS'),12);
    insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:45:10','YYYY/MM/DD HH24:MI:SS'),73);
    commit;
    with tensOfMinutes as (
    select row_number() over (order by busno,timingpt, pttime) as rid,
           busno,
           timingpt,
           pttime,
           round(((cast(pttime as date) - cast('01-JAN-1970' as date)) * 1440) / 5) * 5 as mins,
           distance
      from BUSLOCNS
    select busno,timingpt,to_char(to_date('01-JAN-1970','DD-MON-YYYY') + ( mins / 1440 ),'DD-MON-YYYY HH24:MI:SS') as pttime,minDist
      from (select busno,timingpt,mins,min(distance) as minDist
              from tensOfMinutes a
             group by a.busno, a.timingpt, a.mins
             order by 1, 2, 3 ) ;
    -- Result
    BUSNO                  TIMINGPT PTTIME               MINDIST
    12                     A        10-FEB-2012 14:00:00 12
    12                     A        10-FEB-2012 14:30:00 9
    12                     A        10-FEB-2012 14:45:00 28
    12                     A        10-FEB-2012 15:00:00 12
    12                     A        10-FEB-2012 15:45:00 12regards
    Simon

Maybe you are looking for

  • I Can't Create my jar File

    I have a package called voiceexpert which contains three classes: Disease, DiagnosingEngine and Symptoms which is a JavaBean. Symptoms makes use of Disease and DiagnosingEngine objects. voiceexpert is stored in the directory: c:\diagapp3 I compiled S

  • Hitting deployed weblogic app from a url (weblogic 10.3.4, adf 11.1.1.4)

    Hello, I have an appilcation that I have deployed to weblogic. I test this by logging into the machine where weblogic is installed, opening a browser and entering: http://localhost:7001/incomemax/faces/login (works great) I have an external URL (www.

  • Bell south e mail set up I Pad 2

    How do I set up bell south email on my I pad?

  • Desktop Manager won recognize my BB 8130 PIN

    Hello, i bought a BB off my friend and am going to switch it over to my own plan but when i connect my BB to Desktop Manager it wont Recognize the PIN or the BB. what can i do and were can i go to check if the PIN and any other code/ expires or if th

  • Delete single message in JavaMail

    Is it possible to delete an individual message using JavaMail? I have a group of messages whose Flags.Flag.DELETED == true, but I don't want to 'expunge' all of the messages when I close the Folder (folder.close(true)). Instead, I'd like to only dele