Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Database View with Left Join show unwanted "duplicate" records

Robert Campbell
Tera Guru

I want to show

    1. All cis that
      1. u_core_ci = true
      2. sys_class_name = 'cmdb_ci_service' || sys_class_name = 'u_cmdb_ci_business_application'
    2. If they have a parent ci that is a business application in the ci relationship table, show that relationship, otherwise just show the ci

 

TableActiveLeft JoinOrderVariable prefixWhere clause
cmdb_citruefalse100c1 
cmdb_citruefalse150c2

c1.sys_id=c2.sys_id

&& c2.u_core_ci=true

&& (c2.sys_class_name='cmdb_ci_service' || c2.sys_class_name='u_cmdb_ci_business_application')

cmdb_rel_citruetrue200r1

r1.child=c2.sys_id

cmdb_ci_business_apptruetrue300b3b3.sys_id=r1.parent

 

This gives me mostly what I want except that it has some duplicate c2.  I'd like to not have these duplicates.  I think it's because of the last row above.  I tried adding "&& r1.parent.sys_class_name='cmdb_ci_business_app'" 3rd row but that returns no records.

 

I ran the query and got the results I wanted in mysql like this:

 

 

select 
  c1.name
  ,r1.parent 
from 
  cmdb_ci c1 
left join 
  cmdb_rel_ci r1 
on 
  r1.child = c1.name 
left join 
  cmdb_ci_business_app b1 
on 
  b1.name = r1.parent 
where 
  c1.u_core_ci = 'true' 
  and c1.sys_class_name='Business Service' 
    or c1.sys_class_name='Business Application';

 

2 REPLIES 2

Robert Campbell
Tera Guru

I've updated but it still doesn't work. 

RobertCampbell_0-1677862687764.png

 

How do you use the OR in the WHERE clause?

Robert Campbell
Tera Guru

Because of the amount of data, I tried this on a PDI.  I added the u_core_ci true/false column to the cmdb_ci_service table (and extended tables).

RobertCampbell_1-1678304197648.png

Now I have 10 services, 1 cmdb_ci_service_calculated and 9 cmdb_ci_service with u_core_ci = true.

 

I created one relationship between cmdb_ci_service_calculated and cmdb_ci_business_app and then created this view:

 

 

  1. RobertCampbell_0-1678303375975.png
    1. RobertCampbell_8-1678304972739.png
  2. RobertCampbell_12-1678305327907.png
    1. RobertCampbell_8-1678304972739.png
  3. RobertCampbell_9-1678305051887.png

     

    1. RobertCampbell_3-1678304458717.png
  4. RobertCampbell_7-1678304862687.png

     

    1. RobertCampbell_4-1678304522414.png
    2. What didn't show here is the one cmdb_ci_service that didn't have any parent at all.
  5. RobertCampbell_11-1678305234844.png
    1. RobertCampbell_10-1678305213888.png
  6. RobertCampbell_14-1678305545336.png
    1. RobertCampbell_15-1678305565262.png
  7. RobertCampbell_6-1678304802039.png
    1. RobertCampbell_5-1678304767609.png
  8. RobertCampbell_13-1678305444416.png
    1. RobertCampbell_10-1678305213888.png

 

I expected one of the results to show 10 records with only one of them showing a parent.