首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PostgreSQL - DataError: numeric类型的输入语法无效:"Product“

PostgreSQL - DataError: numeric类型的输入语法无效:"Product“
EN

Stack Overflow用户
提问于 2012-12-13 20:20:42
回答 1查看 10.1K关注 0票数 5

所以我不明白为什么它会产生这个错误。字段值"Product“是否是数字?.我使用UNION ALL进行查询,它使第一行显示其他行的总和。我的问题很长,所以我不会发布它(但如果它是必要的,我会发布它)。

因此,首先我将给出我的代码片段:

代码语言:javascript
复制
with w as (
SELECT
...
CASE x.variants WHEN x.variants THEN x.product || ' - ' || x.variants else x.product END as product
...

) 

SELECT 
...
'Product' as product,
...
UNION ALL
SELECT * FROM w

因此,我使用product列在第一行显示为“Product”(因为我不需要在第一行的这个字段中进行计算。但计算其他字段的总和)

当我运行我的查询时,它输出这个错误:

代码语言:javascript
复制
DataError: invalid input syntax for type numeric: "Product"
LINE 104: 'Product' as product,

编辑: PostgreSQL版本8.4

完整查询:

代码语言:javascript
复制
(
with w as (SELECT
min(fld2.id) AS id, -- note. Posted code is working now, but this select part was not
fld2.partner as partner, -- the same that is at the bottom (where different first line
fld2.product as product, -- is queried. So that gave error.
fld2.qty1 as qty1,
fld2.qty2 as qty2,
fld2.q1 as q1,
fld2.q2 as q2,
CASE WHEN (q1 >= q2 and q2 != 0) THEN ((q1-q2)/q2) * 100 WHEN (q1=0 or q2=0) THEN NULL WHEN (q2 > q1 and q1!=0) THEN ((q1/q2)-1)*100 END as percent_q,
fld2.price_q as price_q,
fld2.y1  as y1,
fld2.y2 as y2,
CASE WHEN (y1 >= y2 and y2 != 0) THEN ((y1-y2)/y2) * 100 WHEN (y1=0 or y2=0) THEN NULL WHEN (y2 > y1 and y1!=0) THEN ((y1/y2)-1)*100 END as percent_y,
fld2. price_y as price_y

FROM
(
SELECT 
min(x.id) AS id,
x.partner as partner,
--quarter as quarter,
CASE x.variants WHEN x.variants THEN x.product || ' - ' || x.variants else x.product END as product,
sum(case when (calcyear=DATE_PART('year',  now()) and x.quarter=3) then x.qty else 0 end) as qty1,
sum(case when (calcyear=DATE_PART('year',  now() - '1 year'::interval) and x.quarter=3) then x.qty else 0 end) as qty2,
sum(case when (calcyear=DATE_PART('year',  now()) and x.quarter=3) then price_unit else 0 end) as q1,
sum(case when (calcyear=DATE_PART('year',  now() - '1 year'::interval) and x.quarter=3) then price_unit else 0 end) as q2,
sum(case calcyear when  DATE_PART('year',  now()) then price_unit else 0 end) as y1,
sum(case calcyear when DATE_PART('year',  now() - '1 year'::interval) then price_unit else 0 end) as y2,
sum(case when (calcyear=DATE_PART('year',  now()) and x.quarter=3) then price_unit WHEN (calcyear=DATE_PART('year',  now() - '1 year'::interval)  and x.quarter=3) THEN -1*price_unit end) AS price_q,
sum(case when calcyear=DATE_PART('year',  now()) then price_unit ELSE -1*price_unit end) AS price_y
FROM (
  SELECT
    min(so.id) as id,
    DATE_PART('year',  so.date_order) AS calcyear,
    DATE_PART('quarter',  so.date_order) AS quarter,
   sol.price_unit as price_unit,
   rp.name as partner,
   pt.name as product,
   pp.variants as variants,
   sol.product_uom_qty as qty
FROM 
sale_order AS so 
INNER JOIN sale_order_line AS sol ON (sol.order_id = so.id)
INNER JOIN res_partner as rp ON (so.partner_id = rp.id)
INNER JOIN product_product as pp ON (sol.product_id=pp.id)
INNER JOIN product_template as pt ON (pp.product_tmpl_id=pt.id)
WHERE EXISTS(
SELECT * FROM  res_partner_category_rel rpcl
WHERE 
rpcl.partner_id=rp.id and rpcl.category_id=37
and (so.date_order >= date_trunc('year', now() - '1 year'::interval)::timestamp::date  and so.date_order <= date_trunc('year', now()+ '1 year'::interval)::timestamp::date-1 )
and rp.distributor=True and rp.active=True
and so.state != 'cancel'
)
GROUP BY
variants,
calcyear,
quarter,
price_unit,
partner,
product,
qty
) AS x
GROUP BY
 partner,
x.variants,
x.product
ORDER BY
  partner
)fld2
GROUP BY
partner,
q1,
q2,
y1,
y2,
qty1,
qty2,
product,
price_q,
price_y,
percent_q,
percent_y
ORDER BY 
partner
)
SELECT
1111 as id,
'Viso' as partner,
'Product' as product,
sum(uf.qty1) as qty1,
sum(uf.qty2) as qty2,
sum(uf.q1) as q1,
sum(uf.q2) as q2,
sum(CASE WHEN (uf.q1 >= uf.q2 and uf.q2 != 0) THEN ((uf.q1-uf.q2)/uf.q2) * 100 WHEN (uf.q1=0 or uf.q2=0) THEN NULL WHEN (uf.q2 > uf.q1 and uf.q1!=0) THEN ((uf.q1/uf.q2)-1)*100 END) as percent_q,
sum(uf.price_q) as price_q,
sum(uf.y1)  as y1,
sum(uf.y2) as y2,
sum(CASE WHEN (uf.y1 >= uf.y2 and uf.y2 != 0) THEN ((uf.y1-uf.y2)/uf.y2) * 100 WHEN (uf.y1=0 or uf.y2=0) THEN NULL WHEN (uf.y2 > uf.y1 and uf.y1!=0) THEN ((uf.y1/uf.y2)-1)*100 END) as percent_y,
sum(uf.price_y) as price_y
FROM 
(
SELECT 
sum(price_q) as price_q,
sum(price_y) as price_y,
sum(qty1) as qty1,
sum(qty2) as qty2,
sum(q1) as q1,
sum(q2) as q2,
sum(y1)  as y1,
sum(y2) as y2
FROM w
)uf
UNION ALL
SELECT * FROM w
) fld
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-13 20:37:36

好了,我发现了问题所在。愚蠢的我:)。它给出这个错误是因为查询的主要部分select columns的顺序是错误的(尽管在我的系统中它是使用图形界面进行排序的,但在这种情况下,当使用UNION ALL时,列也需要正确排序)。因此,我更改了顺序,使其与第一行中的顺序相同,并且起作用了。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13859748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档