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 9231971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:12:06+00:00 2026-06-18T06:12:06+00:00

I have witnessed a strange behaviour while trying to GROUP BY a VARCHAR field.

  • 0

I have witnessed a strange behaviour while trying to GROUP BY a VARCHAR field.

Let the following example, where I try to spot customers that have changed name at least once in the past.

CREATE TABLE #CustomersHistory
(
Id INT IDENTITY(1,1),
CustomerId INT,
Name VARCHAR(200)
)

INSERT INTO #CustomersHistory VALUES (12, 'AAA')
INSERT INTO #CustomersHistory VALUES (12, 'AAA')
INSERT INTO #CustomersHistory VALUES (12, 'BBB')
INSERT INTO #CustomersHistory VALUES (44, '444')

SELECT ch.CustomerId, count(ch.Name) AS cnt
  FROM #CustomersHistory ch
  GROUP BY ch.CustomerId  HAVING  count(ch.Name) != 1

Which oddly yields (as if ‘AAA’ from first INSERT was different from the second one)

CustomerId  cnt  //  (I was expecting)
12          3    //   2
44          1    //   1
  • Is this behaviour specific to T-SQL?
  • Why does it behave in this rather counter-intuitive way?
  • How is it customary to overcome this limitation?

Note: This question is very similar to GROUP BY problem with varchar, where I didn’t find the answer to Why

Side Note: Is it good practice to use HAVING count(ch.Name) != 1 instead of HAVING count(ch.Name) > 1 ?

  • 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-18T06:12:08+00:00Added an answer on June 18, 2026 at 6:12 am

    The COUNT() operator will count all rows regardless of value. I think you might want to use a COUNT(DISTINCT ch.Name) which will only count unique names.

    SELECT ch.CustomerId, count(DISTINCT ch.Name) AS cnt
      FROM #CustomersHistory ch
      GROUP BY ch.CustomerId  HAVING  count(DISTINCT ch.Name) > 1
    

    For more information, take a look at the COUNT() article on book online

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

Sidebar

Related Questions

I witnessed the following weird behavior. I have two functions, which do almost the
I have now witnessed two companies move to agile development with Scrum. In both
Have a painfully simple blog Post creator, and I'm trying to check if the
I have witnessed how Internet Explorer 8 (IE8) is in Windows XP and how
Say, I have the following code: IPrincipal capturedPrincipal = Thread.CurrentPrincipal; myseq.AsParallel().Select(x => { Thread.CurrenctPrincipal
Have the following cronjob set up in root's crontab: (centos 5.x) 2 * *
I have a project in which we would like to do the following :
Have been trying to figure this out for an hour - and still have
Have the following code: $(#blogs).mouseover( function () { $(this).addClass(hover); $(#home).removeClass(hover); $(#homepages).removeClass(hover); $(#apps).removeClass(hover); $(#facebook).removeClass(hover); $(#kontakt).removeClass(hover);
I have a strange issue going on. It may be server related, but I

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.