Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8343475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:04:41+00:00 2026-06-09T06:04:41+00:00

Something very simple, but I cannot get it working for me :) select x.name,

  • 0

Something very simple, but I cannot get it working for me 🙂

select x.name, count(x.name) from <table_name> x where ...<complex and long>...

It throws an error, that the x.name isn’t used with group by.

select x.name from <table_name> x where ...<complex and long>...

works fine and returns e.g. 6 names

select count(x.name) from <table_name> x where ...<complex and long>...

works also fine and returns e.g. the number 6

But the combination is not working, when I tried to add the group by:

select x.name, count(x.name) from <table_name> x where ...<complex and long>...
group by x.name

it worked, but all counts were 1 and not 6.

The thing is, I could firstly get the count into variable, then write the long sql statement just to get the names, but I don’t want to write the long complex select statement twice. There has to be some way to do the combination in one select: get all names and by the way tell me how many they were.

Thanks

P.S.

name    bla1    bla2
a       ...     ...     
a       foo     ...     
b       foo     ...     
c       ...     ...     
d       foo     ...     
d       foo     ...     
b       foo     ...     
c       foo     ...     

The result of x.name where bla1 = foo would be:

a
b
d
d
b
c

The result of count(x.name) where bla1 = foo would be:

6

My desired result of:

...variable definitions
select @foundName = x.name, @numOfAllFoundNames = count(x.name)
from <table_name> x where ...<complex and long>...

should be:
@foundName = a (just one of the names, no matter which one)
@numOfAllFoundNames = 6

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:04:43+00:00Added an answer on June 9, 2026 at 6:04 am

    The simplest way is to use @@rowcount after the query;

    select x.name from <table_name> x where ...<complex and long>...
    
    select 'the number of rows is:', @@rowcount
    

    As an aside, if you request a non-aggregate field (name) and an aggregated field (count(name)) you must provide a group by to tell the server what to count; you see 1’s because group by name applies the count to every distinct name in the set – E.g. you would see 1 less row and 2 if a name was repeated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be missing something very simple, but can't find the answer to this.
I am new to Raphael and am trying to do something very simple, but
I am new to Raphael and am trying to do something very simple, but
Very simple questions guys, but maybe I'm just forgetting something. In 64bit linux, a
This is a very simple question, but I can't seem to find something about
I'm struggling with something that I am sure has a very simple method, but
I'm trying to make something very simple.. but I do wrong, and I don't
I'm trying something very simple. Upon page load the data in content div is
I'm trying to do something very simple, yet I can't seem to find the
I am trying to accomplish something very simple, and I'm hoping someone can point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.