当我在查询下运行时,出现此错误
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
Run Code Online (Sandbox Code Playgroud)
我的错误是,列的并集名称无关紧要,但是顺序却很重要(也许我错了,我找不到文档)
例:
1)很好
select
1 :: integer as someint,
'1' :: text as sometext
union
select
2 :: integer as someint,
'2' :: text as sometext
Run Code Online (Sandbox Code Playgroud)
退货
someint sometext
1 1 1
2 2 2
Run Code Online (Sandbox Code Playgroud)
2)这不好
select
1 :: integer as someint,
'1' :: text as sometext
union
select
'2' :: text as sometext,
2 :: integer as someint
Run Code Online (Sandbox Code Playgroud)
抛出
Error(s), warning(s):
42804: UNION types integer and text cannot be matched
Run Code Online (Sandbox Code Playgroud)
尝试一下https://rextester.com/l/postgresql_online_compiler
最有可能的是 - 尽管无法确定,因为您还没有发布表定义 - 该字段claimid的类型为text(或varchar,但这都是相同的),count(claimid)同时生成bigint. 在这种情况下,快速解决办法就是执行count(claimid)::text.
否则就不清楚你想要实现什么。在顶部选择中,您显然想要对每个患者的费用和支付金额进行求和。底部的选择应该将所有患者的费用和支付金额相加?您不应该尝试在单个查询中组合这些不同的内容。最好有两个不同的查询,它们具有明显的功能,并且不依赖于诸如 之类的限定符的知识step。