1

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'}))

如果您能提供帮助,那就太好了。

4

3 回答 3

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

如果它仍然不起作用 - 尝试评论 sum 并查看它是否有效?

于 2013-08-11T09:02:39.290 回答
0

但是你有一个table2还是只有table1?

尝试:
SELECT table1.fielda,
SUM (CAST (table1.fielda AS INT)) AS "header.specific"
FROM

于 2013-08-10T22:35:29.140 回答
0

除了@Roman 已经清理的内容之外,这里还有更多问题:

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,但没有任何表达式将其连接到其余部分。这有效地导致了 a CROSS JOIN,这导致了笛卡尔积,并且几乎可以肯定以非常昂贵的方式是错误的:每一行都与其余的每一行相结合:
    CROSS JOIN public.specifics_nfl_order_status os
    要么删除表(因为你不使用它)要么添加一个WHEREorON子句将其连接到其余部分。请注意,它不仅是多余的,而且对结果产生了巨大的影响。

  • 这个WHERE子句很可疑:
    AND o.sold_to_id <> '0000000000'
    似乎您将数字存储为字符串或以其他方式混淆两者。此外,CAST (oi.delivery_quantity AS INT) 不应该需要开始。该列应以数据类型integer或其他适当的数字类型开头。一定要使用正确的数据类型

  • 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用于创建查询的评论。这不是最好的想法。生成您提供给我们的那种劣质代码。在使用 PostgreSQL 时,您可能希望摆脱它。

于 2013-08-11T12:15:54.743 回答