PostgreSQL交叉表查询,数据类型不匹配

时间:2016-02-18 09:31:57

标签: postgresql crosstab

我正在尝试运行此查询:

SELECT *
FROM CROSSTAB('SELECT a.marketing_product_group::VARCHAR
                    ,(a.sales_year || '' - '' || a.sales_quater)::VARCHAR AS attribute
                    ,sum(a.sales_value)::NUMERIC AS sales_value
               FROM data_Warehouse.sales_summary a
               WHERE a.customer_code = '' SCAND ''
                     AND a.invoice_date >= '' 2015 - 04 - 01 ''
               GROUP BY a.marketing_product_group
                       ,a.sales_year || '' - '' || a.sales_quater
               ORDER BY a.sales_year || '' - '' || a.sales_quater') 
AS CT(marketing_product_group VARCHAR
       ,period1 VARCHAR
       ,period2 VARCHAR
       ,period3 VARCHAR
       ,period4 VARCHAR)

我在这个网站上看过很多项目并试图纠正它,但我仍然遇到了可怕的错误:

  

return和sql元组描述不兼容

子查询返回数据:

marketing_product_group ;attribute; sales_value
"K3"            ; "2016-1"  ;600.0900000000000000
"CASSETTES"     ; "2016-1"  ;291.0000000000000000
"ENDO FILES"    ; "2016-1"  ;254.2500000000000000
"NEOZYME"       ;"2016-1"   ;15.5300000000000000
"PANAVIA F"     ;"2016-1"   ;47.8600000000000000
"BOUTIQUE NIGHT";"2016-2"   ;1012.5000000000000000
"ENDO MISC"     ;"2016-2"   ;20.1000000000000000
"GLOVES 17"     ;"2016-2"   ;617.2000000000000000
"DIATECH"       ;"2016-2"   ;8.1800000000000000
""              ;"2016-2"   ;47.5000000000000000
"CARESHOP 1"    ;"2016-2"   ;14.4800000000000000
"CLASSIC 1"     ;"2016-3"   ;49.5000000000000000
"GLOVES 15"     ;"2016-3"   ;70.0000000000000000
"SOFLEX"        ;"2016-4"   ;103.2000000000000000
"OMNIA"         ;"2016-4"   ;64.0000000000000000

我飞过它可能是一件非常简单的事情吗?

1 个答案:

答案 0 :(得分:0)

由于您的已投放numeric

sum(a.sales_value)::NUMERIC AS sales_value

您的输出列也必须为numeric

...
AS CT(marketing_product_group VARCHAR
       ,period1 NUMERIC 
       ,period2 NUMERIC 
       ,period3 NUMERIC 
       ,period4 NUMERIC )

这里有一些其他的奇怪之处,但由于你没有提供适当的信息(Postgres版本,表格定义),我会留下它。

基础: