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

Aggregation interacts incorrectly with union #17

Open
tomjaguarpaw opened this issue Sep 17, 2013 · 1 comment
Open

Aggregation interacts incorrectly with union #17

tomjaguarpaw opened this issue Sep 17, 2013 · 1 comment

Comments

@tomjaguarpaw
Copy link

import Database.HaskellDB
import Database.HaskellDB.PrimQuery
import Database.HaskellDB.Query
import Database.HaskellDB.DBLayout

data Name' = Name'
instance FieldTag Name' where
  fieldName _ = "name"

data Apples = Apples
instance FieldTag Apples where
  fieldName _ = "age"

name :: Attr Name' String
name = mkAttr Name'

apples :: Attr Apples Int
apples = mkAttr Apples

applesTable :: Table (RecCons Name' (Expr String) (RecCons Apples (Expr Int) RecNil))
applesTable = Table "apples" [ ("name", AttrExpr "namecol")
                             , ("apples", AttrExpr "applecol")]

totalApples :: Query (Rel (RecCons Name' (Expr String) (RecCons Apples (Expr Int) RecNil)))
totalApples = do
  t <- table applesTable `union` table applesTable
  project (name << t!name #
           apples << _sum(t!apples))

The aggregation is done before the union. It should be after.

*Main> putStrLn $ showSql totalApples 
(SELECT namecol as name,
        SUM(age3) as age
 FROM apples as T1
 GROUP BY namecol)
UNION
(SELECT namecol as name,
        SUM(age3) as age
 FROM apples as T1
 GROUP BY namecol)
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