BlackCat
BlackCat

Reputation: 2044

How to get monthwise sum from table?

Table Transaction(Id, DateTime, Debit, Credit)

I want a monthwise sum of Debit and Credit.

What is a good option to retrieve monthwise result?

Sample Output:

Month    Id  Debit Credit
January   1  200    70
January   2  400    80
February  1  400    90
February  2  300    50

Upvotes: 0

Views: 56

Answers (2)

mkRabbani
mkRabbani

Reputation: 16908

Try this below script with GROUP BY function. I have added YEAR in consideration other wise same month from different year will count as same month.

SELECT YEAR(DateTime), 
MONTH(DateTime), 
Id, 
SUM(Debit) total_debit,
SUM(Credit) total_credit
FROM your_table
GROUP BY YEAR(DateTime), MONTH(DateTime), Id

Upvotes: 1

Mahesh Waghmare
Mahesh Waghmare

Reputation: 744

Apply Group by clause to SQL Query

group by month(DateTime),Year(DateTime)

Upvotes: 0

Related Questions