```
SELECT
COUNT(CASE WHEN x1 = 1 THEN 1 END) AS x1_1
, COUNT(x1) AS x1
, CASE WHEN COUNT(x1) > 0 THEN
COUNT(CASE WHEN x1 = 1 THEN 1 END) * 100.0 / COUNT(x1)
ELSE 0
END AS x1_pct
, COUNT(CASE WHEN x2 = 1 THEN 1 END) AS x2_1
, COUNT(x2) AS x2
, CASE WHEN COUNT(x2) > 0 THEN
COUNT(CASE WHEN x2 = 1 THEN 1 END) * 100.0 / COUNT(x2)
ELSE 0
END AS x2_pct
FROM table1
;
```

The aggregate function COUNT() will only count non-null values (in other words it ignores nulls)So the result of the query above is:

```
| X1_1 | X1 | X1_PCT | X2_1 | X2 | X2_PCT |
|------|----|-----------------|------|----|--------|
| 2 | 3 | 66.666666666666 | 1 | 2 | 50 |
```

But do remember that divide by zero isn't allowed so if COUNT(X1) is zero you cannot attempt the percentage calculationsee: http://sqlfiddle.com/#!3/439ed/1