遇见的hql和纯sql的坑
首先今天公司交给我的任务呢是查询并显示一年内不同月份网站的点击量,说明一下小弟刚实习没多久,对业务只是刚刚了解,我们主要做的是cms系统。分配了任务我就马不停蹄的开始了,我打开项目一看,用的都是hql,一脸懵逼,我用的主要是ssm框架,好吧,hql就hql吧,上网搜了hql语句的一些用法,我先是写了个分组查询:
select to_date(access_date,'yyyy-mm-dd') time,count(*) count from jc_site_flow group by to_date(access_date,'yyyy-mm-dd') order by to_char(to_date(access_date,'yyyy-mm-dd'),'YYYY-MM');
弊端:这个将所有的都查出来了。(脸都丢光了)
又写了一个从网上查到的一个语句:
select
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'01',site_id,0)) January,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'02',site_id,0)) february,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'03',site_id,0)) march,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'04',site_id,0)) april,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'05',site_id,0)) may,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'06',site_id,0)) june,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'07',site_id,0)) july,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'08',site_id,0)) august,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'09',site_id,0)) september,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'10',site_id,0)) october,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'11',site_id,0)) november,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'12',site_id,0)) juDecemberly
from
jc_site_flow
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'01',site_id,0)) January,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'02',site_id,0)) february,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'03',site_id,0)) march,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'04',site_id,0)) april,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'05',site_id,0)) may,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'06',site_id,0)) june,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'07',site_id,0)) july,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'08',site_id,0)) august,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'09',site_id,0)) september,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'10',site_id,0)) october,
sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'11',site_id,0)) november,sum(decode(to_char(to_date(access_date,'yyyy-mm-dd'),'MM'),'12',site_id,0)) juDecemberly
from
jc_site_flow
好吧,这个是可以的,只是这个语句是不是有点太长了,很难受,而且最重要的弊端就是hql不支持decode函数,哦,忘了,我查的这个日期字段是varchar2的(有点不能忍受了),
最后说这样吧,既然hql不行,那就写原生的sql吧:
我的原生sql:
select to_char(access_time,'yyyy-mm'),sum(1) from jc_site_flow group by to_char(access_time,'yyyy-mm') ;
好吧,我自己找了一个是date类型的日期字段,其实这个语句hql应该是可以用的。
最后呢:找技术经理帮忙写了个sql:
select z.month_pv as month_pv, count(t.flow_id) as count_pv
from (SELECT TO_CHAR(ADD_MONTHS(trunc(sysdate,'yy'),ROWNUM-1
),'YYYY-MM') month_pv
FROM DUAL CONNECT BY ROWNUM<=12) z
left join jc_site_flow t
on to_char(t.access_time,'yyyy-MM')=z.month_pv
group by z.month_pv
order by z.month_pv asc
from (SELECT TO_CHAR(ADD_MONTHS(trunc(sysdate,'yy'),ROWNUM-1
),'YYYY-MM') month_pv
FROM DUAL CONNECT BY ROWNUM<=12) z
left join jc_site_flow t
on to_char(t.access_time,'yyyy-MM')=z.month_pv
group by z.month_pv
order by z.month_pv asc
本以为今天的任务可以顺利完成了,没想到查出来了但是页面不显示:
界面格式出来了,没数据
很难受,又是一段修改代码,最后找到错误原因是
我查出来的他将格式改成了全是大写的,而我的数据的是小写的,手动滑稽
好了,这就是我今天遇见的坑,ssh学习之路要展开了。。。。