with globalAttributes as ( SELECT -1 as attrID, 'Uncategorized' as attrDescription union /* SELECT f.filterid, f.filterlabel, ft.filterTypeId, fv.attrid, fv.attrDescription */ SELECT distinct fv.attrid, fv.attrDescription FROM tbl_CMM_Filters f LEFT JOIN tbl_CMM_FilterTypes ft ON (f.filterTypeID = ft.filterTypeID) INNER JOIN tbl_ORG_AttributeLinks k ON (k.child_attrTypeID = 29 AND k.mstr_attrTypeID = 28 AND k.mstr_attrID = f.filterid) INNER JOIN tbl_ORG_Attributes fv ON (fv.attrID = k.child_attrID) WHERE f.OwnerID = AND f.FSL_TableCode = AND (nullif(ft.filterTypeId,'') is null OR ( ft.filterTypeName <> 'ProductHierarchy' AND ft.filterTypeName <> 'BudgetCategories')) ) #forJSON( 'globalAttributes', attributes.dataStore, 'attrDescription' )#