SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Conditional Aggregation


Conditional Aggregation

Author
Message
michael.lock@aer.ca
michael.lock@aer.ca
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 381
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).
mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 856
THe picture is a broken link. Please provide the table structure, small set of sample records, and an example of your expected results.






mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 856
Can you also provide a create table script?






michael.lock@aer.ca
michael.lock@aer.ca
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 381
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.
mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 856
For "bound by" can we assume the sequence number is +1 and -1 for bound records?






michael.lock@aer.ca
michael.lock@aer.ca
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 381
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)


michael.lock@aer.ca
michael.lock@aer.ca
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 381
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!
mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 856
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








michael.lock@aer.ca
michael.lock@aer.ca
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 381
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)
mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 856
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









Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search