Slightly indirectly:
SELECT a, b,
COUNT(DISTINCT c) AS num_c,
COUNT(DISTINCT d) AS num_d,
COUNT(DISTINCT e) AS num_e
FROM t
GROUP BY a, b;
This yields:
1 2 1 2 1
3 2 1 1 2
If the num_c
or num_d
or num_e
column has a value greater than 1, then there are varying values. You can vary the query to list whether the column is varying for a given value of (a, b) by using a CASE statement like this:
-- v for varying, n for non-varying
SELECT a, b,
CASE WHEN COUNT(DISTINCT C) > 1 THEN 'v' ELSE 'n' END AS num_c,
CASE WHEN COUNT(DISTINCT d) > 1 THEN 'v' ELSE 'n' END AS num_d,
CASE WHEN COUNT(DISTINCT e) > 1 THEN 'v' ELSE 'n' END AS num_e
FROM t
GROUP BY a, b;
This yields:
1 2 n v n
3 2 n n v
If you really want just to know whether any set of values in the given column varies for any values of (a, b) — and not which values of (a, b) it varies for — you can use the query above as a sub-query in the FROM clause and organize things as you want.
SELECT MAX(num_c) AS num_c,
MAX(num_d) AS num_d,
MAX(num_e) AS num_e
FROM (SELECT a, b,
CASE WHEN COUNT(DISTINCT C) > 1 THEN 'v' ELSE 'n' END AS num_c,
CASE WHEN COUNT(DISTINCT d) > 1 THEN 'v' ELSE 'n' END AS num_d,
CASE WHEN COUNT(DISTINCT e) > 1 THEN 'v' ELSE 'n' END AS num_e
FROM t
GROUP BY a, b
);
This relies on v
being larger than n
; it is easy enough (and convenient enough) for this binary decision, but not necessarily convenient or easy if there are, say, 4 states to map.
This yields:
n v v