Click here to Skip to main content
15,907,392 members
Home / Discussions / Database
   

Database

 
GeneralRe: Populating textbox(s) from Column(s) Pin
numbrel1-Mar-05 3:53
numbrel1-Mar-05 3:53 
GeneralDatabase paging Pin
grea faflj25-Feb-05 5:49
grea faflj25-Feb-05 5:49 
GeneralRe: Database paging Pin
Jon Sagara25-Feb-05 7:02
Jon Sagara25-Feb-05 7:02 
GeneralSQL2000 RS Clarification Pin
mysorian25-Feb-05 3:28
professionalmysorian25-Feb-05 3:28 
GeneralADO.NET dataset w/o database Pin
scion780925-Feb-05 3:17
scion780925-Feb-05 3:17 
GeneralRe: ADO.NET dataset w/o database Pin
MitchellBaldwin25-Feb-05 9:52
MitchellBaldwin25-Feb-05 9:52 
GeneralRe: ADO.NET dataset w/o database Pin
Anonymous28-Feb-05 4:44
Anonymous28-Feb-05 4:44 
GeneralSUM evaluating to NULL Pin
Rhys Gravell25-Feb-05 2:37
professionalRhys Gravell25-Feb-05 2:37 
OK, How can I make a SUM statement evaluate to NULL if one of the SUM'd records is NULL? Or an equivalent of course...

I have a table with four fields, we'll call them id, date, time, readvalue. Id is a foreign key to a table holding meter information to which the reading pertains. Each meter belongs to a site, and a site can have between 1 and 10 meters. Using SUM and GROUP BY I can create aggregate SUM information. The problem is that a missing read value is represented by a NULL, (as 0 could be a valid read value, as can both negative and positive values), and for each date and time period if one meter belonging to a site has a NULL read value, the site level date/period read value should be derived as NULL. In case you want to know, there are 96 read periods in a day, (1/15 minutes)

Table Structure
MeterId ReadDate TimeIndex ReadValue
int datetime int decimal(19,10)

Aggregating Query
<br />
select<br />
    m.SiteLevelId,<br />
    mr.ReadDate,<br />
    mr.TimeIndex,<br />
    SUM(mr.ReadValue) as 'SiteReadValue'<br />
from<br />
    MeterReadingTable mr INNER JOIN MeterTable m ON mr.Id = m.Id<br />
group by<br />
    m.SiteLevelId,<br />
    mr.ReadDate,<br />
    mr.TimeIndex<br />
GO<br />


SiteLevelId is a foreign key on the meter table, referencing the site to which it belongs, (one to many as a site can have many meters)

Any idea's?

I'd like to avoid using a cursor, which I'm sure I could to get around this, as it's going to be pretty convoluted and probably very innefficient if I go that route.

Rhys

A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation...

Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die.
GeneralRe: SUM evaluating to NULL Pin
Rhys Gravell25-Feb-05 3:26
professionalRhys Gravell25-Feb-05 3:26 
GeneralRe: SUM evaluating to NULL Pin
Anonymous25-Feb-05 14:29
Anonymous25-Feb-05 14:29 
GeneralRe: SUM evaluating to NULL Pin
Anonymous25-Feb-05 20:50
Anonymous25-Feb-05 20:50 
GeneralRe: SUM evaluating to NULL Pin
keepsmile25-Feb-05 23:12
keepsmile25-Feb-05 23:12 
GeneralSQL server problem (authetication) Pin
Anonymous24-Feb-05 23:40
Anonymous24-Feb-05 23:40 
Questionexec @SqlString ???? Pin
boruu24-Feb-05 21:19
boruu24-Feb-05 21:19 
AnswerRe: exec @SqlString ???? Pin
Colin Angus Mackay24-Feb-05 22:40
Colin Angus Mackay24-Feb-05 22:40 
GeneralRe: exec @SqlString ???? Pin
boruu24-Feb-05 22:59
boruu24-Feb-05 22:59 
GeneralData Access Design Pattern: OR Mapping tools Pin
devvvy24-Feb-05 20:14
devvvy24-Feb-05 20:14 
GeneralCopy table Pin
BuddelWilli24-Feb-05 10:25
BuddelWilli24-Feb-05 10:25 
GeneralRe: Copy table Pin
Yulianto.24-Feb-05 13:33
Yulianto.24-Feb-05 13:33 
GeneralOLEDB Connection Pin
Just Greeky Creek24-Feb-05 4:02
Just Greeky Creek24-Feb-05 4:02 
GeneralRe: OLEDB Connection Pin
Yulianto.24-Feb-05 13:35
Yulianto.24-Feb-05 13:35 
GeneralRe: OLEDB Connection. &lt;b&gt;SOLVED&lt;/b&gt; Pin
Just Greeky Creek24-Feb-05 20:06
Just Greeky Creek24-Feb-05 20:06 
GeneralSQL Query Problem Pin
pjholliday23-Feb-05 3:33
pjholliday23-Feb-05 3:33 
GeneralRe: SQL Query Problem Pin
Michael Potter23-Feb-05 9:27
Michael Potter23-Feb-05 9:27 
GeneralODP.NET &amp; MSDAORA Pin
yyf23-Feb-05 3:02
yyf23-Feb-05 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.