0

Below shows my data table name called "company"

enter image description here

Here is my SQL query

SELECT 
    name, COUNT(DISTINCT num) AS count
FROM 
    company
WHERE 
    (num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY 
    name

After run this query it shows below result

enter image description here

But I need below result. Please help me to solve my problem. Thank you

enter image description here

4
  • Why are you using the like operator for the num column, is it a varchar? Commented May 11, 2015 at 8:15
  • 1
    So you want to count the distincts numbers with only 2 first digits ? Commented May 11, 2015 at 8:21
  • yes it is in varchar Commented May 11, 2015 at 8:21
  • yes only with first 2 digits Commented May 11, 2015 at 8:23

1 Answer 1

4

I assume you want to have the distinct on the first 2 characters of the column.

You can use the function LEFT() for this:

Query

SELECT name , COUNT(DISTINCT LEFT(num, 2)) AS count
FROM new_table
WHERE ( num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY name
Sign up to request clarification or add additional context in comments.

Comments

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.