views:

1235

answers:

1

Hello.

I'm trying to make a report but I'm having problems with my archi nemesis SQL.

I have a table where the close date of a transaction is stored.

I want to know how many transaction per month there was so I did:

SELECT trunct( closedate, 'MONTH' ) FROM  MY_TRANSACTIONS

I'm using oracle.

I'm getting a list like this:

2002-09-01 00:00:00.0
2002-09-01 00:00:00.0
...
2002-10-01 00:00:00.0
2002-10-01 00:00:00.0
...
2002-11-01 00:00:00.0
2002-11-01 00:00:00.0

etc.

So I thought "If I add a COUNT( ) in the select and GROUP BY at the end of the statement that should do" but it doesn't. My guess is because each record is treated as a different value : -S

Any hint please?

Thanks.

+9  A: 

You want to group by all non-agg fields. And you don't want to truncate the date, you want the month part of the date.

so something like

select to_char(datefield, 'Month'), count(*) from ... group by to_char(datefield, 'Month');

Arnshea
@Arnshea: Thanks. Do you know what's the oracle equivalent for "datepart"???
OscarRyz
to_char(datefield, 'Month') should do it
Arnshea
@Arnshea: It did ( partially ) because I think is sum all decembers from the last years...
OscarRyz
Try to_char(datefield, 'MONTH YYYY')
Eric Petroelje
Thanks a lot... I was doing ( by intuition ) doing exactly that Eric. :)
OscarRyz
It's better form to do: TRUNC(datefield, 'Mon') to truncate the days, hours, minutes, seconds from the date.
WW
TRUNC will be slightly more efficient, but then you'll probably want to format the output anyway...
Jeffrey Kemp