Here is a slightly different and maybe simpler solution I found:
SELECT (sum(label*r) - 0.5*sum(label)*(sum(label)+1)) / (sum(label) * sum(1-label)) AS auc
FROM (
SELECT label, row_number() OVER (ORDER BY confid) r
FROM T
) t;
that returns the same result as the query in the question.
Update
This SQL query (as well as the one in the question) are non-deterministic when there are multiple examples with the same prediction (confid) but different labels. To compute a deterministic AUC using interpolation the query can be modified as follows:
SELECT (sum(pos*r) - 0.5*sum(pos)*(sum(pos)+1) - 0.5*sum(pos*neg)) /
(sum(pos) * sum(neg)) AS auc
FROM (
SELECT pos, neg,
sum(pos+neg) OVER (ORDER BY confid ROWS UNBOUNDED PRECEDING) r
FROM (
SELECT confid, sum(label) AS pos, sum(1-label) AS neg
FROM T
GROUP BY confid) t
) t;
In the AUC formula, the denominator is the total number of pairs (positive X negative). The numerator computes how many of the pairs are are ranked correctly. sum(pos*r)
computes the total number of pairs so far (based on confidence order). That number includes positive X positive pairs so the second term subtracts those. Finally, the last term subtracts half of positive X negative pairs with the same prediction.