I have a table containing pipes that are installed at a location.
Installed dates are always filled in, removed dates can be NULL
Type Installed Removed Length
PT2 01/01/2011 NULL 2000
PT2 01/01/2011 NULL 2000
PT1 01/01/2011 NULL 1200
PT1 01/01/2011 NULL 1200
PT1 15/02/2011 25/02/2011 1000
PT1 15/02/2011 25/02/2011 1000
Now I need an overview of the total length per type that was installed for a given month, so the result should be for example from 01/02/2011 to 28/02/2011:
Type From To Length
PT2 01/02/2011 28/02/2011 4000
PT1 01/02/2011 14/02/2011 2400
PT1 15/02/2011 24/02/2011 4400 Edit: (starts on 15 not 14)
PT1 25/02/2011 28/02/2011 2400
EDIT: Clarification on this expected result.
In the end this will be used to see the total length of pipe at any given moment during the requested month. So if you look at the table above, during February 2 PT2 pipes were installed. They were installed in January, but are still present, so during the entire month there is a total length of 4000.
Same for PT1: From the start of February 2 PT1 pipes are installed, so 2400. However on the 15th an extra 2 PT1 pipes of length 1000 are installed so during the 15th to the 25th the total length of PT1 pipes is 4400.
These 2 pipes are removed on the 25th so 25 to end of month it’s 2400 again.
I hope this makes a little more sense now.
I’m struggling with how to do this in SQL, It’s for a report, and generally there are hundreds of these pipes installed in any given month.
It’s for use in a Powerbuilder application, so if you know any datawindow trickery that might help feel free to share.
Having too much fun with this! I’ve taken a few liberties, like assuming a sort order, and changing your query criteria to a date range instead of declaring a month.
To start with, I changed the data set from date ranges to dates of changes and quantity of changes (positive values on installation date, negative values on removal date).
And, yes, those colons in front of the arguments (I’ve switched you to start and end dates… easier for me and now you can do six month reports) means I’m leveraging a DataWindow. (I’m betting someone can create a pure SQL approach with the above concept change, but I’m going with what I know.)
Load the SQL into a DataWindow (I used freeform), and optionally set a client-side sort by type and date_of_change (belt and suspenders). The data set includes null Removed dates as a change the day after the end of your query range, so create a filter to exclude those nulls:
Create a group based on type, and put type into the group header.
In the detail band (where all subsequent controls are going), create a compute called date_from with the following expression:
Create a compute called date_to with the following expression:
Create a compute called installed_length with the following expression:
Select all the controls in your detail band and give them a Visible expression of:
which will make them invisible if the next row has the same date_of_change and type; you only want the last row to show with the sum of all of today’s activities.
Drag the detail band to height of zero, and make the detail band autosize height.
That will give you a version of what you’re after. FWIW, I tried to make the report sort on date (not clear if that’s what you wanted, or sorted first on type), but it breaks the CumulativeSum() functionality. Maybe someone else can figure it out.
Good luck,
Terry.
P.S. If SO lets me put this much in, here’s the export of my prototype. It may or may not be useful to you.