1

I have a problem in query.

Error is: {"Invalid column name 'TotalRecords'."}

I have a table named upload_news in this table there many records and I want retrieve data by country wise where in the distinct countries greater than 20 records.

select count(Distinct country) AS TotalRecords, country from upload_news where TotalRecords > 20";

2 Answers 2

2

You need to use Group By and Having:

select count(Distinct country) AS TotalRecords, country from upload_news 
group by country
having count(Distinct country) > 20
Sign up to request clarification or add additional context in comments.

Comments

1

Try like this

"SELECT *
FROM
(
select count(Distinct country) AS TotalRecords, country from upload_news 
group by country
) T
where TotalRecords > 20";

because TotalRecords alias column, you can't access it directly.

1 Comment

{"Column 'upload_news.country' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."}

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.