【问题标题】:SQL Add a user created results rowSQL 添加用户创建的结果行
【发布时间】:2013-11-06 00:14:10
【问题描述】:

我有以下查询:

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

我收到“未能找到从未知到文本的转换函数”的错误,但我不确定这是什么意思。

【问题讨论】:

  • 描述来自哪里?

标签: sql postgresql


【解决方案1】:

您需要添加明确的type cast 以避免错误。 'Common' 只是一个字符串文字,Postgres 希望您提供它应该转换为的类型。

您还需要将 num.new_driver_form (count() returns bigint) 中的 bigint 数字转换为 numeric(或有损浮点类型),以便从您的除法中获得有意义的结果。然后您将对round() 函数感兴趣。

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

当然,您的JOIN 一开始并没有多大意义,因为第二个子查询计算单行并且JOIN 条件始终为TRUE。可以简化为:

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'

【讨论】:

    【解决方案2】:

    这个方法很简单

    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'
    

    【讨论】:

    • 我知道这是一个后续问题,但我这样做并尝试加入另一个查询,我也添加了“队列”作为描述。这不可能吗?
    • 我在两个表中都创建了“描述”,错误是“找不到从未知到文本的转换函数”
    • @Stuave - 你必须发布 sql,我不知道为什么会出现这个错误
    猜你喜欢
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多