Grouping Dates in Pivot Tables

I am struggling with a problem I know there must be a simple and direct solution:
I have an array with lots of dates, I need to extract the number of times each date was mentioned.
For example to show “Total units sold by salesperson in each day” in pivot table
but then, I have to expand this to group by month. How many per month.

to

I have created a partial solution. But it seems overcomplicated and slow.
Can anyone give me some light ?

Not knowing how your data is stored it is very hard to recommend anything
Personally I’d be using a be using a database either in memory or on disk and sql queries to manipulate the data very quickly

My database is inside a multidimensional array. Do you think it is better to work in sql??

Yes
This way you just have to create the right queries rather than all the code to do the pivoting
And you can create new tables and remove new tables as needed to manipulate the data
An in memory sqlite database is VERY fast

[quote]I have an array with lots of dates, I need to extract the number of times each date was mentioned.
For example to show “Total units sold by salesperson in each day” in pivot table[/quote]

if the data at the top was a database table, then the queries would look (roughly) like this:

Total units by salesman by date:

select SALESMAN,sum(UNITS) from SALESTABLE group by SALESMAN,DATEFIELD;

Total units by salesman by month:

select SALESMAN, strftime('%m', DATEFIELD) as MonthNo , sum(UNITS) from SALESTABLE group by SALESMAN, strftime('%m', DATEFIELD) ;