开发者

SQL get unique month year combos

开发者 https://www.devze.com 2023-02-02 23:20 出处:网络
SELECTMONTH(sessionStart) AS Expr1, YEAR(sessionStart) AS Expr2 FROMtblStatSessions WHERE(projectID = 187)
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

精彩评论

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

关注公众号