分组不在Oracle工作 -

时间:2014-08-25 05:32:47

标签: oracle group-by

当我运行以下查询时,它可以正常工作

select  TO_CHAR(mydate ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')

但是当我尝试order by失败时

select  TO_CHAR(mydate ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc

enter image description here

同样当我尝试在日期做任何算术时,我得到同样的错误。

select  TO_CHAR(mydate +10/24 ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc

有人可以建议什么是最好的订购方式并添加10小时?

1 个答案:

答案 0 :(得分:0)

试试这个:

select  TO_CHAR(mydate ,'YYYYMMDD')  from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc

即,您需要将select的所有列都放在group by中以使其正常工作。

修改: -

select  TO_CHAR(mydate ,'YYYYMMDD') , count(someID) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc
having count(someID)>1