tags:

views:

51

answers:

1

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, 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)
+2  A: 

It would be helpful to know the error message you have and your 0% accept rate doesn't encourage people to help you.

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?

Chris Diver
I want to create a list in asp.net of the year and then the months a blog entry was entered
littleMan
Would I use DISTINCT in the statement
littleMan
`SELECT YEAR(ReleaseDate), MONTH(ReleaseDate)` at the start should do it, you wouldn't need `DISTINCT`
Chris Diver