Grouping by Free Text Field

Posted on 2011-03-18
Last Modified: 2012-05-11
Hi All,

I am in a dilemma, I have this database I need to pull a count from but then this is to be based on a free text field. The data is recorded as follows:

Product Name
Delegate 1 Email Address
Delegate 1 Name
Delegate 1 Job Title
Delegate 2 Email Address
Delegate 2 Name
Delegate 2 Job Title

So from the data above the count should be '2', I just need to be able to get the count when looping through the database to know how many delegates a customer has filled in.

Question by:pigmentarts
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35165163
this sounds like some bad design, but anyhow:
can you clarify that the "count" will be based on the field's 2 first "words" aka everyting before the second space?
LVL 12

Author Comment

ID: 35165403
Sorry didn't make it clear, that is what is stored in 1 field i.e  'Delegate 1 Email Address'
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35165424
I understood that. so, please clarify if the rule is: "group by the first 2 words in the field value, aka everything before the second space in the string"
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35165440
anyhow, here some example:
declare @demo_table table  ( product_name varchar(100) )
insert into @demo_table values ( 'Delegate 1 Email Address')
insert into @demo_table values ( 'Delegate 1 Name')
insert into @demo_table values ( 'Delegate 1 Job Title')
insert into @demo_table values ( 'Delegate 2 Email Address')
insert into @demo_table values ( 'Delegate 2 Name')
insert into @demo_table values ( 'Delegate 2 Job Title')

select left( product_name, charindex(' ', product_name, charindex(' ', product_name) + 1 ))
     , count(*)
  from @demo_table
group by left( product_name, charindex(' ', product_name, charindex(' ', product_name) + 1 ))

Open in new window

LVL 12

Author Comment

ID: 35165848
Yes angelIII I need to group by say the number of email addresses
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35166172
well, unless you clarify EXACTLY the rules to "count", I cannot give more concrete code than above.
LVL 12

Author Comment

ID: 35166276
I need to group by the number of times 'Email Address' appears in the list so I can get the count in this case which should be '2'. Sorry
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 35166506
that is easy:
select count(*) from yourtable where product_name like '%Email Address%'

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question