Month Year extract (1 Viewer)

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:42
Joined
May 7, 2009
Messages
19,243
Code:
SELECT Format([Transaction Date],"mmm yyyy") AS Period,
    Transactions.[Transaction Type Ind],
    Sum(Transactions.[Total (UGX)]) AS [Total (UGX)]
FROM Transactions
GROUP BY
    Format([Transaction Date],"mmm yyyy"),
    Transactions.[Transaction Type Ind],
    Format([Transaction Date],"yyyymm")
HAVING (((Transactions.[Transaction Type Ind])=2))
ORDER BY Format([Transaction Date],"yyyymm");
 

ebs17

Well-known member
Local time
Today, 10:42
Joined
Feb 7, 2020
Messages
1,946
Assuming you could also do something with 202201 and not only with Jan 2022, then the following statement takes less than 75 percent of the runtime compared to the above, because one can calculate better with numbers than with strings.
It could be that you work a lot and successfully and therefore have a lot of transactions and therefore have to think about calculation times.
SQL:
SELECT
   Year([Transaction Date]) * 100 + Month([Transaction Date]) AS Period,
   [Transaction Type Ind],
   SUM([Total (UGX)]) AS [Total (UGX)]
FROM
   Transactions
WHERE
   [Transaction Type Ind] = 2
GROUP BY
   Year([Transaction Date]) * 100 + Month([Transaction Date]),
   [Transaction Type Ind]
ORDER BY
   Year([Transaction Date]) * 100 + Month([Transaction Date])
[Transaction Type Ind] can be omitted from the grouping here because it has already been filtered for a value.

If you also take into account the targeted use of indices, you can generally expect the runtime to be halved again.
 

Users who are viewing this thread

Top Bottom