Ben, I'd suspect that this is hard to get, but I'm not
racking my brain right now. Some databases allow
"user-defined aggregates", and these aggregates
are, more-or-less, the moral equivalent of what
you're looking for, I'd guess.
You can also often get the same effect by grouping
by some key like time, using a granularity that
allows you to partially precompute some of the
aggregate and then finish off the "fold" at query
time.
SQL (well, really, the relational calculus) is the analytic philosophy of computer science. IYKWIM.
Also, shouldn't this be easy with a 'group by' in SQL? Or am I missing something?
select bar.foo_id, sum(bar.count) from bar group by bar.foo_id
Would give you the total of bar.count for each foo_id, if that's what you're asking for.
I think the complication there is that he wants a count for each id that matches, not just all ids that match in foo and bar. Or am I missing something?
Ack, I'm a little bit unsettled as opinion seems to be against me and I haven't used SQL for a year, but my SQL instinct tells me that yes, this is doable. I could prob even come up with a statement if I wasn't so tired. If noone else settles this w/in 12 hours, I will.
Here is an online tutorial that illustrates what I mean -- but I think you want something along the lines of:
select foo_id, sum(count) from bar group by foo_id
Right? And if you only want the subset of foo_id's from bar that actually occur in foo, well, you can probably subsume that statement above in a sub-select, right?
7 to 5, though perhaps obviated and refuted by 6.
If anyone knows why my computer sometimes just suddenly turns off, I'd like to know that, too. It seems to happen at random points during the decoding of ogg files (something that happens so often it's not clear that I've isolated anything, and even after updating to a new version), creates no messages, and is very annoying.
Yeah, what 6 said. Plus the sub-select, if you want to join to foo somehow.
This only started happening on Saturday; the only change is that I got new speakers on Thursday.
Oh, and by no means group by time. Kluge of the century, that.
Also, shouldn't this be easy with a 'group by' in SQL? Or am I missing something?
You're missing the part of the post where I disclaimed any knowledge of SQL.
No, I was asking the rest of the mineshaft. Also, what role does the table 'foo' play in your question? It seems like everything you need is in table 'bar'.
6 has it, but, to be entirely correct, you want an outer join on foo to include zeros for the non-matching ids.
Hooray! Though it seems extremely implausible that I've listened to the Tin Hat Trio more than anyone else since setting this shit up.
20 -- is that your nickname for Ogged, Labs, and Apo?
Also, what role does the table 'foo' play in your question?
It has other data that wasn't strictly relevant to the question. Something that at least works:
sqlite> select artists.name, temp.plays from artists outer join (select songs.artist_id as artist_id, sum(songs.plays) as plays from songs group by songs.artist_id) as temp on temp.artist_id = artists.id order by temp.plays desc limit 10;
Tin Hat Trio|17
Richard Thompson|17
L'ocelle Mare|16
Dirty Projectors|13
Josephine Foster|13
Flying Canyon|11
Alasdair Roberts|10
Sonore|10
Sun City Girls|10
Mountain Goats|10
In your example ben, not in your actual application. I also assume that you're not naming your tables 'foo' and 'bar' for realz.
Tim Peters, 29 April 1998: Indeed, when I design my killer language, the identifiers "foo" and "bar" will be reserved words, never used, and not even mentioned in the reference manual. Any program using one will simply dump core without comment. Multitudes will rejoice.
Also, after re-reading the Thread So Far, I've been giggling about the use of 'moral equivalent' in (2).
What else is in the moral equivalence class of (say) virtual base classes in C++? I suggest 'building a squirrel catapult' is probably in there.
Insofar as I understand the question you're asking:
select foo.id, sum(bar.count) from foo, bar
where foo.id = bar.foo_id
group by foo.id
4 is right. I've found that my philosophy ABD was excellent training for working with relational databases.
Re. 11 -- have you taken a look at your power supply? Because (assuming yours is not a laptop computer) a loose connection to the source of electricity could totally engender symptoms similar to those of which you speak, namely shutting off at random.
The Tin Hat Trio has a track record of sabotaging their fans' computers. If you've visited their site, that's probably where the problem is. They think of it as harmless fun.
select foo.id, sum(bar.count) from foo left outer join bar on foo.id = bar.foo_id group by foo.id
11: Another possibility is that you have a busted cpu fan. This will tend to have the effect of forced emergency shutdowns (to avoid damaging the chip) when doing CPU intesive things (like coding/decoding).
That was my first thought, since that's happened before. In the past, though, I've been unable even to boot after it's shut down for that reason, and the fan is spinning (I can't tell how fast, though). OTOH I now can't seem to access the bios configuration deal at boot time, which is where I'd get shunted in the past when the fan wasn't up to speed, so maybe things are just totally fucked up.
31: Flaky power supply sounds likely then. Do you have access to another? If not, they are pretty cheap ($15-20, If I recall from the last one I bought, a few years ago)
Here's something fun: according to the bios, at boot, my cpu was like 90 C. According to the just-installed lm_sensors, right now, it's 48C. On the other hand, I have reason to doubt its accuracy:
CPU Temp: +42°C (low = -1°C, high = -1°C) sensor = thermistor
Not to mention this:
VCore 1: +1.74 V (min = +4.08 V, max = +4.08 V) ALARM
VCore 2: +0.00 V (min = +4.08 V, max = +4.08 V) ALARM
+3.3V: +3.23 V (min = +4.08 V, max = +4.08 V) ALARM
+5V: +4.95 V (min = +6.85 V, max = +6.85 V) ALARM
+12V: +12.61 V (min = +16.32 V, max = +16.32 V) ALARM
-12V: -27.36 V (min = +3.93 V, max = +3.93 V) ALARM
-5V: -13.64 V (min = +4.03 V, max = +4.03 V) ALARM
Stdby: +5.03 V (min = +6.85 V, max = +6.85 V) ALARM
I mean, that just looks like it can't be right.