0

I need to get the count of News , it gives me error

Column 'News.NewsId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

select    News.NewsId
        , News.NewsTitle
        , COUNT(News.NewsId) as Total 
from News inner join NewsCategory on News.NewsId = NewsCategory.NewsId
where NewsCategory.CategoryId in (Select CategoryId 
                                  from Category 
                                  where CategoryName = 'travel' 
                                    or  CategoryName = 'Technology' 
                                    or  CategoryName = 'Sport')
1
  • Are you wanting to aggregate the results by newsid? Meaning do you want multiple records returned? If so, you are simply missing the group by clause. Add this to the end: group by news.newsid, news.newstitle... If not, you should better define your question... Commented Jul 27, 2016 at 2:30

3 Answers 3

0

use ti like this:

select    News.NewsId, News.NewsTitle, COUNT(News.NewsId) as Total from News  group by  News.NewsId, News.NewsTitle
inner join 
NewsCategory
on News.NewsId = NewsCategory.NewsId where NewsCategory.CategoryId in (Select CategoryId from Category where CategoryName = 'travel' or  CategoryName = 'Technology' or CategoryName = 'Sport')'Technology' or CategoryName = 'Sport')

You are using count that's why you need to group by the field,include at your count(News.NewsId)

This one displays count for every category according to each NewsId:

select count(1), a.NewsId, a.NewsTitle,b.category from
(select  NewsId, NewsTitle from tbl_news)as a


left JOIN

(select    NewsId, categoryid,Category  from tbl_category) as b
on a.newsid = b.newsid GROUP BY a.NewsTitle,b.category 
Sign up to request clarification or add additional context in comments.

5 Comments

It's working, thank you :) But the count is always 1, I need to get the Count or the number of news under each category. like category Sport has 2 news and category Technology has 4 news, Can you please help me to solve this, Thank you
you mean count per each category?
Yes ... Thank you so much for your help :)
helloo are you there?
i don't even know your db design.. just making some solution for you. Hope you can add more details here for your result
0

You need to group by the column NewsId and NewsTitle

select    News.NewsId
        , News.NewsTitle
        , COUNT(News.NewsId) as Total 
from News inner join NewsCategory on News.NewsId = NewsCategory.NewsId
where NewsCategory.CategoryId in 
                        (Select CategoryId 
                         from Category 
                         where CategoryName IN ('travel','Technology','Sport'))
GROUP BY News.NewsId
        , News.NewsTitle

Or you can write the query as

select    News.NewsId
        , News.NewsTitle
        , COUNT(News.NewsId) as Total 
from News 
inner join NewsCategory on News.NewsId = NewsCategory.NewsId
inner join Category     on Category.CategoryId = NewsCategory.CategoryId
where Category.CategoryName IN ('travel','Technology','Sport'))
GROUP BY News.NewsId, News.NewsTitle

Comments

0

Just write this using two joins and a group by:

select n.NewsId, n.NewsTitle, COUNT(*) as Total 
from News n inner join
     NewsCategory nc
     on n.NewsId = nc.NewsId join
     Category c
     on nc.CategoryId = c.CategoryId
where c.CategoryName in ('travel', 'Technology', 'Sport')
group by n.NewsId, n.NewsTitle;

1 Comment

It's working, thank you :) But the count is always 1, I need to get the Count or the number of news under each categories. like category Sport has 2 news and category Technology has 4 news, Can you please help me to solve this, Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.