Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,
It is my table can i get result for this .
Each user have points,i have to show user points by number of points got in current month,current year

Thanks.

userId points date
1 5 8/25/2013
1 3 8/16/2013
1 2 8/01/2013
1 2 9/25/2013
1 1 9/08/2013
1 3 8/25/2012
1 4 8/25/2011
1 3 8/25/2012
1 5 9/27/2013
1 3 8/25/2011
1 1 8/25/2011
1 5 8/25/2012

result as

userid --TotalNumberofpointsinCurrrentMonth-- TotalNumberofpointsinCurrrentyear
1 ------- ------- 10 ----------------------------- --------------- 18
Posted

Hi...

try this

SQL
;WITH ResultCTE As (

SELECT UserId,NULL monthPoints,SUM(Points) YearPoints  FROM YourTableName
WHERE YEAR([Date]) = YEAR(GETDATE())
GROUP BY UserId

UNION ALL 

SELECT UserId,SUM(Points) monthPoints,NULL YearPoints  FROM YourTableName
WHERE YEAR([Date]) = YEAR(GETDATE()) AND MONTH([Date]) = MONTH(GETDATE())
GROUP BY UserId
)

SELECT UserId,SUM(monthPoints) monthPoints,SUM(YearPoints) yearPoints 
FROM ResultCTE GROUP BY UserId


Thanks
Happy Coding.. :)
 
Share this answer
 
v2
Hi Member,

Please try this query for Sql Database

SQL
select SUM(Person.points) as TotalNumberofpointsinCurrrentMonth from Person where MONTH(Person.date)= MONTH(GETDATE()) group by userId

select SUM(Person.points) as TotalNumberofpointsinCurrrentyear from Person where YEAR(Person.date)= YEAR(GETDATE()) group by userId
 
Share this answer
 
Comments
Member 10239935 29-Aug-13 8:37am    
Hi ,
Thanks.
Can i make as single query because it have to bind in grid
as
this manner,separate queries give two table,
userid --TotalNumberofpointsinCurrrentMonth-- TotalNumberofpointsinCurrrentyear
1 ------- ------- 10 ----------------------------- --------------- 18
Member 10239935 29-Aug-13 10:29am    
please use this.its solved
SELECT distinct userId,
(SELECT sum(points)
from tbl_points
WHERE PointsDate between '8/1/2013' and '8/31/2013' and userId= X.UserId) AS CurrentMonthPoints,
(SELECT distinct SUM(points)
from tbl_points
WHERE PointsDate between '1/1/2014' and '12/31/2014' and userId = X.UserId) AS CurrentYearPoints
from tbl_user_performance_points X
here is another query..
select (SELECT SUM(Person.points) from Person where MONTH(Person.date)= MONTH(GETDATE())) as TotalNumberofpointsinCurrrentMonth,
SUM(Person.points) as TotalNumberofpointsinCurrrentyear from Person
group by userId

you can add where clause for outer query for specified year ....
 
Share this answer
 
v2

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