我要翻译的查询的基本部分是这样的:
SELECT c.id, c.name, officer.id, officer.name
FROM (
SELECT DISTINCT ON (company.number) company.id, company.name
FROM company
ORDER BY company.number DESC
LIMIT 15
) AS c
INNER JOIN officer ON c.id = officer.company_id;
我正在尝试的 Esqueleto(无法编译)是这样的:
query mLocation = E.select $ E.from $
\o -> do
let sub = E.subList_select
$ E.from
$ \(c `E.LeftOuterJoin` cc `E.LeftOuterJoin` l) -> do
E.distinctOn [ E.don (c E.^. CompanyNumber) ] $ do
E.on $ c E.^. CompanyPostcode E.==. l E.^. LocationPostcode
E.on $ c E.^. CompanyId E.==. cc E.^. CompanyCategoryCompany
whereRegisteredFrom c (queryFilterFromDate qf)
whereRegisteredTo c (queryFilterToDate qf)
whereLikeTerm c (queryFilterSearchTerm qf)
whereVisible c
whereWithinRadiusOf (queryFilterRadius qf) mLocation
let categories' = map (\(QueryCategory a) -> a) (fromMaybe [] $ queryFilterCategories qf)
subQuery = E.subList_select $ E.from $ \cat -> do
E.where_ $ cat E.^. CategoryCode `E.in_` E.valList categories'
return $ cat E.^. CategoryId
when (isJust (queryFilterCategories qf))
(E.where_ $ cc E.^. CompanyCategoryCategory `E.in_` subQuery)
E.limit (fromInteger limit)
E.offset $ (fromInteger page - 1) * (fromInteger limit)
E.orderBy [ E.desc (c E.^. CompanyNumber) ]
return c
E.on $ sub E.^. CompanyId E.==. o E.^. OfficerCompanyId
return (sub, o)