Re: Except when you don't, because sometimes you won't

1

The answer is no.


Posted by: M/tch M/lls | Link to this comment | 03-12-07 9:58 PM
horizontal rule
2

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.


Posted by: chet | Link to this comment | 03-12-07 10:03 PM
horizontal rule
3

Makes no fuckin sense at all.


Posted by: John Emerson | Link to this comment | 03-12-07 10:10 PM
horizontal rule
4

SQL (well, really, the relational calculus) is the analytic philosophy of computer science. IYKWIM.


Posted by: arthegall | Link to this comment | 03-12-07 10:16 PM
horizontal rule
5

Also, shouldn't this be easy with a 'group by' in SQL? Or am I missing something?


Posted by: arthegall | Link to this comment | 03-12-07 10:22 PM
horizontal rule
6
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.


Posted by: K. Matthew | Link to this comment | 03-12-07 10:25 PM
horizontal rule
7

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?


Posted by: ogged | Link to this comment | 03-12-07 10:25 PM
horizontal rule
8

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.


Posted by: foolishmortal | Link to this comment | 03-12-07 10:25 PM
horizontal rule
9

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?


Posted by: arthegall | Link to this comment | 03-12-07 10:25 PM
horizontal rule
10

7 to 5, though perhaps obviated and refuted by 6.


Posted by: ogged | Link to this comment | 03-12-07 10:26 PM
horizontal rule
11

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.


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:26 PM
horizontal rule
12

Yeah, what 6 said. Plus the sub-select, if you want to join to foo somehow.


Posted by: arthegall | Link to this comment | 03-12-07 10:26 PM
horizontal rule
13

This only started happening on Saturday; the only change is that I got new speakers on Thursday.


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:27 PM
horizontal rule
14

Oh, and by no means group by time. Kluge of the century, that.


Posted by: foolishmortal | Link to this comment | 03-12-07 10:27 PM
horizontal rule
15

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.


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:28 PM
horizontal rule
16

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'.


Posted by: arthegall | Link to this comment | 03-12-07 10:29 PM
horizontal rule
17

6 has it, but, to be entirely correct, you want an outer join on foo to include zeros for the non-matching ids.


Posted by: foolishmortal | Link to this comment | 03-12-07 10:30 PM
horizontal rule
18

16: see 17


Posted by: foolishmortal | Link to this comment | 03-12-07 10:30 PM
horizontal rule
19

Fair enough.


Posted by: arthegall | Link to this comment | 03-12-07 10:31 PM
horizontal rule
20

Hooray! Though it seems extremely implausible that I've listened to the Tin Hat Trio more than anyone else since setting this shit up.


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:31 PM
horizontal rule
21

20 -- is that your nickname for Ogged, Labs, and Apo?


Posted by: arthegall | Link to this comment | 03-12-07 10:32 PM
horizontal rule
22

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


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:33 PM
horizontal rule
23

In your example ben, not in your actual application. I also assume that you're not naming your tables 'foo' and 'bar' for realz.


Posted by: arthegall | Link to this comment | 03-12-07 10:34 PM
horizontal rule
24

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.


Posted by: ben w-lfs-n | Link to this comment | 03-12-07 10:36 PM
horizontal rule
25

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.


Posted by: arthegall | Link to this comment | 03-12-07 10:40 PM
horizontal rule
26

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.


Posted by: zadfrack | Link to this comment | 03-13-07 5:58 AM
horizontal rule
27

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.


Posted by: Clownaesthesiologist | Link to this comment | 03-13-07 6:13 AM
horizontal rule
28

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.


Posted by: John Emerson | Link to this comment | 03-13-07 7:23 AM
horizontal rule
29

select foo.id, sum(bar.count) from foo left outer join bar on foo.id = bar.foo_id group by foo.id


Posted by: dob | Link to this comment | 03-13-07 10:20 AM
horizontal rule
30

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).


Posted by: soubzriquet | Link to this comment | 03-13-07 10:26 AM
horizontal rule
31

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.


Posted by: ben w-lfs-n | Link to this comment | 03-13-07 10:31 AM
horizontal rule
32

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)


Posted by: soubzriquet | Link to this comment | 03-13-07 10:36 AM
horizontal rule
33

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.


Posted by: ben w-lfs-n | Link to this comment | 03-13-07 9:46 PM
horizontal rule