SELECT [PROJ_NAME],MAX(DBARCHIVE) AS DBARCHIVE,MAX(DBDRAFT) AS DBDRAFT,
MAX(DBPUBLISHED) AS DBPUBLISHED,MAX(DBREPORTING) AS DBREPORTING,
COUNT([PROJ_NAME]) AS CANTIDAD
FROM (
SELECT [PROJ_NAME],
CASE WHEN DB = 'Archive' THEN 'X' ELSE '' END AS DBARCHIVE,
CASE WHEN DB = 'Draft' THEN 'X' ELSE '' END AS DBDRAFT,
CASE WHEN DB = 'Published' THEN 'X' ELSE '' END AS DBPUBLISHED,
CASE WHEN DB = 'Reporting' THEN 'X' ELSE '' END AS DBREPORTING
FROM (
SELECT [PROJ_NAME],'Archive' AS DB
FROM [ProjectServer_Archive].[dbo].[MSP_PROJECTS]
UNION
SELECT [PROJ_NAME],'Draft' AS DB
FROM [ProjectServer_Draft].[dbo].[MSP_PROJECTS]
UNION
SELECT [PROJ_NAME],'Published' AS DB
FROM [ProjectServer_Published].[dbo].[MSP_PROJECTS]
UNION
SELECT [ProjectName],'Reporting' AS DB
FROM [ProjectServer_Reporting].[dbo].[MSP_EpmProject]
) Q GROUP BY [PROJ_NAME],DB
) R GROUP BY [PROJ_NAME]
HAVING COUNT([PROJ_NAME]) < 4
ORDER BY CANTIDAD,[PROJ_NAME]