Skip to content
Advertisement

How to sum transaction amounts for different time intervals in sql server?

Let say I have table called Transactions and have 3 following columns:

So I need to sum transaction amounts for each customer in different date intervals. For example, transaction amount for last 10 days, transaction amount from last 10 days to last 30 days and etc..

this is what I have tried. It work, but running time is ultimately slow.

For better understanding I write down what I need

Please consider that I have many many rows, so I need make my query running time as less as possible.

Advertisement

Answer

If I understand correctly, you want conditional aggregation:

For performance, you should add a where clause so you only aggregate the data you need. For instance, for the above two columns:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement