简体   繁体   中英

GROUP BY not working in Oracle 11g

I have a table

expenses (
    purchase_date date, 
    item_purchased varchar2(15),
    amount number
)

I want to perform a 'group by' on purchased_date(a date column). Say, I have to do this:

SELECT purchase_date, count(*) 
FROM expenses 
GROUP BY purchase_date;

It gives me result as :

purchase_date    count
-------------   -------
19-Jul-12           1
20-Jul-12           1

Though I have multiple entries belonging to a same day

I am using Oracle 11g. One of my friend uses Oracle 10g and the same query works as expected in her version. What's the problem in 11g then?

Please help me friends.

TIA. :)

A DATE column also contains the time of the day. Unfortunately SQL*Plus (which you seem to be using) does not display the time in the default configuration.

To get rid of the time during aggregation use the trunc() function:

SELECT trunc(purchase_date), count(*) 
FROM expenses 
GROUP BY trunc(purchase_date);

If that doesn't change the result then it's very likely your data is not what you think it is. You should post a self-contained test case then (including the INSERT statement to create the test data). You can do that at http://sqlfiddle.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM