开发者

SQL get unique month year combos

SELECT     MONTH(sessionStart) AS Expr1, YEAR(sessionStart) AS Expr2
FROM         tblStatSessions
WHERE     (projectID = 187)
GROUP BY sessionStart

This returns:

11 | 2010

11 | 2010

11 | 2010

12 | 2010

12 | 2010

But I need it to only return each instance once, IE:

11 | 2010

12 | 2开发者_如何学C010

If that makes sense!


The following should be what you want:

SELECT     MONTH(sessionStart) AS Expr1, YEAR(sessionStart) AS Expr2
FROM         tblStatSessions
WHERE     (projectID = 187)
GROUP BY MONTH(sessionStart), YEAR(sessionStart)

in general you need to group by every non-aggregate column that you are selecting. Some DBMSs, such as Oracle, enforce this, i.e. not doing so results in an error rather than 'strange' query execution.


Try:

SELECT     MONTH(sessionStart) AS Expr1, YEAR(sessionStart) AS Expr2
FROM         tblStatSessions
WHERE     (projectID = 187)
GROUP BY Expr1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜