Sum over time

Hello

I'm doing something that I think is quite a common request, but after scratching my head and browsing the web I am still not close to a solution.

I am running:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

I'm looking to summarize a set of values, taking into account the two parent grouping and start and end dates.
I use for the parent of grouping:

+ Sum ([value]) over (Partition by order [Parent] [parent], [Child]) +.

I was hoping to expand that AMOUNT to manage also the start and end dates, so the end result would contain a sum of the values for each different period of time.

For example, using the data below I try to sum up the prices of the components of a car over time:

line, product, component, rate, start date, end date
1, car, chassis, 180, 01/01/2000, 31/12/2009
2, car, chassis, 200, 01/01/2010, 01/01/2050
3, car, engine, 100, 01/01/2000, 01/01/2050


Notice that there is a price for "Chassis" of item change, so the output, I'm looking for is:


line, product, component, rate, start date, end date, sum
1, car, chassis, 180, 280 01/01/2000, 31/12/2009,
2, car, engine, 100, 280 01/01/2000, 31/12/2009,
3, car, chassis, 200, 01/01/2010, 01/01/2050, 300
4, car, engine, 100, 01/01/2010, 01/01/2050, 300


But in reality, everything I need is:

Row, product, start date, end date, sum
1, car, 01/01/2000, 31/12/2009, 280
2, cars, 01/01/2010, 01/01/2050, 300

Preference the query would be a view instead of a stored procedure, and he must be able to manage many 'products', 'components' and start and end dates.


All help most appreciated and if need more info, please let me know.

Thank you
Julian

Hi, Julian,.

I think I understand.
You want to use the lump SUM, not the analytical SUM. The analytical function you would give a total for the entire product, but would keep the individual lines for each component. But you don't want individual lines for each component; you want to just one line for each product and the period. The tricky thing with this problem is to set a period. In the following query, a new period for a given product begins when there is a start_date or an end_date for this product, which is calculated in the sub_query called periods.

The following subquery, active_ratews, see what rate applied during this period, seeing the lines in product_rates overlap with the lines in periods. Two overlap if everyone will start before the other ends. If it's not obvious to you (and it is of course obvious to me when I heard it), then look at it this way: two ranges are not overlapping if (and only if) one end before another begins.

The result of the ios active_rates close enough of what you posted as intermediate results. The main request that added all the lines for the same product and timely.

WITH   period_start_points          AS
(
     SELECT  product
     ,     start_date
     FROM     product_rates
    UNION
        SELECT  product
     ,     end_date + 1     AS start_date
     FROM     product_rates
)
,     periods          AS
(
     SELECT  product
     ,     start_date
     ,     LEAD (start_date) OVER ( PARTITION BY  product
                                      ORDER BY      start_date
                           )          AS end_date
     FROM    period_start_points
)
,     active_rates     AS
(
     SELECT     p.product
     ,     p.start_date
     ,     p.end_date - 1     AS end_date
     ,     r.component
     ,     r.rate
     FROM     periods          p
     JOIN     product_rates     r  ON     p.product     = r.product
                       AND     p.start_date     <= r.end_date
                       AND     r.start_date     <  p.end_date
)
SELECT    ROW_NUMBER () OVER ( ORDER BY  product
                             ,           start_date
                    )  AS row_n
,       product, start_date, end_date
,       SUM (rate)             AS total_rate
FROM       active_rates
GROUP BY  product, start_date, end_date
ORDER BY  product, start_date
;

Viguierkevin wrote: in addition, a quick query to improve my ads - how indent without doing ittallic of text, and how to code a different font?

This site normally does not display multiple spaces in a row.
Whenever you post the text formatted (for example, the results of the query) on this site, type the 6 characters:

\

(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.  Everything between the \

tags appear in a police fixed-width.

Published by: Frank Kulash, January 4, 2012 14:09
Added explanation.

Tags: Database

Similar Questions

  • Interview with case/when &amp; sum() over (partition) producing unexpected lines

    Since some time ago, I asked a question in this forum on an unusual problem of join and subtract ( subtract the total periods of highest level duration ). I had what seemed like a work request. So far, I could not really use it. Now that I'm looking closer results, I think that there is something wrong with it, but I can't understand why it's happening.

    Using the definition of the table of the original question, the following simple query shows a special extract of the data I'm looking at.

    Select duration, event_type, start_time, code_range, request_id from MYTABLE where REQUEST_ID = 'abc '.

    This translates into the following lines (separate columns by ' / '):

    START_TIME/REQUEST_ID/EVENT_TYPE/CODE_RANGE/DURATION
    2010-11-12 01:42:04.0/abc/Junk/publicEntryPoint/2,003
    2010-11-12 01:42:04.0/abc/Junk/webServiceCall/947
    2010-11-12 01:42:04.0/abc/Junk/webServiceCall/969

    Another similar request with REQUEST_ID = 'def' means:

    START_TIME/REQUEST_ID/EVENT_TYPE/CODE_RANGE/DURATION
    2010-11-12 00:22:13.0/def/junk/webServiceCall/788
    2010-11-12 00:22:13.0/def/junk/webServiceCall/1,128
    2010-11-12 00:22:13.0/def/junk/publicEntryPoint/2,003

    What follows is an excerpt simplified query I have a problem with:

    Select value start_time request_id event_type, code_range, duration, case code_range
    When "publicEntryPoint" then length * 2 - sum (duration) over (partition by request_id)
    of another-1
    end inner_duration from MYTABLE where EVENT_TYPE = 'spam' and trunc (START_TIME) = to_date('2010-11-12','yyyy-mm-dd')
    and rownum < 1000;

    Notice the couple of unconventional functions used here, the ' case/when' and 'sum() over (partition).

    This property returns a bunch of lines (count: 999), but here are the two with the particular REQUEST_ID values:

    START_TIME/REQUEST_ID/EVENT_TYPE/CODE_RANGE/DURATION/INNER_DURATION
    2010-11-12 01:42:04.0/abc/junk/publicEntryPoint/2,003/2,003
    2010-11-12 00:22:13.0/def/junk/publicEntryPoint/2,003/87

    The second line is correct. The first line has an unexpected value of INNER_DURATION of 2003. It should be 87 as the second row. I don't understand why this is happening.

    I think that 'and rownum '.<1000" must="" be="" the="" problem.="" it="" must="" be="" excluding="" some="" of="" the="" rows="" needed="" for="" the="" sum.="" the="" analytic="" function="" will="" be="" applied="" after="" this="" condition--see="" the="" sql="">

    Analytical functions are the last set of operations performed in a query, except for the
    final ORDER BY clause. Every joint and every WHERE, GROUP BY and HAVING clauses are
    completed before the analytical functions are processed

    Have you tried the query without this condition?

    Kind regards
    Bob

  • Since the installation on my Windows 7 64 - bit installed, Firefox has constantly gotten slow that hour passes, hangs for 10-15 seconds at a time and just gets worse over time with updates and all.

    Since the installation on my Windows 7 64 - bit installed, Firefox has constantly gotten slow that hour passes, hangs for 10-15 seconds at a time and just gets worse over time with updates and all. It was fast when I installed first, but of the six latest mos has slown to a crawl.

    upgrade your browser Firefox 8 and try

  • Can I create a spreadsheet to track events over time?  If so, how?

    Is it possible to use the numbers to track events over time?  Dates would be the only numbers you use.

    Can you give more details on what you are looking to do?  Surely you don't want only a column of dates...

    SG

  • Want a ramp of output voltage over time and measure input 2 analog USB-6008

    Hello

    I want to produce an analog voltage output signal that increases over time with a certain slope, which I'll send in a potentiostat and at the same time I want to read voltage and current (both are represented by a voltage signal) that I want to open a session and ultimately draw from each other. To do this, I have a DAQ USB-6008 system at my disposal.

    Creation of the analogue output with a linear ramp signal I was possible using a while loop and a delay time (see attachment). Important here is that I can put the slope of the linear ramp (for example, 10mV/s) and size level to make a smooth inclement. However when I want to measure an analog input signal he's going poorly.

    To reduce noise from the influences I want for example to measure 10 values for example within 0.1 second and he averaged (this gives reading should be equal or faster then the wrong caused by the slope and the linear ramp step size.) Example: a slope of 10 mV/s is set with a 10 step size. Each 0.1 s analog output signal amounts to 1 mV. Then I want to read the analog input in this 0.1 s 10 values)

    Because I use a timer to create the linear ramp and the analog input is in the same loop, the delay time also affects the analog input and I get an error every time. Separately, in different VI-programs (analog input and output) they work fine but not combined. I searched this forum to find a way to create the ramp in a different way, but because I'm not an experienced labview user I can't find another way.

    To book it now a bit more complicated I said I want to measure 2 input analog (one for the voltage of the potentiostat) signals and one for the current (also represented by a voltage signal) and they should be measured more quickly then the bad of the analog signal. I have not yet started with because I couldn't read on channel work.

    I hope someone can help me with this problem

    An array of index. You want to index the columns for a single channel.

  • Plotting the amplitude of a spectral peak over time

    Hello

    I am creating a VI where the acquired continuously DAQ, plots and save a waveform in millisecond delay (which is already done in this case), then takes this waveform, finds a specific pic (probably the first) and trace the amplitude of this pic over time (+ 30 minutes, one point each scan which is obtained). Essentially, I have a detector quickly attached to a chromatograph, and I want to select a single ion and monitor the amplitude of this ion. I can draw the waveforms along with it in post processing, but I want to do is to have the 'slow' constantly plot to update and display when it moves through time. Joined the VI will go in, using IMS software V1.3.vi, the paragraph is the "GC" Mode it seems I should use the "peak detect.vi.", but I'm not familiar with this operation, and so I don't know how to show a constantly updated graphic or remove the amplitudes and draw. Thank you for your help,

    <><>

    Eric-WSU wrote:

    I get an amplitude on plot of time, but it does not appear until after all the iterations are stopped

    I have not watched your VI (because I'm in a previous version of LabVIEW), but it's probably because your graphic is outside the loop.

    Here's how you can get a graph of the peaks (all vertices, by iteration):

    Or if you want to only a certain PEAK (and how this pic changes with the number of iterations):

  • Hi, I have two decimal strings, each of them contains the numbers change over time. I want to plot them on the graph XY, a string for each axis. Can you show me how do?

    Hi, I have two decimal strings, each of them contains the numbers change over time. I want to plot them on the graph XY, a string for each axis. Can you show me how do?

    This is not a formula, but a constant simple schema of a table complex. Wire remains, then do a right click of the terminal of the initializer of the feedback node and select "create constant. That should do it.

  • full waveform graph to calculate the area under the curve over time

    Hello

    If please find my attached VI and help me please for integration to get area undercut for my chart of waveform.

    I would like to find my wave power wind energy production chart oever within one month of time.

    I think I have to integrate to get the area under the curve and so I m now using tool integrated into labview attached.

    As I did not wait years for my result and so I want to make sure that the tool that I m using is correct.

    Please advise me what method I will use for my chart.

    Dominique says:

    I would like to integrate my table of power over time (table of power is on each interval of a minute) and the entire graph is one month. So, I'll put dt = 1?

    You put dt at some units you want in the final result. If you want Wh, you want dt in hours. If the spacing is currently a minute, you can express in fractional hours and use the result as dt. Try dt = 1/60.

  • Get information from change of color over time

    Hi all

    I am new to LabView and on this forum, so I hope I'm in the right subforum. If this isn't the case, it would be nice if a mod could move this thread.

    My problem is the following:

    Let's say I have a white spot I want to observe with a camera. The color of the spot turns green over time and I want to acquire the photo, as well as to get the color intensity of green increase developing countries over time. I do not know what features I need to use to get there. Can someone me hint in the right direction? I think that it is at least possible in LabView.

    Thank your all for your help.

    Best regards

    Tresdin

    I don't know what the best resource for IMAQdx pilots, I think that the examples that provide OR are pretty useful. Here's a simple VI which will tell you the average value of green of all the pixels in your image.

  • Laptop HP G62-451SA: cannot read the label for Office 2010 product key as it dissipated over time

    Obliged to restore settings after a total crash.

    Spent the whole day as recommended by HP, including using my recovery disk that has failed.

    Tried to open Microsoft Office 2010 as usual, but said to give the product key to activate it.  Mine is on a sticker on the bottom of my laptop.  Unfortunately it is place to be where it is that some of the numbers have disappeared over time.  Now I can read only the last 15 digits.

    Ages worn on Microsoft support pages and basically they said that the onus to solve this problem since I legally paid for it when buying their product to HP.

    I'll lose more than a year of work, if I can't get this sort.

    Thank you.

    The label on the bottom of your laptop is not for the office. It's your Windows license key. It should say Windows 7 Home Premium above the numbers.

    Most of the HP machines come with a trial of office unless order you it with more full Office and pay extra. The trial usually actually works after the period 60 day trial - unless you do a recovery. After a recovery it will require you to buy a license key to use.

  • Advice on the restructuring of code to avoid the downturn over time

    Hi, I am a new user of LabView, you want some advice on how to better structure the program I wrote and which is used to perform a hydrostatic test arrangement. Data are collected on a module of AI cDAQ 9207 and a cDAQ 9474 module is used to control the two valves that subsequently controls pressure in and out of the test Chamber. I enclose my first version of the reference code.

    What the VI is supposed to do when the user of the weapon system and strikes then the execution, the program switch will cause a transition of the test chamber through several States corresponding to the levels of different pressure for some time. Time curve vs pressure is written to a file of measures. I built using a state machine structure.

    My problem is, during the initial trial, when the duration of the test was about 5 minutes or so, the program went very well. However, once I started to test the length of the actual test of 2.5 hours, I noticed the program running slower and slower as time progressed to the point where it would take several seconds for each iteration of the loop, then by accident about an hour or two in the test. After scouring the articles and messages, I suspect the main culprit is the diagram of waveform and to a lesser extent DAQ assistant.

    While I need the loop structure data collection and case to run pretty quickly (preferably the order a few hundred s/s) I need the code to be able to react to changes in pressure in the room quickly enough, the actual data, that I need to store and display the graph can be as slow as a sample per second or even longer. My guess is at this point that I have to rebuild the program using some sort of architecture of producer/consumer, I'm looking for some advice on how best to structure the program.

    Data acquisition obviously belongs in the producer loop that will run at a fast enough pace and there will be a consumer loop that will run at a much slower pace for the Bulletin Board writing to the extent of the file vi, but where should I put the case of structure state machine which currently controls the valves? Should put in the loop of the producer since it must run at the same fast pace as the collection of data, so it can react to changes in pressure or should I build another loop of consumer for him? What other indicators (pressure and Boolean) and controls on the front panel, they should be on another loop operating at a medium speed I want to respond to a faster pace than the data to the chart/write the loop, but they do not need to be as fast as the data acquisition loop?

    Finally, I'm not quite sure what would be the best way to reduce the sampling rate of the data that I have in fact appear on the graph and the store in the file, it wouldn't make sense to create another task of measurement that samples at a rate below just for that purpose. Suggestions and advice would be greatly appreciated! Thanks in advance!

    Bob

    My guess is that if you monitor the use of memory over time, you see that going up, Yes? Everything is is always a good idea to get rid of the express VIs, I would look for the problem of slowdown in the structure of the case. You repeatedly open and never close tasks which are created using some DIO channels. Hnece each time that the loop more memory is allocated. Try to move creating channel screws out of the loop, then the tasks are created once before the start of the loop and then written to as required inside the loop - and don't forget to delete tasks once the loop over.

    Mike...

    PS: You are right with your comments regarding the structure of the producer consumer. Basically it break up as things get quickly (DAQ IO) that there is in a loop, while the user interface and the stuff that can take a long time (such as the data record) are in another loop. You can even have three loops with one for the acquisition of input data and the other for the digital output.

    PPS: Finally don't forget that even if the structure of consumption of the producer is usually represented with everything in a single diagram, there no need to be. I wrote a lot where each loop is in his own VI which is run dynamically, then the application starts.

  • I2C communication slows down over time using USB-8451

    Hi all!

    I try to communicate via a device slave using a USB-8451 I2C and I noticed that after a while the communication speed slows way.

    I can send and receive data very well and apparently my system seems to work. But the problem is that over time the speed of data transfer slows down so much so that if I let it run all night it has slowed to a crawl in the morning. My ultimate goal is to gather data, draw and save every second and at the beginning it is easily achievable, but after 10-15 hours I can no longer collect data as fast I need. I'm not quite sure if this is a problem with my code LabVIEW, the NI USB-8451 box or the slave device. But if I stop running the LabVIEW program and start it again, everything returns to normal.

    The slave device is a personalized card with a microchip PIC which acts as a slave I2C and returns the data at the request of the master. I can't imagine anyone will be able to determine if this is a problem directly, but if we can rule out the other two (code LabVIEW or the NOR-8451) as the source of the problem so I know it's my slave device.

    I've greatly simplified the LabVIEW code that I used to collect data on I2C and I see even this gradual slowing down over time. Attached, it's that the very simplified VI and the data file, it produced. I only ran it for about 45 minutes, but from the beginning to the end we see again that the enforcement timeframe is rising.

    I hope I'm just doing something stupid, thanks in advance!

    -Aaron

    Aaron,

    Do not have the additional module responsible for these functions, but I'm sure I know whats going on.  It seems that each time through the loop 'OR-845 x I2C Create Configuration Reference.vi' creates a new reference.  After a while it will start to slow things down.  Better to open the reference before the while loop starts and spend just the reference in the loop.  Don't forget to close the reference after the loop stops.

  • create the voltage over time

    Is it possible to generate an output voltage on a DAQ 6009 A0? I have a data excel sheet that I put in a 2D array. The voltage is not constant over time. I have problems with the synchronization of the signal. I was able to trace the signal on a chart. This is the same as the original data to excel. I have two data streams. Voltage and time. But after separating the two signals how can I generate the graph?


  • My WRT110 slows down gradually over time. Why?

    I noticed that my internet download speed became slow.  I tried using SpeakEasy and the speed of Comcast tested and found my upload and download speeds to be both less than 2 MB.  In the tests, I disconnected my WRT110 RangePlus Wireless Router and connected directly to the Comcast RCA modem.  I raised the speed races and got 21MB down and 4.4 upwards on two speed test sites.  I then be able to reset the WRT110 then reconnected to it.  So I ran the tests of speed and their return the same 21Mo down and 4.4 upward.  I came home a day later and tested and my download speed is 8 MB.  I came back today and it was 5MB.  I connected directly to the modem Comcast and returned to 22 MB down speed.  I then be able to reset the WRT110 and reconnected it and speeds were down to 22MB.  I have run this test twice over a period of days and got the same defacement of speed over time.  What could cause this?

    I think you can try to upgrade/re-flash the firmware on your router.

    Connect the computer to the router with the Ethernet cable. Download the latest firmware from the site Web of Linksys. Open the router configuration page and update the firmware on your router.

    After upgrading the firmware of your router, it is recommended that you must reset the router and reconfigure. Press and hold the reset button on the router for 30 seconds. Release the reset button and wait 30 seconds. Power cycle the router and reconfigure.

  • Svchost size seems to me like svchost (s) becomes larger and larger over time (between restart).

    original title: svchost size

    It seems that svchost (s) becomes larger and larger over time (between restart).

    He/they tend to reach the top of the sort (PWS) in memory of my task manager.

    It automatically get rid of her... whatever... it is now for use by more than one process (when they are no longer used)?

    If not, is there a way for him to say?

    Hello

    Sometimes you try more than once (the link of MVP) and there may be a site
    issue.

    Try this link to my profile
    https://MVP.support.Microsoft.com/profile=D2CBAC3A-D59A-4085-BA97-22B57FD00DF2

    Here is the link to the signature
    https://MVP.support.Microsoft.com/profile/Rob1

    I tried to answer the question in paragraph 1 above. Unless you encounter
    a kind of system problem the SVChost in Taskmanager will vary, in particular
    so shortly after a restart. And when you use the computer SVChost entries will be
    adding and removing management tasks on the fly. Check the information in the upper part
    two links in my previous answer.

    I hope this helps and happy holidays!

    Rob Brown - Microsoft MVP<- profile="" -="" windows="" expert="" -="" consumer="" :="" bicycle=""><- mark="" twain="" said="" it="">

Maybe you are looking for