Help with LAG () OVER () and dates

I have the following query right now
  SELECT "DATE",
          "CELL_SITE",
          "LASTV_ATTCNT",
          "LASTV_ATTCNT2",
          "LASTV_BLKCNT",
          "LASTV_DRPCNT",
          "V_ATT_CNT",
          "V_CUST_BLK_CNT",
          "V_DRP_CALL_CNT",
          "LASTD_ATTCNT",
          "LASTD_BLKCNT",
          "LASTD_DRPCNT",
          "D_ATT_CNT",
          "D_CUST_BLK_CNT",
          "D_DRP_CALL_CNT"         
     FROM (  SELECT DATE,
                   CELL_SITE,
                    LAG (SUM (V_ATT_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTV_ATTCNT",
                    LAG (SUM (V_CUST_BLK_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTV_BLKCNT",
                    LAG (SUM (V_DRP_CALL_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTV_DRPCNT",
                    LAG (SUM (D_ATT_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTD_ATTCNT",
                    LAG (SUM (D_CUST_BLK_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTD_BLKCNT",
                    LAG (SUM (D_DRP_CALL_CNT), 24)
                       OVER (PARTITION BY BSM_NM ORDER BY DATE)
                       AS "LASTD_DRPCNT",
                    SUM (V_ATT_CNT) AS "V_ATT_CNT",
                    SUM (V_CUST_BLK_CNT) AS "V_CUST_BLK_CNT",
                    SUM (V_DRP_CALL_CNT) AS "V_DRP_CALL_CNT",
                    SUM (D_ATT_CNT) AS "D_ATT_CNT",
                    SUM (D_CUST_BLK_CNT) AS "D_CUST_BLK_CNT",
                    SUM (D_DRP_CALL_CNT) AS "D_DRP_CALL_CNT"
               FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
              WHERE DATE >
                         (SELECT MAX (DATE) FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)
                       - 2
           GROUP BY DATE, CELL_SITE)
    WHERE DATE >=
             (SELECT MAX (DATE) - NUMTODSINTERVAL (12, 'HOUR')
                FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)What I've noticed is that the LAG function is doing kind of what I want but not exactly. Lets say I have data for hours 12AM, 1AM, 2AM, 3AM, 4AM, 5AM, 6AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM, and that the current hour is 12PM, the LAG function goes back to 12AM, which is what it should be doing.
but lets say I have data for 12AM, 1AM, 2AM, 3AM, 4AM, 5AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM, but am MISSING 6AM (no data for 6AM). The lag function will now go back to 11PM instead of 12AM since the 6AM hour is missing.
if I have data for 12AM, 1AM, 2AM, 3AM, 4AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM and am MISSING 5AM and 6AM, the data will go back to 10PM then and not just 12AM.
Can I prevent this from happening and always just go back 12 hours?
Edited by: k1ng87 on Apr 25, 2013 1:27 PM

Hi,
k1ng87 wrote:
LAG (SUM (V_ATT_CNT), 24)
OVER (PARTITION BY BSM_NM ORDER BY DATE)
AS "LASTV_ATTCNT",
LAG (SUM (V_CUST_BLK_CNT), 24)
OVER (PARTITION BY BSM_NM ORDER BY DATE)
AS "LASTV_BLKCNT",
LAG (SUM (V_DRP_CALL_CNT), 24)
OVER (PARTITION BY BSM_NM ORDER BY DATE)
AS "LASTV_DRPCNT",
Since you want so many columns from the same previous row, it would probably be simpler and more efficient to do a self-join, rather than so many LAG functions.
What I've noticed is that the LAG function is doing kind of what I want but not exactly. Lets say I have data for hours 12AM, 1AM, 2AM, 3AM, 4AM, 5AM, 6AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM, and that the current hour is 12PM, the LAG function goes back to 12AM, which is what it should be doing.You're always passing 24 as the 2nd argument to LAG; that means "Return the value from the 24th precding row." If there's a row for every hour, and the current row is 12PM, then wouldn't 24 rows back would be 12PM yesterday, not 12AM? Maybe you meant to pass 12, not 24, as the second argument to LAG. I'll assume you really want the 24th row rom now on, but the technique is the same whether that number is 24 or 12.
but lets say I have data for 12AM, 1AM, 2AM, 3AM, 4AM, 5AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM, but am MISSING 6AM (no data for 6AM). The lag function will now go back to 11PM instead of 12AM since the 6AM hour is missing.
if I have data for 12AM, 1AM, 2AM, 3AM, 4AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12PM and am MISSING 5AM and 6AM, the data will go back to 10PM then and not just 12AM.
Can I prevent this from happening and always just go back 12 hours?A self-join would handle this automatically: it will look for the matching row, without regard for which other rows might be present or absent.
If you really had to do this with analytic functions, you could use MIN or FIRST_VALUE with a range window:
RANGE BETWEEN  1 + (.5 / 24)  PRECEDING
      AND      1 - (.5 / 24)  PRECEDINGWhen you ORDER BY a DATE column (don't call it "DATE"), the units are 1 day, so 1+(.5/24) days ago is 24.5 hours ago; that is, the window only includes DATES between 24.5 and 23.5 hours ago.
If, for some reason, you really wanted to call LAG, then you could do an outer join to guarantee that a row was present for each of the preceding 24 hours.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002}

Similar Messages

  • Help with CF Reports and Dates

    Can anyone here help me with
    This
    post ? I have been waiting for 3 days and after 4 posts no one
    has been able to answer, please help!! I'm stuck on my project only
    because of this. Thank you very, very much.
    Nelson.

    Sorry, I don't use cfreport so I would not be any help with
    what may be inside the total_sales_report_by_date.cfr template.
    However, shouldn't you be executin your query inside of your .cfm
    emplate, then passing the query result to your report as a query
    parameter within your cfreport tag, rather than passing the date
    parameters in cfreportparam tags?
    Phil

  • Help with SQL Query and dates

    I am trying to return results between a year or 365 days. I need to run this at any time and return a year prior. I'm not getting any results? Any help? THX!
    SELECT
    NAME.ID,
    Activity.UF_2,
    Name.FIRST_NAME,
    Name.LAST_NAME,
    Name.EMAIL,
    Activity.TRANSACTION_DATE
    FROM
    Activity INNER
    JOIN Name
    ON Activity.ID
    = Name.ID
    Where
    activity.TRANSACTION_DATE
    BETWEEN
    CAST(CAST(DATEADD(DAY,
    -365,
    GETDATE())
    AS
    DATE)
    AS
    DATETIME)
    AND
    DATEADD
    (SECOND,
    -1,
    DATEADD(DAY,
    1,
    CAST(CAST(DATEADD(DAY,
    -365,
    GETDATE())
    AS
    DATE)
    AS
    DATETIME)))
    and Activity.UF_1
    =
    'a'
    and Activity.UF_2
    =
    'NAT'

    Where activity.TRANSACTION_DATE >= DATEADD(YEAR, -1, GETDATE()) AND activity.TRANSACTION_DATE < GETDATE()

  • Help with relocate master and date metadata

    Hi,
    I run into trouble while trying to relocate my masters.
    Here are 2 pictures taken within a few minute.
    http://rapidshare.com/files/366853944/Archive.zip.html
    IMG_9336.jpg 28.10.07 12:01:29
    IMG_9337.jpg 28.10.07 12:16:18
    Obviously the year of theses pictures is 2007 but when I try to "relocate master" to a folder with the year of the picture, one of them is put in a 2010 (IMG_9336.jpg) folder and the other one in a 2007 folder (IMG_9337) ?
    They are both supposed to be put in a "2007" folder as they were shot in 2007, so why is aperture thinking one of them was shot in 2010 ???
    I am desperate to find a solution as It happens to a bunch of pictures in my library and I can't figure out what is wrong with them!
    Do you experience the same with theses pictures ?
    Hope you understand what I mean and that you can help !
    Dag

    How can I let you download the 2 pictures I have trouble with then?
    I don't see a way to attach them to my post, is there a way to do it ?
    Dag

  • Pie chart with two measures and date dimension navigation not working

    Hi Experts,
    Pie chart with two measures and date dimension navigation not working. Any help is appreciated.
    Thanks
    V

    Hi Deepak,
    I had time dimension in the RPD.
    I have stacked bar chart with same time dim like year & month in the report. when I go to legand and set navigation it is working fine. But not with pie chart.
    I am not not sure what is the problem. When I click on Pie chart it is not navigating to the target report. Can it be any other issues..???

  • Download internal table data into excel sheet with column heading and data

    Hi,
      I am having one internal table with column headings and other table with data.
    i want to download the data with these tables into an excel sheet.
    It should ask the user for file name to save it on their own name. They should give the file name in runtime and it should be downloaded into an excel sheet.
    Can anyone tell what is the right function module for downloading these two internal table data with column heading and data.
    what we have to do for storing the file name in runtime.
    Can anyone help me on this.
    Thanks,
    Rose.

    Hi Camila,
        Try this
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        FILENAME                        = PATH2
       FILETYPE                        = 'XLS'
      TABLES
        DATA_TAB                        = IT_DATA
       FIELDNAMES                      = IT_HEADINGS
    EXCEPTIONS
       FILE_WRITE_ERROR                = 1
       NO_BATCH                        = 2
       GUI_REFUSE_FILETRANSFER         = 3
       INVALID_TYPE                    = 4
       NO_AUTHORITY                    = 5
       UNKNOWN_ERROR                   = 6
       HEADER_NOT_ALLOWED              = 7
       SEPARATOR_NOT_ALLOWED           = 8
       FILESIZE_NOT_ALLOWED            = 9
       HEADER_TOO_LONG                 = 10
       DP_ERROR_CREATE                 = 11
       DP_ERROR_SEND                   = 12
       DP_ERROR_WRITE                  = 13
       UNKNOWN_DP_ERROR                = 14
       ACCESS_DENIED                   = 15
       DP_OUT_OF_MEMORY                = 16
       DISK_FULL                       = 17
       DP_TIMEOUT                      = 18
       FILE_NOT_FOUND                  = 19
       DATAPROVIDER_EXCEPTION          = 20
       CONTROL_FLUSH_ERROR             = 21
       OTHERS                          = 22

  • Screen locked with voice over and zero value

    My ipad screen is locked with voice over and every time I put in my pass code it just repeats and says zero value.  How do I get into my ipad without having to loose all my data.  Have icloud backup but not sure if that is working properly.Frustrated!

    Try triple-clicking the home button and see if that turns it off, and if it does you can then change what a triple-click does via Settings > General > Accessibility > Accessibility Shortcut (or Triple-Click Home depending upon the iOS version).
    If that doesn't turn it off then you can either turn it off directly on the iPad to go into Settings > General > Accessibility and turn VoiceOver 'off'. You need to use a tap-to-select and then double-tap to activate/type process and 3 fingered scrolling e.g. to type a digit of your passcode tap the digit so that it gets a box around it, and then double-tap the digit to type it.
    Or you can do it by connecting to your computer's iTunes (after typing in your passcode via the tap/double-tap process) : select the iPad  in iTunes, select its Summary tab, scroll to the bottom of that and click the 'configure accessibility' button :
    And on the popup select 'none' for the 'seeing' option :
    Clicking 'ok' should turn it 'off' on your iPad

  • I have 2 apple id's with different apps and data saved under each.  It's very annoying so now I want to create a new id with my primary email address I use now.  If I do that is there any way to transfer all my saved apps and app data like game saves etc?

    I have 2 apple id's with different apps and data saved under each.  It's very annoying so now I want to create a new id with my primary email address I use now.  If I do that is there any way to transfer all my saved apps and app data like game saves etc so I don't lose all of that information and can easily switch to a singular apple id?

    Apple does not transfer content bought with one Apple ID to another Apple ID. Apple will not merge two Apple IDs.
    If most of your content was bought with the Yahoo! Apple ID but you now want the Gmail address for your Apple ID, the trick will be to change the address used for the Yahoo ID with the Gmail address. However, to do that you must first free the Gmail address from that other Apple ID. Use the instructions from Apple to substitute another address that is not used as an Apple ID for your Gmail address in the Apple ID with the Gmail address. Then, when the Gmail address is no longer used in an Apple ID, you can use the same instructions to substitute the Gmail address for the Yahoo address in the Apple ID with the Yahoo address.
    Changing the email address you use for your Apple ID -
    http://support.apple.com/kb/HT5621

  • Is it possible to enter events into a specific iCal calendar, then print a list of those events with the name and date on a single sheet?

    Is it possible to enter events into a specific iCal calendar, then print a list of those events with the name and date on a single sheet?  I don't need the calendar grid.  I just want a list of events on a particular calendar, and their dates.  Is that possible? 

    Not easily. Two possibilities:
    1) If all the events have some common feature (eg you have included "XXX" in all the summaries), search for that feature then select and copy the found items at the bottom of the window. You can then paste them into TextEdit or a spreadsheet for printing
    2) You could write an Applescript to locate the events and write them into a text file for printing.
    The first one will probably give you what you want.

  • Need help with Blog, Wiki and Gallery

    Hi Team,
    Need help with Blog, Wiki and Gallery startup. I have newly started visiting forums and quite interested to contribute towards these areas also.
    Please help.
    Thanks,
    Santosh Singh
    Santosh Singh

    Hello Santhosh,
    Blog is for Microsoft employees only. However, you can contribute towards WIKI and GALLERY using the below links.
    http://social.technet.microsoft.com/wiki/
    http://gallery.technet.microsoft.com/

  • What's the phone number I should call for help with my iPhone and ihome dock?

    What's the phone number I should call for help with my iPhone and ihome dock?

    http://www.ihomeaudio.com/support/

  • Help with photoshop quitting and AMD graphics

    help with photoshop quitting and AMD graphics
    im not a techy, but it appears i have to do someting with my amd graphics card - this might be why my software is crashing - ive no idea what to do though

    Hi Chris
    I have tried to go on the website, then i tried to download the automatic detect because i wasnt sure which driver i had or needed - but it has just downloaded a load of game software - which i dont want ( i dont think)
    i have find out my laptop has a amd radeon HD 8750M card, but i dont know what im doing! i would hate to mess my computer up as i am in thailand with no one to help me!
    its frustrating as i am paying for CC but cant use it!

  • Help with converting hex to date and time

    Hi!
    I am using snmp4j libraries to walk the mib tree. The system date is getting returned in 11 hexadecimal octets. I require help in converting it to date and time. I looked up the RFC convention of what each octets represent. Require help with conversion.
    Eg: 07:db:06:0a:29:1d:00:2b:05:1e
    Thanks,

    Octets Contents Range
    1-2 year 0..65536
    3 month 1..12
    4 day 1..31
    5 hour 0..23
    6 minutes 0..59
    7 seconds 0..60
    (use 60 for leap-second)
    8 deci-seconds 0..9
    9 direction from UTC '+' / '-'
    10 hours from UTC 0..13
    11 minutes from UTC 0..59
    For example I just took the time an hour ago it came out as
    07:db:06:13:12:11:1a:00:2b:05:1e
    I have to convert this as a date long value to send it.
    Do I have to write an utility of my own to do it?

  • Help with intricate search and results display

    Hi All,
    I am looking for help with a problem I have, my knowledge on Numbers is limited, I have learnt a lot by trial and error but I do not know where to start on this problem.
    What I am trying to do is display result from sheet 1 onto sheet 2 when I enter the letters of the REFERENCE into cell C3. Below is an example of the result I am tring to achieve -
    SHEET 2
    Reference
    AB
    Reference
    SZone
    Parts
    LZone
    Parts
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB12
    3
    75
    2
    100
    AB12
    3
    75
    2
    100
    AB13
    3
    75
    3
    75
    AB13
    3
    75
    3
    75
    AB14
    3
    75
    3
    75
    AB14
    3
    75
    3
    75
    AB15
    3
    75
    1
    200
    AB15
    3
    75
    1
    200
    AB16
    3
    75
    3
    75
    AB16
    3
    75
    3
    75
    AB21
    3
    75
    3
    75
    AB21
    3
    75
    3
    75
    AB22
    3
    75
    3
    75
    I have searched for AB by entering it in cell B3 on Sheet 2, an auotmatic search has been carried out on Sheet 1 and all the columns have been brought into sheet 2 with the corresponding data and placed under the COLUMN HEADERS. I have over 4000 lines on Sheet 1, although the most result that will ever be pulled through will be 150.
    When the data is pulled in I will need to do other calculation in the COLUMNS F, G and H so the data need to only be mapped to COLUMNS A to E.
    Also I want to be able to use this spreadsheet on my iPad.
    Has anybody got an idea/solution that will help.
    Thanks in advance.
    Ian

    Ian,
    We've had a recent report of troubles with large tables in the iOS version of Numbers, so you may want to consider ways to accomplish your goals without the full 4000-row set of data.
    I re-read the problem statement in your original post and see that I should have known that you were needing a solution for both platforms. But, I'm still not clear on what part you have figured out and what part you still need help with. We often refer to your second table as a "Breakout Table". There are different ways to program it. My favorite is to add a column to the main table that identifies rows that meet the transfer criteria and assigns them a serial number to help determine where they should go in the second table.
    Here's an example:
    In the new column on the right edge of the table T1, the expression is:
    =IF(ISERROR(FIND(T2 :: $A$1, A)), "", COUNT($F$1:F1))
    Note that the first cell in the Aux column is seeded with a zero.
    The expression in the body cells of the second table, T2, is:
    =INDEX(T1, MATCH(ROW()-1, T1 :: $F, 0), COLUMN())
    Note that the search term is to be entered into Cell A1 of table T2.
    Does this give you a start?
    Jerry

  • Help with voice-over on Ipod nano

    I'm totally blind and am using the 60g Ipod Nano with voice-over inabled. I am finding though even though the voice speaks the names of the menus, ie music, albums etc it doesn't speak the names of my albums or songs or tracks. What have I done wrong? I've tried connecting and leaving it to see if any voice tags are made, but nothing has happened. Any help would be welcome, it's driving me mad Many thanks from Lori.

    Try resetting your iPod.
    1. Toggle the Hold switch on and off. (Slide it to Hold, then turn it off again.)
    2. Press and hold the Menu and Center (Select) buttons simultaneously until the Apple logo appears, about 6 to 8 seconds. You may need to repeat this step.
    Tip: If you are having difficulty resetting your iPod, set it on a flat surface. Make sure the finger pressing the Select button is not touching any part of the click wheel. Also make sure that you are pressing the Menu button toward the outside of the click wheel, and not near the center.
    If the above steps did not work, try connecting iPod to a power adapter and plug the power adapter into an electrical outlet, or connect iPod to your computer. Make sure the computer is turned on and isn't set to go to sleep.
    Since you are totally blind, perhaps some one can double check your voice over settings to make sure everything is updated.
    The following information are links:
    iPod nano (5th generation): Enabling and updating VoiceOver
    Using Software Update for Windows to keep your Apple software up-to-date
    Every iPod comes with complimentary, single-incident telephone technical support within 90 days of your iPod purchase. _If you also purchased AppleCare, then your warranty is extended for technical support and hardware coverage for *two years* from the original purchase date of your iPod._

Maybe you are looking for

  • Get file metadata with Reader API

    Hi It is possible to retrieve the metadata of a file with the Reader API? How? Thanks

  • Converting from string to blob

    hi, how can I convert String to blob in java programming

  • Javadoc formatting

    My javadoc output keeps prefixing package names for common Java classes. For example, in a method with return type String it will print java.lang.String. Is there a way to fix this? These fully-qualified class names really clutter up the documentatio

  • Recovering dropped table without using pointing time recovery/

    Hi geeks, Noob here.I have a question*.I want to recover the dropped table with data with out using pointing time recovery.* for Ex:Use hasr dropped the table at 10:00 and realized that he dropped the table at 16:00hrs .So i want to fetch the data(DD

  • Office 365 and Dynamics SL v7sp3

    I have a client that wants to use Office 365 with Dynamics SL v7sp3, does anyone have experience with this configuration and can let me know if any issues? Bob Marlow