SELECT COUNT(DISTINCT(cust_cod)) , no_of_mon , v2.total-COUNT(DISTINCT(cust_cod)) AS total FROM churn CROSS JOIN ( SELECT SUM(cnt) as total FROM ( SELECT COUNT(DISTINCT(cust_cod)) as cnt FROM churn WHERE sales_reg = '80' AND pack_cod = 'UTH0021' GROUP BY no_of_mon) AS v1) AS v2 WHERE sales_reg = '80' AND pack_cod = 'UTH0021' GROUP BY no_of_mon, v2.total;
count | no_of_mon | total
-------+-----------+-------
2 | 6 | 1577
9 | 7 | 1570
11 | 8 | 1568
12 | 9 | 1567
17 | 10 | 1562
31 | 11 | 1548
44 | 12 | 1535
1162 | 13 | 417
291 | 14 | 1288
this is the actual o/p
but not the expected