Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
SELECT {fn MONTHNAME(feepaiddate)} AS MonthName,YEAR(feepaiddate) AS Year, SUM(paidAmt) AS Profits FROM [bitcrj_database].[tbstufees]
 GROUP BY { fn MONTHNAME(feepaiddate) }, MONTH(feepaiddate), YEAR(feepaiddate)  order by Year(feepaiddate),month(feepaiddate)


Output:-


Month | Year | Profits
------ | ---------| ---------
January | 2022 | 34800
February | 2022 | 40500
March | 2022 | 43600 // Incorrect Sum in march


but i manual count in March month SUM is Correct 42000

What I have tried:

i want to correct output of month march
Posted
Updated 25-Mar-22 3:45am

Check your inputs: the chances are they aren't what you think.

And don't GROUP BY the same data twice: month name and number. Instead, write a GROUP by that returns the year, month, and sum - then test that in isolation. When that works, JOIN that back to the original table to pick up the month name and ORDER BY
 
Share this answer
 
I do agree with OriginalGriff's statement
Quote:
Check your inputs: the chances are they aren't what you think.
because I set up some dummy data and your code works fine.

I tend to disagree with him on the join to the original table though - what are you going to join to? You can however, join to a sub-query or CTE that does this
SQL
select distinct DATENAME(MONTH, feepaiddate) AS [MonthName], MONTH(feepaiddate) as [MonthNo]
	FROM tbstufees
(inner join on [MonthNo]

One other point, why are you using ODBC scalar functions just to get the month name? Why not use the built-in SQL functions? e.g.
SQL
{fn MONTHNAME(feepaiddate)} AS MonthName
can be replaced with
SQL
DATENAME(MONTH, feepaiddate) AS [MonthName]
Your query will run faster.

Also note that I am surrounding the reserved words MonthName and Month with square brackets when they are used as column aliases - this is to signal that they are definitely column names and not the functions with the same name. Or note that I used MonthNo instead of Month avoiding the problem altogether
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900