SELECT A.ID, SUM(A.COUNTS) AS COUNT_TOTAL FROM ( SELECT X AS ID, COUNT(*) AS COUNTS FROM TABLE1 GROUP BY X UNION ALL SELECT Y AS ID, COUNT(*) AS COUNTS FROM TABLE1 GROUP BY Y ) A GROUP BY A.ID ORDER BY A.ID;