3
select substring(datapath,1,5), COUNT(substring(datapath,1,5)) from batchinfo
where datapath like '%thc%'
group by datapath
having COUNT(substring(datapath,1,5))>1

i am trying to count how many of each substrings there are in the table and for some reason it counts the ENTIRE string. what am i doing wrong?

1
  • can you provide simple example? Commented Dec 10, 2010 at 19:18

2 Answers 2

4

You simply need to GROUP BY the substring you're trying to count instead of the full datapath. There's also no need to repeat the substring function on the count.

select substring(datapath,1,5), COUNT(*) 
    from batchinfo
    where datapath like '%thc%'
    group by substring(datapath,1,5)
    having COUNT(*)>1
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

select d, count(*)
from
(
    select substring(datapath,1,5) d from batchinfo
    where datapath like '%thc%'
)
group by d
having COUNT(*) > 1

1 Comment

I like this because you're not repeating the substring

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.