Solved

sql query count with like

Posted on 2014-03-19
7
237 Views
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
sanctions,notice--3
review,sanctions,notice-2


-----
I am just intered in
sanctions-5
0
Comment
Question by:Angela4eva
7 Comments
 
LVL 73

Accepted Solution

by:
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%'
0
 

Author Comment

by:Angela4eva
ID: 39940300
what if I have more than one field like i want to count sanctions and notices
0
 
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

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

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

sanctionscount--1
Noticescount-2
0
 
LVL 73

Expert Comment

by:sdstuber
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%'
0
 
LVL 73

Expert Comment

by:sdstuber
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 +'%'
0
 
LVL 40

Expert Comment

by:Sharath
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

777 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