【问题标题】:How to count 2 fields with different conditions for each count in HQL如何为 HQL 中的每个计数计算 2 个具有不同条件的字段
【发布时间】:2011-03-10 08:39:33
【问题描述】:

我在编写 HQL 时遇到问题。 问题是我想把这样的东西转移到 HQL

 select 
   tb.aca_year, 
   (case when tw.isfulltime = 1 then count(te) end) as fulltime,
   (case when tw.isfulltime = 0 then count(te) end) as parttime
 from timetable tb, teacher te, teacherworktype tw 
  where .............
  group by tb.aca_year
 ................

有什么建议吗?

最好的问候,

【问题讨论】:

  • 不知道类模型就不能写HQL。所以告诉我们一些关于你的课程的事情......
  • 感谢您关心我的问题。但是,这些课程很长。你能根据我的问题来帮助我吗?
  • 我很困惑。 case when tw.isfulltime 既不在聚合函数中也不在 group by 子句中 tw.isfulltime 时如何实际工作...?

标签: database hibernate hql


【解决方案1】:

你可以这样做:

select tb.aca_year, 
    sum(case when tw.isfulltime = 1 then 1 else 0 end) as fulltime, 
    sum(case when tw.isfulltime = 0 then 1 else 0 end) as parttime
from timetable tb, teacher te, teacherworktype tw 
where ............. 
group by tb.aca_year ................

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2018-07-10
    • 2019-07-13
    • 2017-08-26
    相关资源
    最近更新 更多