当我在查询下面运行时,我收到此错误
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
答案 0 :(得分:2)
我的错误是,在合并中列名无关紧要,但是顺序却很重要(也许我错了,我找不到文档)>
示例:
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
答案 1 :(得分:0)
最有可能 - 虽然因为您尚未发布表定义而无法确定 - 但字段claimid
的类型为text
(或varchar
,但这就是全部相同)而count(claimid)
产生bigint
。在这种情况下,快速解决方法是count(claimid)::text
。
否则,你想要实现的目标还不清楚。在顶部选择中,您显然希望对每位患者的费用和付费金额进行汇总。并且底部选择应该是所有患者的费用和支付金额的总和?您不应该尝试在单个查询中组合这些不同的东西。最好有两个不同的查询,它们具有明显的功能,并且不依赖于step
等限定符的知识。