Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count # of valus that exist in column string

I am trying to count # of rows where a value exists in any string of another table. Here is my example data:

ToolCode
Drill
Screwdriver
Saw
WorkCenter
Screwdriver1
Drill1
Saw2
Saw3
Screwdriver15
Drill 12
Saw10

So the totals would be:

Tool Code Quantity
Drill 2
Saw 3
Screwdriver 2

I attempted to use different count by len functions, but was unable to make it work. I am hoping for help in this matter and getting an example of what to do.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Select ToolCode, Len(ToolCode), Count(WorkCenter)
FROM JT_ToolCode, JT_WorkCenter
WHERE ToolCode = Left(Workcenter,Len(ToolCode))
Group By ToolCode, WorkCenter

>Solution :

You can try something like this:

SELECT ToolCode.ToolCode, COUNT(*) AS Quantity
FROM ToolCode
JOIN WorkCenter ON WorkCenter.WorkCenter LIKE CONCAT('%', ToolCode.ToolCode, '%')
GROUP BY ToolCode.ToolCode;

This query looks at both the ToolCode and WorkCenter tables. It checks if each tool is mentioned in any work center. Then, it counts how many times each tool appears in the work centers and groups the results by tool.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading