sql query count with like

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
sanctions,notice--3
review,sanctions,notice-2


-----
I am just intered in
sanctions-5
Angela4evaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sdstuberConnect With a Mentor Commented:
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%'
0
 
Angela4evaAuthor Commented:
what if I have more than one field like i want to count sanctions and notices
0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
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

0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
Angela4evaAuthor Commented:
thank you for the suggestion but
I want to display

sanctionscount--1
Noticescount-2
0
 
sdstuberCommented:
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%'
0
 
sdstuberCommented:
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 +'%'
0
 
SharathData EngineerCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.