结合SUM和CAST - 不工作?

时间:2013-08-10 17:51:00

标签: postgresql casting sum

PostgreSQL Unicode 9.01不喜欢:

SELECT table1.fielda,
SUM (CAST (table2.fielda AS INT)) AS header.specific
FROM *etc*

SUM-CAST有什么问题?

错误讯息:

  

列表达式不正确:'SUM(CAST   (specifics_nfl_3pl_work_order_item.delivery_quantity AS INT))

查询:

SELECT specifics_nfl_3pl_work_order.work_order_number,
specifics_nfl_3pl_work_order.goods_issue_date,
specifics_nfl_3pl_work_order.order_status_id,
SUM (CAST (specifics_nfl_3pl_work_order_item.delivery_quantity AS INT)) AS units
FROM public.specifics_nfl_3pl_work_order specifics_nfl_3pl_work_order,
public.specifics_nfl_3pl_work_order_item specifics_nfl_3pl_work_order_item,
public.specifics_nfl_order_status specifics_nfl_order_status
WHERE specifics_nfl_3pl_work_order.order_status_id In (3,17,14) 
AND specifics_nfl_3pl_work_order_item.specifics_nfl_work_order_id=
specifics_nfl_3pl_work_order.id 
AND ((specifics_nfl_3pl_work_order.sold_to_id<>'0000000000') 
AND (specifics_nfl_3pl_work_order.goods_issue_date>={d '2013-08-01'}))

如果你能提供帮助,真的很棒。

3 个答案:

答案 0 :(得分:2)

如果我是你,那么我会采取以下措施:

  • 给你的表短的别名
  • 格式化查询
  • 使用正确的ANSI连接:
  • 删除函数名称和(
  • 之间的空格

select
    o.work_order_number,
    o.goods_issue_date,
    o.order_status_id,
    sum(cast(oi.delivery_quantity as int)) as units
from public.specifics_nfl_3pl_work_order as o
    inner join public.specifics_nfl_3pl_work_order_item as oi on
       oi.specifics_nfl_work_order_id = o.id
    -- inner join public.specifics_nfl_order_status os -- seems redundant
where
    o.order_status_id In (3,17,14) and
    o.sold_to_id <> '0000000000' and
    o.goods_issue_date >= {d '2013-08-01'}

其实我觉得你在这里需要分组:

select
    o.work_order_number,
    o.goods_issue_date,
    o.order_status_id,
    sum(cast(oi.delivery_quantity as int)) as units
from public.specifics_nfl_3pl_work_order as o
    inner join public.specifics_nfl_3pl_work_order_item as oi on
       oi.specifics_nfl_work_order_id = o.id
where
    o.order_status_id In (3,17,14) and
    o.sold_to_id <> '0000000000' and
    o.goods_issue_date >= {d '2013-08-01'}
group by
    o.work_order_number,
    o.goods_issue_date,
    o.order_status_id

如果它仍然不起作用 - 尝试评论总和,看看它是否有效?

答案 1 :(得分:0)

但是你有一个table2或者只有table1?

尝试:
    SELECT table1.fielda,
    SUM(CAST(table1.fielda AS INT))AS“header.specific”
    来自

答案 2 :(得分:0)

除了@Roman already cleared up之外,还有更多问题:

SELECT o.work_order_number
      ,o.goods_issue_date
      ,o.order_status_id
      ,SUM(CAST(oi.delivery_quantity AS INT)) AS units -- suspicious
FROM   public.specifics_nfl_3pl_work_order o,
JOIN   public.specifics_nfl_3pl_work_order_item oi
                                     ON oi.specifics_nfl_work_order_id = o.id 
CROSS  JOIN public.specifics_nfl_order_status os -- probably wrong
WHERE  o.order_status_id IN (3,17,14)
AND    o.sold_to_id <> '0000000000'              -- suspicious
AND    o.goods_issue_date> = {d '2013-08-01'}    -- nonsense
GROUP  BY 1, 2, 3
  • o.goods_issue_date> = {d '2013-08-01'} 语法无意义。也许你的意思是:
    o.goods_issue_date> = '2013-08-01'

  • 您的specifics_nfl_order_status列表中有FROM个表格,但没有任何表达方式将其与其他表格相关联。这实际上导致 CROSS JOIN ,这导致笛卡尔积,并且几乎肯定以非常昂贵的方式出错:每一行都与其余的每一行结合:
    CROSS JOIN public.specifics_nfl_order_status os
    删除表(因为您不使用它)或添加WHEREON子句以将其连接到其余表。请注意,不仅仅是多余的,它会对结果产生巨大影响。

  • WHERE条款可疑:
    AND o.sold_to_id <> '0000000000'
    好像你将数字存储为字符串或两者混淆。 此外,开始时不应该CAST (oi.delivery_quantity AS INT) 。该列应为数据类型integer或其他适当的数字类型。请务必使用正确data types

  • search_path的默认设置包括public,您可能不需要对表格进行架构限定。可以使用以下内容代替public.specifics_nfl_3pl_work_order,而不是specifics_nfl_3pl_work_order GROUP BY 1, 2, 3

  • GROUP BY o.work_order_number, o.goods_issue_date, o.order_status_id正在使用位置参数,只是一个符号的快捷方式:
    MS Query
    Details in the manual.

  • 根据评论,您使用{{1}}来创建查询。这不是最好的想法。产生你提供给我们的那种劣质代码。您可能希望在使用PostgreSQL时摆脱它。