WQL Collection query using LIKE and NOT LIKE operators

Joshua Wyman 11 Reputation points
2023-08-02T21:27:51.17+00:00

I am trying to figure out something to utilize the AD Security Group membership collected by MECM for the systems in our environment.

Long in short I'm trying to create query-based collections that include/exclude systems based on what Security groups the computer accounts belong to.

I'm targeting the SystemGroupName field which contains a comma-separated list of the AD group membership of my devices.

I essentially need to be able to do the following:
(Sample string: MyDom\Group1, MyDom\Group2, MyDom\Group3, MyDom\Group4)

SELECT <my field values> FROM SMS_R_System WHERE (SMS_R_System.SystemGroupName LIKE "%Group1%") AND NOT (SMS_R_System.SystemGroupName LIKE "%Group2%" AND SMS_R_System.SystemGroupName LIKE "%Group4%")

I've tried using equal to, not equal to, and also varying the parenthesis. I can get results, but essentially it's only the result of the first LIKE statement. It seems it's not taking the results of that match, and then pruning it based on the NOT LIKE portion of things.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
Microsoft Configuration Manager
{count} votes

3 answers

Sort by: Most helpful
  1. Joshua Wyman 11 Reputation points
    2023-08-04T16:19:57.5033333+00:00

    Ok, so I found a way to make this work which seems excessive. I worked it out with a DB admin to try and figure this out because it was not working when it should have. Maybe it's just a MECM quirk regarding it.

    Doing a one-by-one subselect of my criteria worked. So if I had a field that contained four groups, and multiple computers have varying combinations of those values, and I wanted to select certain criteria I had to do this:

    SELECT <criteria> FROM SMS_R_System WHERE SecurityGroupName LIKE %Group1%

    AND
    SMS_R_Sytstem.ResourceID IN (SELECT Select ResourceID FROM SMS_RSystem WHERE SecurityGroupName LIKE %Group3%)

    AND
    SMS_R_Sytstem.ResourceID NOT IN (SELECT Select ResourceID FROM SMS_RSystem WHERE SecurityGroupName LIKE %Group2%)

    AND
    SMS_R_Sytstem.ResourceID NOT IN (SELECT Select ResourceID FROM SMS_RSystem WHERE SecurityGroupName LIKE %Group3%)

    I'm sure there is a way to make this work without having to iteratively filter the returned data but this at least got me the end result without a long duration for my query. As well as me being able to shift the group names in the structure, and flipping the NOT IN to IN to include/disinclude items for other collections

    1 person found this answer helpful.

  2. Erland Sommarskog 120.2K Reputation points MVP
    2023-08-02T21:54:33.73+00:00

    It's not clear what you want to achieve. But the above query means "Give me all rows which contains Group1 unless they also contain Group2 and Group4.

    That is, the above will hit

    Group1, Group2, Group3
    Group1, Group3, Group4
    Group1, Group5, Group7
    

    but not

    Group1, Group2, Group3, Group4, Group5
    

  3. AllenLiu-MSFT 49,216 Reputation points Microsoft External Staff
    2023-08-03T02:47:04+00:00

    Hi, @Joshua Wyman

    Thank you for posting in Microsoft Q&A forum.

    You may try this sentence:

    SELECT <my field values> FROM SMS_R_System WHERE (SMS_R_System.SystemGroupName LIKE "%Group1%") AND (SMS_R_System.SystemGroupName NOT LIKE "%Group2%") AND (SMS_R_System.SystemGroupName NOT LIKE "%Group4%")
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Add comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.