Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Conditional Aggregation Expand / Collapse
Author
Message
Posted Monday, April 21, 2008 9:19 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
I have a table with records which are flagged as passing or failing an initial criteria test.

The records look like this;


For any given well I need to test each record with an OreFlag of N which is bounded by records with an OreFlag of Y and if;

- The length of the N record is less than 1 (meter). This condition may be a variable
- The length of the N record is less than the smaller of the to Y records
- and the Length weighted average of the MBit column would be greater than 0.06 (This the initial - variable - critera which termined whether a record was initially set to Y or N).


if you look at records 10 through 12 of the screen shot you will see that these should be aggregated. This, of course, leads to the next issue. Recursion. Conditions exist (see wellNum4) where, once the first aggregation is completed, It creates new records which should then also be aggregated until, at some point, no more aggregation can be done.

Aggregation involves taking the minimum TopDepth, maximum BaseDepth and the length weighted average of MBit, PHIE, Vsh, SwE (if you're a geologist you'll know what those attributes are ), and the sum of length.

The data set I'm working with has 367,000 rows. If you're interested in helping me solve this problem and having the data set I can up-load it (it's about 4Mb zipped).
Post #488047
Posted Monday, April 21, 2008 9:58 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 8:10 AM
Points: 445, Visits: 840
THe picture is a broken link. Please provide the table structure, small set of sample records, and an example of your expected results.






Post #488077
Posted Monday, April 21, 2008 10:42 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 8:10 AM
Points: 445, Visits: 840
Can you also provide a create table script?






Post #488106
Posted Monday, April 21, 2008 10:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
Ah. I should just mention that in the assembled intervals PNG the 2nd example is a case where there would be recursion. The first three records would be aggregated (shown as the row in red), and then that newly aggregated row should be combined with the two above it to produce the row shown in blue.
Post #488107
Posted Monday, April 21, 2008 10:55 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 8:10 AM
Points: 445, Visits: 840
For "bound by" can we assume the sequence number is +1 and -1 for bound records?






Post #488113
Posted Monday, April 21, 2008 10:56 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
The table I'm working with is acutally a temporary table I've created from the source data which has been quite processed to get it to this point.

The table definition below will give you a table which matches the data I posted and has the appropriate PK.

The column called Seq is one I've added for convenience of processing (makes it able to do previous row, next row processing)

CREATE TABLE [EUB\CB238].[TEMP](
[HOLEID] [varchar](13) NOT NULL,
[EvalNum] [int] NOT NULL,
[Seq] [int] NOT NULL,
[TopDepth] [numeric](6, 2) NOT NULL,
[BaseDepth] [numeric](6, 2) NOT NULL,
[MBit] NUMERIC(6,2) NULL,
[PHIE] NUMERIC(6,2) NULL,
[SwE] NUMERIC(6,2) NULL,
[Vsh] NUMERIC(6,2) NULL,
[Length] [numeric](6, 2) NULL,
[OreFlag] [varchar](1)
)
CREATE UNIQUE CLUSTERED INDEX TEMP_PK ON TEMP(HoleId, EvalNum, TopDepth)

Post #488114
Posted Monday, April 21, 2008 11:02 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
I'm not sure what you mean by bound by. If you mean that I can join the table to it's self like this

FROM ##TEMP CUR
LEFT JOIN ##TEMP PREV
ON PREV.HoleId = CUR.HoleId
AND PREV.EvalNum = CUR.EvalNum
AND PREV.Seq = CUR.Seq - 1
LEFT JOIN ##TEMP NEX
ON NEX.HoleId = CUR.HoleId
AND NEX.EvalNum = CUR.EvalNum
AND NEX.Seq = CUR.Seq + 1

to get the previous and next rows - then yes.

Again - Thanks!
Post #488118
Posted Monday, April 21, 2008 11:34 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 8:10 AM
Points: 445, Visits: 840
Here is what I have so far to gather the records that need to be aggregated. How do I determine the lenght weighed average of the MBit column?

select a.*,b.seq prec,c.seq nrec from wells a 
join wells b on a.evalnum = b.evalnum and a.holeid=b.holeid and a.seq = b.seq -1 and b.oreflag = 'y'
join wells c on a.evalnum = c.evalnum and a.holeid=c.holeid and a.seq = c.seq +1 and c.oreflag = 'y'

where a.oreflag = 'n' and a.length < 1and a.length < b.length and a.length < c.length


union
select d.*,0,0 from wells d
join
(select a.*,b.seq prec,c.seq nrec from wells a
join wells b on a.evalnum = b.evalnum and a.holeid=b.holeid and a.seq = b.seq -1 and b.oreflag = 'y'
join wells c on a.evalnum = c.evalnum and a.holeid=c.holeid and a.seq = c.seq +1 and c.oreflag = 'y'

where a.oreflag = 'n' and a.length < 1and a.length < b.length and a.length < c.length
) set1 on d.seq=set1.prec

union
select d.*,0,0 from wells d
join
(select a.*,b.seq prec,c.seq nrec from wells a
join wells b on a.evalnum = b.evalnum and a.holeid=b.holeid and a.seq = b.seq -1 and b.oreflag = 'y'
join wells c on a.evalnum = c.evalnum and a.holeid=c.holeid and a.seq = c.seq +1 and c.oreflag = 'y'

where a.oreflag = 'n' and a.length < 1and a.length < b.length and a.length < c.length
) set1 on d.seq=set1.nrec








Post #488130
Posted Monday, April 21, 2008 11:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 04, 2012 6:36 AM
Points: 96, Visits: 228
Length (it should actually be [interval] thickness rather than length - My bad on the naming) weighted average is calculated like this;

((Prev.MBit * Prev.Length)
+ (Cur.MBit * Cur.Length)
+ (Nex.MBit * Nex.Length))
/(Prev.Length + Cur.Length + Nex.Length)

Post #488133
Posted Monday, April 21, 2008 1:00 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 01, 2013 8:10 AM
Points: 445, Visits: 840
Let's try this

select 
a.holeid,a.evalnum,c.seq
/*a.seq,a.topdepth,a.basedepth,a.mbit,a.phie,a.swe,a.vsh,a.length,a.oreflag,b.seq prec,c.seq nrec,a.mbit*a.length wambit,a.phie*a.length waphie,a.vsh*a.length wavsh,a.swe*a.length waswe*/
,case when case when a.basedepth > b.basedepth then a.basedepth else b.basedepth end > c.basedepth then case when a.basedepth > b.basedepth then a.basedepth else b.basedepth end else c.basedepth end maxbasedepth
,case when case when a.topdepth < b.topdepth then a.topdepth else b.topdepth end < c.topdepth then case when a.topdepth < b.topdepth then a.topdepth else b.topdepth end else c.topdepth end mintopdepth
,((a.mbit*a.length) +(b.mbit*b.length) + (c.mbit*c.length)) /(a.length+b.length + c.length) lwambit
,((a.phie*a.length) +(b.phie*b.length) + (c.phie*c.length)) /(a.length+b.length + c.length) lwaphie
,((a.swe*a.length) +(b.swe*b.length) + (c.swe*c.length)) /(a.length+b.length + c.length) lwaswe
,((a.vsh*a.length) +(b.vsh*b.length) + (c.vsh*c.length)) /(a.length+b.length + c.length) lwavsh
, (a.length+b.length + c.length) sumlength
,case when ((a.mbit*a.length) +(b.mbit*b.length) + (c.mbit*c.length)) /(a.length+b.length + c.length) > .06 then 'Y' else 'N' end oreflag
from wells a
join wells b on a.evalnum = b.evalnum and a.holeid=b.holeid and a.seq = b.seq -1 and b.oreflag = 'y'
join wells c on a.evalnum = c.evalnum and a.holeid=c.holeid and a.seq = c.seq +1 and c.oreflag = 'y'

where a.oreflag = 'n' and a.length < 1and a.length < b.length and a.length < c.length









Post #488183
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse