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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:37:22+00:00 2026-06-11T06:37:22+00:00

I have a sample data as: Johnson; Michael, Surendir;Mishra, Mohan; Ram Johnson; Michael R.

  • 0

I have a sample data as:

Johnson; Michael, Surendir;Mishra, Mohan; Ram
Johnson; Michael R.
Mohan; Anaha
Jordan; Michael
Maru; Tushar

The output of the query should be:

Johnson; Michael   2
Mohan; Anaha       1
Michael; Jordon    1
Maru; Tushar       1
Surendir;Mishra    1
Mohan; Ram         1

As you can see it is print the count of each name separated by , but with a twist. We cannot simply do a groupby on full name because sometimes the name may contain middle name 1st initial and sometimes it may not. Eg. Johnson; Michael and Johnson; Michael R. are counted as single name and hence their count is 2. Further either Johnson; Michael should appear or Johnson; Michael R. should appear in resultset with count of 2 (not both because that would be repeated record)

The table contains names separated by , and it is not possible to denormalize it as it is LIVE and given to us by someone else.

Is there anyway to write a query for this without using cursor? I have around 3 million records in my DB and I have to support pagination etc also. What do you think would be the best way to achieve this?

  • 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-11T06:37:24+00:00Added an answer on June 11, 2026 at 6:37 am

    This is why your data should be normalised.

    ;with cte as  
    ( 
        select 1 as Item, 1 as Start, CHARINDEX(',',People+',' , 1) as Split, 
               People+',' as People 
        from YourHorribleTable
        union all 
        select cte.Item+1, cte.Split+1, nullif(CHARINDEX(',',people, cte.Split+1),0), People as Split 
        from cte 
        where cte.Split<>0   
    )    
    select Person, COUNT(*)
    from
    (
    select case when nullif(charindex (' ', person, 2+nullif(CHARINDEX(';', person),0)),0) is null then person  
        else substring(person,1,charindex (' ', person, 2+nullif(CHARINDEX(';', person),0)))
        end as Person
    from
    (
    select LTRIM(RTRIM( SUBSTRING(people, start,isnull(split,len(People)+1)-start))) as person
    from cte  
    ) v
    where person<>''
    ) v
    group by Person
    order by COUNT(*) desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sample data akb.ggb@yahoo.com should output akb ggb sdsd.sdsd@gmail.com should output sdsd
Sample Data : input : abcdacdc Output : cadb here we have to sort
I have a sample data set below: Date 01/01/2010 01/02/2010 01/03/2010 Running the query
I have a sample data table(id, name, date_create) table(1, 'Iphone 4S', '2012-06-18 17:19:05') //
I have a sample data game(id, name) 1 | Avatar 2 | Angry Bids
I have below sample data. AID Date Title ----- ---------- ------ 1 2011-12-12 test1
I have the following sample data: Id Name Quantity 1 Red 1 2 Red
I have the following sample of data to insert into tables(from parent to child,
1I have the following two tables (sample data) and need to be able to
I have posted my class below and with sample data. I need to pass

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.