数据库结构为: userName varchar
              loginTime timestamp需求是能够对登录用户进行按年、按月、按日三种时间粒度的查询,我写了一个SQL语句如下,不知道效率能否怎么再改进,请教高手。按年汇总:
select userName, to_date(to_char(LOGINTIME, 'yyyy'), 'yyyy') period, count(*) from EOSADMINLOGINTRACE group by userName, to_date(to_char(LOGINTIME, 'yyyy'), 'yyyy') order by to_date(to_char(LOGINTIME, 'yyyy'), 'yyyy');
按月汇总:
select userName, to_date(to_char(LOGINTIME, 'yyyy-MM'), 'yyyy-MM') period, count(*) from EOSADMINLOGINTRACE group by userName, to_date(to_char(LOGINTIME, 'yyyy-MM'), 'yyyy-MM') order by to_date(to_char(LOGINTIME, 'yyyy-MM'), 'yyyy-MM');
按天汇总
select userName, to_date(to_char(LOGINTIME, 'yyyy-MM-dd'), 'yyyy-MM-dd') period, count(*) from EOSADMINLOGINTRACE group by userName, to_date(to_char(LOGINTIME, 'yyyy-MM-dd'), 'yyyy-MM-dd') order by to_date(to_char(LOGINTIME, 'yyyy-MM-dd'), 'yyyy-MM-dd');