3

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. :)

1 Answer 1

4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.