SCCM 2012 SP1 Collection query based on DN

I had problem to create query that would show me all systems from specific OU. I have tested all queries that I could find in internet but none of them returned all computers only some. Perhaps it has to do something with our sccm setup (sccm in one domain and all clients in another untrusted domain). After few days struggle with it I almost gave up when it hit me. Why to try all the time to use SMS_R_System.SystemOUName when it clearly does not work for me instead I have used SMS_R_System.DistinguishedName with “like” operator and wildcard in computer name to pull all computers from specific OU. Now it works like a charm. The query looks like this

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,
SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,
SMS_R_SYSTEM.ResourceDomainORWorkgroup,
SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.DistinguishedName like “CN=%,OU=Windows 7,OU=Laptops,DC=domain,DC=com”

This query can be tweaked to pull systems from specific OU based on some prefix in their name, for example

%-D for desktops

%-L for Laptops