Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inner join with derived table #151

Open
agreif opened this issue Sep 12, 2016 · 0 comments
Open

inner join with derived table #151

agreif opened this issue Sep 12, 2016 · 0 comments

Comments

@agreif
Copy link

agreif commented Sep 12, 2016

Hi,

is there a way to get this derived table SQL in esqueleto?

SELECT invoice.id, SumPayment
FROM invoice
INNER JOIN 
 (
   SELECT payment.invoice_id, SUM(payment.amount) AS SumPayment
   GROUP BY payment.invoice_id
   FROM payment
 ) AS payment
ON payment.invoice_id = invoice.id

I tried something like

  E.select $ E.from $ \(i `E.InnerJoin` p') -> do
    E.on (i E.^. InvoiceId E.==. p' E.^. PaymentInvoiceId)
    return ...
  where
    p' = E.sub_select $ E.from $ \p'' -> do
      E.groupBy $ p'' E.^. PaymentInvoiceId
      return (p'' E.^. PaymentInvoiceId, E.sum_ (p'' E.^. PaymentAmount))

But that does not compile

I would prefer a way without rawSql because I want to also join other tables.

thanks,
Alex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant