按出生日期统计年龄段oracle原生SQL和hibernate中查询设计

 select agerange ,count(*) as count from
(
 select 
 case 
     when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '0' and to_char
        (sysdate,'yyyy')-to_char(birthdate,'yyyy') < '20' then '20岁以下'

      when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '20' and to_char
      (sysdate,'yyyy')-to_char(birthdate,'yyyy')<='30' then '20-30'

       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '30' and to_char
       (sysdate,'yyyy')-to_char(birthdate,'yyyy')<='40' then '30-40' 

       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '40' and to_char
       (sysdate,'yyyy')-to_char(birthdate,'yyyy')<='50' then '40-50' 

       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '50' and to_char
       (sysdate,'yyyy')-to_char(birthdate,'yyyy')<='60' then '50-60' 

       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >'60'  then '60岁以上' 
       end 
       as agerange from hr_resume
)
group by agerange

    根据数据库表中的出生日期统计出各个年龄段的人数,在oracle中测试通过

hibernate HQL函数

写道
√通过 ⊙测试没结果,但没出错 ×未通过

方法 说明 类型 支持 测试结果HQL 使用方法

ABS(n) 取绝对值 数学函数 JPA QL HQL √ ABS(column_name[数字类型对象属性])
SQRT(n) 取平方根 数学函数 JPA QL HQL √ SQRT(column_name[数字类型对象属性])
MOD(x,y) 取余数 数学函数 JPA QL HQL √ MOD([对象属性(数字)或值],[对象属性(数字)或值]) 数字必须是整型。返回参数1/参数2得的余数。
SIZE(c) 方法集合内对象数量 集合函数 JPA QL HQL
MINELEMENT(c) 返回集合中最小元素 集合函数 HQL
MAXELEMENT(c) 返回集合中最大元素 集合函数 HQL
MININDEX(c) 返回索引集合最小索引 集合函数 HQL
MAXINDEX(c) 返回索引集合最大索引 集合函数 HQL
CONCAT(s1,s2) 连接连个字符串 字符串函数 JPA QL HQL √ CONCAT([对象属性],[对象属性]) 相当与“||”
SUBSTRING(s,offset,length) 返回部分字符串 字符串函数 JPA QL HQL √ SUBSTRING([要截取的字符串属性字段],开始位置,截取长度)
TRIM([[ BOTH | LEADING 去掉字符串中的某个给定的字符.
| TRAILING]] char FROM s) 默认去掉字符串两面的空格. 字符串函数 JPA QL HQL √ 默认用法,TRIM([字符串对象属性列]) 将字段两端的空格去掉。
LOWER(s) 小写 字符串函数 JPA QL HQL √ LOWER([字符串对象属性列]) 将该列结果含有的字母全部大写
UPPER(s) 大写 字符串函数 JPA QL HQL √ UPPER([字符串对象属性列]) 将该列结果含有的字母全部大写
LENGTH(s) 返回字符串长度 字符串函数 JPA QL HQL √ LENGTH(字段名) 返回字段内容的长度,包括数字。null值返回null.
CURRENT_DATE() 返回数据库当前日期 时间函数 JPA QL HQL √ CURRENT_DATE() 返回数据库当前日期
CURRENT_TIME() 时间 时间函数 √ CURRENT_TIME() 返回数据库当前时间
CURRENT_ 时间戳
TIMESTAMP()
SECOND(d) 从日期中提取具体参数分别为: 时间函数 HQL √ SECOND(时间字段) 空的时候返回null
MINUTE(d) 秒,分,小时,天,月,年 √ 同上
HOUR(d) √ 同上
DAY(d) √ 同上
MONTH(d) √ 同上
YEAR(d) √ 同上
CAST(t as type) 强制类型转换 转换函数 HQL √ CAST([字段或值] as [要转换的类型-int,string...])
max()
min()
count() 

hibernate试了好几种方法总是报hql错误,最后只得用nativesql完成 ,下面贴出错误的写法望哪位大侠帮我看看正确的hql该咋写

错误一HQL写法

        String hql1 =" select agerange ,count(agerange) as count from "+
            "(select case "+ 
            " when year(current_date())-year(r.birthdate) >= 0  and year(current_date())-year(r.birthdate) < 20 then '20岁以下' "+

             " when year(current_date())-year(r.birthdate) >= 20 and year(current_date())-year(r.birthdate)<= 30 then '20-30' "+
             
             " when year(current_date())-year(r.birthdate) >= 30 and year(current_date())-year(r.birthdate)<= 40 then '30-40' "+
             
             " when year(current_date())-year(r.birthdate) >= 40 and year(current_date())-year(r.birthdate)<= 50 then '40-50' "+
             
             " when year(current_date())-year(r.birthdate) >= 50 and year(current_date())-year(r.birthdate)<= 60 then '50-60' "+
             
             " when year(current_date())-year(r.birthdate) > 60  then '60岁以上'  end as agerange from Resume r"+
             " )group by agerange"; 

 错误二HQL写法

        String hql2= "select " +
            " sum(case when year(current_date())-year(r.r.birthdate) >= 0  and year(current_date())-year(r.birthdate) < 20 then 1 else 0 end),"+
            " sum(case when year(current_date())-year(r.birthdate) >= 20 and year(current_date())-year(r.birthdate)<= 30 then 1 else 0 end),"+
            " sum(case when year(current_date())-year(r.birthdate) >= 30 and year(current_date())-year(r.birthdate)<= 40 then 1 else 0 end),"+
            " sum(case when year(current_date())-year(r.birthdate) >= 40 and year(current_date())-year(r.birthdate)<= 50 then 1 else 0 end),"+
            " sum(case when year(current_date())-year(r.birthdate) >= 50 and year(current_date())-year(r.birthdate)<= 60 then 1 else 0 end),"+
            " from Resume r where 1=1 group by r.birthdate ";

 正确的NativeSql写法

        String nativeSQL="select agerange ,count(*) as count from"+
"(select case"+
"      when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '0' and to_char (sysdate,'yyyy')-to_char(birthdate,'yyyy') < '20' then '20岁以下' "+

"      when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '20' and to_char (sysdate,'yyyy')-to_char(birthdate,'yyyy')< '30' then '20岁~29岁' "+

"       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '30' and to_char (sysdate,'yyyy')-to_char(birthdate,'yyyy')< '40' then '30岁~39岁' "+

"       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '40' and to_char (sysdate,'yyyy')-to_char(birthdate,'yyyy')< '50' then '40岁~50岁' "+

"       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >= '50' and to_char (sysdate,'yyyy')-to_char(birthdate,'yyyy')<='60' then '50岁~60岁' "+

"       when to_char(sysdate,'yyyy')-to_char(birthdate,'yyyy') >'60'  then '60岁以上' end as agerange from hr_resume "+
") group by agerange ";
        List<Object[]> list =  this.getSession().createSQLQuery(nativeSQL).list();
        return list;
 


 

猜你喜欢

转载自sunrysoft.iteye.com/blog/1463766