sql query count with like

Posted on 2014-03-19
Medium Priority
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
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
LVL 74

Accepted Solution

sdstuber earned 1000 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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

LVL 74

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 74

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 41

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

765 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