Query to show support groups and its individual custom attributes

Query to show support groups and its individual custom attributes

PGSQL & MSSQL:

SELECT ci.CINAME AS "Support Group Name", su.first_name "Owned By", ci.DESCRIPTION AS "Description",ia.attributename "Additional Attributes Name",ia.attributevalue "Additional Attributes Value"  FROM BaseElement baseci LEFT JOIN CI ci ON baseci.CIID=ci.CIID LEFT JOIN CIType citype ON ci.CITYPEID=citype.TYPEID LEFT JOIN resources res on res.ciid=ci.ciid LEFT JOIN VendorDefinition resourceVendor ON res.VENDORID=resourceVendor.VENDORID LEFT JOIN SDOrganization aao ON resourceVendor.VENDORID=aao.ORG_ID LEFT JOIN ResourceState state ON res.RESOURCESTATEID=state.RESOURCESTATEID LEFT JOIN instanceattributes ia on ia.ciid=ci.ciid LEFT JOIN QueueDefinition qd ON ci.ciid=qd.ciid LEFT JOIN Queue_email qe on qe.queueid=qd.queueid  LEFT JOIN SupportGroup sg ON sg.ciid=qd.ciid LEFT JOIN AaaUser su ON sg.ownedby=su.user_id LEFT JOIN SiteDefinition site on qd.siteid=site.siteid LEFT JOIN SDOrganization sdo on sdo.org_id=site.siteid where citype.TYPENAME='Support Group' order by 1