sql query count with like

Posted on 2014-03-19
Last Modified: 2014-07-31
I have field called dipose and it has several values delimited by commas

now I want to count the number of time  a word 'sanctions' appeared

where I do
select dipose,count(*)
from table1

where dipose like '%sanctions%'
group by dipose

I am getting several records with counts
dipose                     count

I am just intered in
Question by:Angela4eva
LVL 73

Accepted Solution

sdstuber earned 250 total points
ID: 39940139
you'll need to take the dispose column out of the grouping if you don't want sums groupd by the individual dispose values

select count(*)
from table1
where dipose like '%sanctions%'

if you want the word "sanctions" to appear in the results, you can hardcode that

select 'sanctions' , count(*)
from table1
where dipose like '%sanctions%'

Author Comment

ID: 39940300
what if I have more than one field like i want to count sanctions and notices
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39940327
I will suggest the below query, though I must presume that the starting point (table design) is not really optimal for this query of queries...
select sum(case when dipose like '%sanctions%' then 1 else 0 end) santions_count
, sum(case when dipose like '%notices%' then 1 else 0 end) notices_count
from table1

Open in new window

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.


Author Comment

ID: 39940348
thank you for the suggestion but
I want to display

LVL 73

Expert Comment

ID: 39940438
if having the results in one row isn't sufficient you could unpivot them or do a simple union
(the unpivot is more efficient though)

select 'sanctions' , count(*)
from table1
where dipose like '%sanctions%'
union all
select 'notices' , count(*)
from table1
where dipose like '%notices%'
LVL 73

Expert Comment

ID: 39940448
an "unpivot" might look something like this with a dummy join

select x.str,count(*) from
 (select 'sanctions' as str union all select 'notices' as str) as x, table1
where dispose like '%' + str +'%'
LVL 40

Expert Comment

ID: 39941464
Post# 39940327 would work for you. The other approach is convert the comma separated values to rows and count the values. Let me know if you are interested in that solution.

Featured Post

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.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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

22 Experts available now in Live!

Get 1:1 Help Now