You need to add an explicit type cast to avoid the error. 'Common'
is just a string literal and Postgres wants you to provide the type it should be cast to.
You will also want to cast the bigint
number in num.new_driver_form
(count()
returns bigint
) to numeric
(or a lossy floating point type) to get a meaningful result from your divison. You will then be interested in the round()
function.
SELECT num.description
,(num.new_driver_form::numeric / denom.form_sent_to) AS conversion
FROM (
SELECT 'Common'::text AS description
,count(DISTINCT u1.user_id) AS new_driver_form
FROM user_tags u1
JOIN user_tags u2 USING (user_id)
WHERE u1.name = 'sentForm'
AND u2.name = 'recForm'
) num
JOIN (
SELECT 'Common'::text AS description
,count(DISTINCT user_id) AS form_sent_to
FROM user_tags
WHERE name = 'sentForm'
) denom ON num.description = denom.description
Of course, your JOIN
doesn't make a lot of sense to begin with, since the second subquery computes a single row and the JOIN condition is always TRUE
. Can be simplified to:
SELECT num.description
,(num.new_driver_form::numeric / denom.form_sent_to) AS conversion
FROM (
SELECT 'Common'::text AS description
,count(DISTINCT u1.user_id) AS new_driver_form
FROM user_tags u1
JOIN user_tags u2 USING (user_id)
WHERE u1.name = 'sentForm'
AND u2.name = 'recForm'
) num
JOIN (
SELECT count(DISTINCT user_id) AS form_sent_to
FROM user_tags
WHERE name = 'sentForm'
) denom ON TRUE
Or just use a subquery:
SELECT 'Common'::text AS description
,(count(DISTINCT u1.user_id)::numeric
/ (SELECT count(DISTINCT user_id)
FROM user_tags
WHERE name = 'sentForm')) AS conversion
FROM user_tags u1
JOIN user_tags u2 USING (user_id)
WHERE u1.name = 'sentForm'
AND u2.name = 'recForm'