I'm staring at the last section of the "Advanced Associations" documentation, 
which says "you can use Sequel‘s association support to get aggregate 
information for columns in associated tables (sums, averages, etc.)"

Yup, that's what I want to do. But the provided example is absolutely baffling 
me. Truncated code for my case as follows:

        TABLE skus (
                id uuid DEFAULT uuid_generate_v1() NOT NULL,
                name varchar,
                PRIMARY KEY (id)
        );

        TABLE items (
                id uuid DEFAULT uuid_generate_v1() NOT NULL,
                sku_id uuid NOT NULL,
                quantity int4, 
                PRIMARY KEY (id)
        );

An "item" is an invoice line item. If one row of the 'skus' table is for a 
brown chair, there will be 0 or more rows in 'items' that indicate I have 
sold/shipped various quantities of brown chairs to some number of people. 
There's the obvious foreign key between sku_id and skus.id.

        class Sku < Sequel::Model
                one_to_many :items
        end

        class Item < Sequel::Model
                many_to_one :sku
        end

What I'd like to do is be able to retrieve a set of skus and the sum of the 
quantity field of the associated items. In other words "Sku A, and I've shipped 
xx of them. Sku B, and I've shipped yy of them," et cetera. I can certainly do 
that in ways that require lots of queries, but I can't quite wrap my head 
around how create the Sequel equivalent of

        select skus.*, sum(items.quantity)
        from skus join items on (skus.id=sku_id)
        group by skus.*

which isn't actually valid SQL code, I know. It could also be

        select *, 
        (select sum(quantity) from items where sku_id = skus.id)
        from skus 


-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to