2013-08-29 39 views
0

这是我的表我可以得到这个结果。 每个用户都有积分,我必须显示本月获得的用户积分数,当前年份如何总结列并获得基于月和年的行

谢谢。

-------------------------------- 
| userId | points | date  | 
-------------------------------- 
| 1  | 5  | 8/25/2013 | 
| 1  | 3  | 8/16/2013 | 
| 1  | 2  | 8/01/2013 | 
| 1  | 2  | 9/25/2013 | 
| 1  | 5  | 8/25/2013 | 
| 1  | 3  | 2/16/2012 | 
| 2  | NULL | NULL  | 
| 2  | NULL | NULL  | 
-------------------------------- 

他们的结果应该是这样的:

--------------------------------------------------- 
| userId | CurrentMonthpoints | CurrentYearPoints | 
--------------------------------------------------- 
| 1  | 15     | 17    | 
| 2  | NULL    | NULL    | 
--------------------------------------------------- 

我的要求:

SELECT userId, 
     (SELECT sum(points) 
      from tbl_points 
      WHERE PointsDate between '8/1/2013' and '8/31/2013') AS CurrentMonthPoints, 
     (SELECT distinct SUM(points) 
      from tbl_points 
      WHERE PointsDate between '1/1/2014' and '12/31/2014') AS CurrentYearPoints 
from tbl_user_performance_points 

但我查询错误地显示为:

--------------------------------------------------- 
| userId | CurrentMonthpoints | CurrentYearPoints | 
--------------------------------------------------- 
| 1  | 15     | 17    | 
| 2  | 15     | 17    | 
--------------------------------------------------- 

先感谢

回答

1

试试这个:

select a.userID, 
    a.points_sum_m as currentmonthpoints, 
    b.points_sum_y as currentyearpoints 
from (select userID, sum(points) as points_sum_m 
    from tbl_points 
    where month(date) = month(getdate()) 
    group by userID) a 
inner join (select userID, sum(points) as points_sum_y 
    from tbl_points 
    where year(date) = year(getdate()) 
    group by userID) b 
on a.userID = b.userID; 

SQLFiddle:http://sqlfiddle.com/#!3/ff780/12

+0

由于Zessx表示它太工作正常,没有给出手动日期,非常感谢 –

+0

没问题!我这样做,这样你就不必每个月都改变你的查询日期到当前月份。这将使其始终保持当前的月份和年份。 – Tricky12

0
with month_cte(userid,Current_Month) as 
    (select userid,sum(Points)Current_Month from tbl_points 
    where datepart(mm,PointsDate)=datepart(mm,getdate()) and datepart(YY,PointsDate)=datepart(yy,getdate()) group by userid) 

    , year_cte(userid,Current_year) as 
    (select userid,sum(Points)Current_Year from tbl_points 
    where datepart(YY,PointsDate)=datepart(yy,getdate()) group by userid) 

    select Distinct t.Userid, Current_Month, Current_Year From tbl_points T 
    left join month_cte mc on T.userid=mc.userid 
    left join year_cte yc on t.userid=yc.userid 
0

如果你只是想纠正你的查询与此代替它...

SELECT 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 
+0

令人惊讶的效果很好,半天花了这个。真的时间saved.adined'选择Distint Userid'非常感谢:) :) –

+0

谢谢,很高兴帮助,但它解决你的问题,但是这可能不是做你正在做的事情的最好方法......给乍得的答案一个尝试......我会与他的答案或沿着这条线的东西一起去......它会表现得更好。我还为他的答案+1,使其达到顶峰。 –