UNION类型不匹配[英] UNION type mismatch

本文是小编为大家收集整理的关于UNION类型不匹配的处理方法,想解了UNION类型不匹配的问题怎么解决?UNION类型不匹配问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

当我在查询下运行时,我收到了这个错误

<块引用>

UNION 类型 text 和 bigint 无法匹配

SELECT 
    1 AS STEP
  , '' AS ProviderName
  , '' AS Procedurecode
  , Claimid
  , Patient_First_Name
  , Patient_Last_Name
  , DOS
  , SUM(COALESCE(Total_Charge,0))
  , SUM(COALESCE(PaidAmount,0))
  , PostedDate
  , CheckEFTDate
  , CheckEFTNo 
FROM table_name
GROUP BY ProviderName, Claimid, Patient_First_Name, Patient_Last_Name, DOS, PostedDate,
         CheckEFTDate, CheckEFTNo
UNION ALL
SELECT 
    2 AS STEP
  , '' AS ProviderName
  , '' AS Procedurecode
  , COUNT(Claimid)
  , '' AS Patient_First_Name
  , '' AS Patient_Last_Name
  , NULL::date AS DOS
  , SUM(COALESCE(Total_Charge,0))
  , SUM(COALESCE(PaidAmount,0))
  , NULL::date AS PostedDate
  , NULL::date AS CheckEFTDate
  , '' AS CheckEFTNo 
FROM table_name
GROUP BY Claimid

推荐答案

我的错误是在联合中列名无关紧要,但顺序很重要(也许我错了,我找不到文档)

例子:

1) 这很好

select
1 :: integer as someint, 
'1' :: text as sometext

union

select
2 :: integer as someint,
'2' :: text as sometext

返回

someint sometext
1   1   1
2   2   2

2) 这不好

select
1 :: integer as someint, 
'1' :: text as sometext

union

select
'2' :: text as sometext,
2 :: integer as someint

抛出

Error(s), warning(s):

42804: UNION types integer and text cannot be matched

自己试试https://rextester.com/l/postgresql_online_compiler

本文地址:https://www.itbaoku.cn/post/1763746.html