1

我有一个员工班。

 case class Employee(name: String, age: Int, company: String)

我在 Quill 中编写了一个 Group-By 查询。

val q = quote {
     query[Employee]
     .filter(_.age == lift(100))
     .groupBy(_.company)
     .map { a =>
      val tot = a._2.size
      val totCond = (a._2.map { i =>
        if (i.name == "software") 1 else 0
      }.sum)
      (tot, totCond)
    }
}

ctx.run(q)

此查询转换为

SELECT COUNT(*), SUM(CASE WHEN x36.name = 'software' THEN 1 ELSE 0 END) FROM employee x36 
WHERE x36.age = ? GROUP BY x36.company

这很好。

现在我想对查询进行排序

  quote {
   query[Employee]
     .filter(_.age == lift(100))
     .groupBy(_.company)
     .map { a =>
       val tot = a._2.size
       val totCond = (a._2.map { i =>
         if (i.name == "software") 1 else 0
       }.sum)
       (tot, totCond)
     }.sortBy(_._1)(Ord.desc)
 }

But this translates to 

 SELECT x36._1, x36._2 FROM (SELECT COUNT(*) AS _1, SUM(CASE WHEN x36.name = 'software' THEN 1 
 ELSE 0 END) AS _2 FROM employee x36 WHERE x36.age = ? GROUP BY x36.company) AS x36 ORDER BY 
 x36._1 DESC

这显示为执行所有逻辑的子查询和仅从主查询中选择记录的主查询。

我想摆脱这一点,只在一个查询中完成所有事情。有办法吗??

4

0 回答 0