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.
Select ToolCode, Len(ToolCode), Count(WorkCenter)
FROM JT_ToolCode, JT_WorkCenter
WHERE ToolCode = Left(Workcenter,Len(ToolCode))
Group By ToolCode, WorkCenter
2
Answers
While not efficient this would work by simply joining on a like.
Demos
Note: this assumes any occurrence of a toolcode in a workcenter is desired a like to the toolcode. however you could get false or double counts if you have tool codes like Drill and PneumaticDrill, as Drill will include PneumaticDrill in it’s count. as in this demo
Or using an inline view which runs for each row in the first table getting a count from the second
Giving us:
You can try something like this:
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.