对于 SQL 服务器,这应该可以工作:
SELECT coalesce(v.y, s.y) y, coalesce(v.m, s.m) m, coalesce(v.d, s.d) d,
100.0 * isnull(s.num, 0) / (isnull(s.num, 0) + isnull(v.num, 0)) conversion
FROM
(
SELECT datepart(YEAR, [date]) y, datepart(m, [date]) m,datepart(d, [date]) d,
count(action) num
FROM actions
WHERE action = 'visit'
GROUP BY datepart(YEAR, [date]), datepart(m, [date]), datepart(d, [date])
) v
FULL OUTER JOIN
(
SELECT datepart(YEAR, [date]) y, datepart(m, [date]) m,datepart(d, [date]) d,
count(action) num
FROM actions
WHERE action = 'signup'
GROUP BY datepart(YEAR, [date]), datepart(m, [date]), datepart(d, [date])
) s
ON v.y = s.y AND v.m = s.m AND v.d = s.d
我将字符串连接、舍入和一般鲁棒性作为练习留给你。