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
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, W开发者_如何学PythonP_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
.
The only valid columns are those in the GROUP BY
or an aggregate function.
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. (e.g. two blog entries in one month)
Did you mean to ORDER BY
instead?
Simething like This :
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
精彩评论