0

I'm trying to replicate the following query usine Squeryl.

SELECT c.order_number,p.customer,p.base,( 
  SELECT sum(quantity) FROM "Stock" s where s.base = p.base  
) as stock 
FROM "Card" c, "Part" p WHERE c."partId" = p."idField";

I have the following code for selecting the Cards and Parts but I cannot see a way to add a sumation into the select clause.

from(cards, parts)((c,p) => 
where(c.partId === p.id)    
select(c,p)

Any help is much appreciated!

4

1 回答 1

3

在 Squeryl 中,您可以Queryable在查询的 from 子句中使用任何对象。因此,要创建子查询,类似以下内容应该适合您:

def subQuery = from(stock)(s => groupBy(s.base) compute(sum(s.quantity)))

from(cards, parts, subquery)((c, p, sq) =>
  where(c.partId === p.idField and sq.key === p.base)
  select(c.orderNumber, p.customer, sq.measures))

当然,字段名称可能略有不同,只是猜测类定义。如果您想要整个对象cardsparts不是原始查询中的单个字段 - 只需将 select 子句更改为:select(c, p, sq.measures)

于 2013-04-24T16:47:03.310 回答