|
by TAFKAM 01/31/2003, 9:28pm PST |
|
 |
|
 |
|
Zebco Fuckface wrote:
TAFKAM wrote:
I know V's a database person, anyone else? I've been busting my head against this seemingly simple problem and thought I might as well run it up a flagpole over here as well:
Item=Text PRIMARY KEY
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, Q1, Q2, Q3, Q4, FULLYEAR = real
1-12 = Months
I have aggregate fields Q1, Q2, Q3, Q4, FULLYEAR that I want to stick the average of the periods into for a table full of records.
What's the cleanest, easiest way to do it? 1-12 are nullable. That could potentially whack out the average values for a flat (1+2+3)/3
I thought of using variables and setting them to 1 or 0 depending on if NULL, then dividing by (var1+var2+var3), but that would be seriously wasteful in a loop for all records - Resetting the variables, testing them, writing * 5 fields x *100000000 records.
Am I missing something obvious?
Well, putting IfNull(columname, 0) in the query string would fix up that problem. Sounds like you're doing mini data-warehousing; a general solution depends entirely on the database you're using. You can use materialized views (or creating a unique clustered index on the view, or whatever the fuck they call it) on SQL Server, as an example.
Of course, tracking something monthly but letting null in your data set is kind of silly.
Thanks ZF - The system is actually a fully-functioning forecast/financial system I'm designing and implementing for work: This procedure will run nightly and roll-up data from a variety of different tables into one table on which the views will be created - The views will be used to drive Crystal Reports. The front-end will be over a browser using ASP.net. The reason that NULLs get into the data is that in a table for customer WALMART, Customer ID 1, for example, the base data would look like:
CustID ITEM Year Month UnitsSold SellingPrice
1 ITEM 2002 1 534 4.5 (sold 534 at $4.50 in January 2002)
1 ITEM 2002 2 NULL NULL (didn't sell any of that item in Feb 2002)
1 ITEM 2002 3 234 5.53(Sold 234 at $5.53 in March)
Then, when the procedure crosstabs it and drops 4.50 into month1, NULL into month2, and 5.53 into month3, when I create the average values for those three months to use the NULL value would be to screw the data.
I was thinking about the isnull function also, but that will work fine for NULL, when it isn't null it will still screw the denominator. (A+B+C)/(0+4.3+0) = CRAP
Any other ideas? |
|
 |
|
 |
|
|
|