2013-08-29 74 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,使其達到頂峯。 –