0

I have a table of Matricies (JobMatricies) ID, Desc, DeptIDs

1   Admin (PM)  6,7,138,131,11,9,10,134,135,14,105,129
5   Sales Processing (PM)   92,16,153,17,91,32,26,93,99,18,89,90,155,19
6   Construction Processing (PM)    100,36,20,136,22,88,23,25,34,106,38,39,132,41,42,43,154,152,84

DeptIDs are a Comma Delimited list of departments that I want to use to count how many records are represented by the Matrix.

Normally I would do something like....

select  Matrix_ID, 
        Matrix_Desc, 
        JobCount =  (select count(sched_ID) from JobSchedule where dept_ID in                                                    (**92,16,153,17,91,32,26,93,99,18,89,90,155,19**))
from    jobMatrices

How do I replace the hard coded delimited string with the ID's stored with each matrix, so that I can produce a list of matricies with their own unique count based on the comma delimited string that is stored with each matrix.

Thanks

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 4
    Fix your data model to be a proper relational model, where you do not store multiple numeric ids in a single string column. I'm sure that will simplify most queries you want to write, including this one. – Gordon Linoff Jul 08 '16 at 16:31
  • Thanks Gordon, appreciate the comments. Yes, we can add another table to store each ID as a separate record then use a sub select. Is there a way to solve the issue with how the data is currently stored? – Scott Terrell Jul 08 '16 at 16:42
  • 1
    What is your RDBMs ? Different flavors has different ways to handle comma separated values. – Jorge Campos Jul 08 '16 at 16:45
  • In Postgres you can use `cardinality(string_to_array(deptids,','))` –  Jul 08 '16 at 17:44
  • You will probably find an implementation of a `split` function appropriate for your database if you search for that. – shawnt00 Jul 08 '16 at 18:13

3 Answers3

0

I just answered a similar question where the poster wanted to sum the delimited list of numbers in a similar table layout. My solution used CTE's in Oracle to turn that list into a CTE table which would allow you to join against it. I believe that technique would be of use here if your RDBMS supports that. Please have a look: https://stackoverflow.com/a/38231838/2543416

Community
  • 1
  • 1
Gary_W
  • 9,933
  • 1
  • 22
  • 40
0
select s.ID
     , m."Desc"
  from JobSchedule s
     , jobMatrices m
 where position(',' || s.ID || ',' in ',' || m.DeptIDs || ',')>0;

select m."Desc"
     , count(s.ID) JobCount
  from JobSchedule s
     , jobMatrices m
 where position(',' || s.ID || ',' in ',' || m.DeptIDs || ',')>0
 group by m."Desc";
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • Hi there, welcome to SO, next time take your time to Format your codes in your future answers. Just adding a blank line before the text and four white spaces before every line of code. – Jorge Campos Jul 08 '16 at 17:53
0

In MySQL, you can treat a comma-separated string as a SET, and there's a builtin function FIND_IN_SET() that helps:

select  m.Matrix_ID, 
        m.Matrix_Desc, 
        (select count(sched_ID) from JobSchedule 
         where FIND_IN_SET(dept_ID, m.DeptIds)) AS JobCount
from    jobMatrices AS m;

This will have terrible performance, however.

If you use some RDBMS other than MySQL, they may have a different solution that works similarly.

You should be specific in your question and tag your question appropriately. You only tagged your question , but this is a language used by many RDBMS vendors.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828