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
  • 5
  • 3
LVL 142

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 142

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"
LVL 142

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

LVL 12

Author Comment

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

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 142

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Implementing SQL Server Data Files in Azure 1 35
Query Peformance + mulitple query plans 9 47
Max Consumption Rate (MCR) 3 33
SQL Exceptions 3 37
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now