简体   繁体   English

SQl:按月和年分组

[英]SQl: Group by month and year

Hi my code doesn't work, Im trying to group my blogentries by year and month here's my sql 嗨,我的代码不起作用,我试图按年份和月份对博客条目进行分组,这是我的SQL

SELECT * FROM(
    SELECT WP_BlogEntries.BlogEntryID, WP_BlogEntries.AddedDate,
        WP_BlogEntries.AddedBy, WP_BlogEntries.BlogID, 
        WP_BlogEntries.Title, WP_BlogEntries.Description, WP_BlogEntries.Body,
        WP_BlogEntries.ReleaseDate, WP_BlogEntries.ExpireDate,
        WP_BlogEntries.Approved, WP_BlogEntries.Listed,
        WP_BlogEntries.CommentsEnabled, WP_BlogEntries.OnlyForMembers,
        WP_BlogEntries.ViewCount, WP_BlogEntries.Votes, 
        WP_BlogEntries.TotalRating
    FROM WP_BlogEntries
    WHERE WP_BlogEntries.ReleaseDate < GETDATE()
        AND WP_BlogEntries.ExpireDate > GETDATE()
        AND Approved = 1
        AND Listed = 1
        AND WP_BlogEntries.BlogID = @BlogID) MonthEntries 
    GROUP BY YEAR(ReleaseDate), MONTH(ReleaseDate)

It would be helpful to know the error message. 了解错误消息将很有帮助。

You can't do a SELECT * FROM if you specify GROUP BY . 如果指定GROUP BY则不能执行SELECT * FROM

The only valid columns are those in the GROUP BY or an aggregate function. 唯一有效的列是GROUP BY或聚合函数中的列。

If you group by year and month, then each row will contain one year and month, it is impossible for SQL to know which other columns to display as there could be more than one. 如果按年和月分组,则每一行将包含一年和一个月,SQL无法知道要显示哪些其他列,因为可能会有不止一个。 (eg two blog entries in one month) (例如,一个月内有两个博客条目)

Did you mean to ORDER BY instead? 您是说要ORDER BY吗?

像这样的事情: select convert(varchar(50),YEAR(date)) +'/'+convert (varchar(50), MONTH(date)) ,Name , COUNT( Name) ,[DATE] from table1 group by convert(varchar(50),YEAR(date)) +'/'+convert (varchar(50), MONTH(date)) ,Name,[date] order by [Date] Desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM