Re: [SQL] Aggregate query for multiple records

2004-08-28 Thread Greg Stark
Scott Gerhardt [EMAIL PROTECTED] writes: Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum() values for each distinct wid as in the example below,

Re: [SQL] Aggregate query for multiple records

2004-08-28 Thread Tom Lane
Greg Stark [EMAIL PROTECTED] writes: [ nice example snipped ] ... Also, you'll have to change it to use reals. That part, at least, can be worked around as of 7.4: use polymorphic functions. You can declare the functions and aggregate as working on anyelement/anyarray, and then they will

Re: [SQL] Aggregate query for multiple records

2004-08-28 Thread Scott Gerhardt
Greg Stark [EMAIL PROTECTED] writes: [ nice example snipped ] ... Also, you'll have to change it to use reals. That part, at least, can be worked around as of 7.4: use polymorphic functions. You can declare the functions and aggregate as working on anyelement/anyarray, and then they will

Re: [SQL] Aggregate query for multiple records

2004-08-28 Thread Josh Berkus
Scott, Unfortunately, your revised query works like a charm except for the fact that prd_data.date - prd2.date + 1 give incorrect values when the year wraps, see in the output below. Need to conditionally subtract 88 from the date or use an incrementing count() function instead of date math

Re: [SQL] Aggregate query for multiple records

2004-08-27 Thread Troy
shouldn't take that long, I would think. You have indexes on wid and date? Troy Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum() values for

Re: [SQL] Aggregate query for multiple records

2004-08-27 Thread Josh Berkus
Scott, SELECT wid, (SELECT SUM(oil) FROM prd_data pd2 WHERE pd2.wid = prd_data.wid ORDER BY date LIMIT 6) as tot_oil, (SELECT SUM(hours) FROM prd_data pd2 WHERE pd2.wid = prd_data.wid ORDER BY date LIMIT 6) as tot_hours FROM prd_data ORDER BY wid; There is only

Re: [SQL] Aggregate query for multiple records

2004-08-27 Thread Josh Berkus
Scott, I tried your query but it doesn't seem to work. The interpreter expects prd2.date to be grouped but this doesn't make sence, see below: Oh, yeah, darn it. Hmmm ... maybe you could explain the purpose of selecting just 6? This query is going to get a *lot* uglier if there

[SQL] Aggregate query for multiple records

2004-08-26 Thread Scott Gerhardt
Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum() values for each distinct wid as in the example below, but except for all wid's (not just WHERE

Re: [SQL] Aggregate query for multiple records

2004-08-26 Thread Josh Berkus
Scott, Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. We have a charter? Why didn't anyone tell me? My questions is: What is the best method to perform an aggregate query to calculate sum() values for each distinct wid as in the