我有以下查询:
SELECT
num.description,
(num.new_driver_form/denom.form_sent_to) as conversion
FROM
(SELECT
'Common' 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' as description,
count(DISTINCT user_id) as form_sent_to
FROM
user_tags
WHERE
name = 'sentForm')denom
ON num.description = denom.description
我收到的错误是“未能找到从未知到文本的转换功能”,但我不确定这意味着什么。
答案 0 :(得分:0)
这是一种简单的方法
SELECT
'cohort' as description,
count(DISTINCT u1.user_id) as new_driver_form,
x.field as field
FROM user_tags u1
JOIN user_tags u2 USING (user_id)
JOIN anothertable X ON x.blah = u1.blah
WHERE
u1.name = 'sentForm'
AND
u2.name = 'recForm'
如果字符串来自其中一个表,那么这样就可以了:
SELECT
<replace with field name> as description,
count(DISTINCT u1.user_id) as new_driver_form,
x.field as field
FROM user_tags u1
JOIN user_tags u2 USING (user_id)
JOIN anothertable X ON x.blah = u1.blah
WHERE
u1.name = 'sentForm'
AND
u2.name = 'recForm'
答案 1 :(得分:0)
您需要添加显式type cast以避免错误。 'Common'
只是一个字符串文字,Postgres希望你提供它应该转换的类型。
您还需要将num.new_driver_form
号码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
(bigint
)转换为count()
returns bigint
(或有损浮点类型)以获得有意义的结果divison。然后,您将对numeric
函数感兴趣。
JOIN
当然,你的TRUE
开始时没有多大意义,因为第二个子查询计算单行而JOIN条件总是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
。可以简化为:
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'
或者只使用子查询:
{{1}}