我有以下postgresql查询:
with A as
(
select '201405MASE04' as TestID, Count(*) TotQ, Count(distinct Case When SE1 = '' then NULL else SE1 end) TotSE,
case when Count(*)=0 then 7 else Count(distinct RC) end TotRC
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Omit <> 1
),
B as
(
select '201405MASE04' as TestID, Count(*) TotQ
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Omit = 1
),
C as
(
select '201405MASE04' as TestID, Count(*) TotQ
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Question_Type_ID='2'
)
Select A.TestID, A.TotQ + coalesce(B.TotQ,0) - coalesce(C.TotQ,0) as TotQ, A.TotSE, A.TotRC
From A
left outer Join B on A.TestID = B.TestID
left outer Join C on A.TestID = C.TestID
当我尝试运行此查询时,它会抛出以下错误:
ERROR: failed to find conversion function from unknown to text
********** Error **********
ERROR: failed to find conversion function from unknown to text
SQL state: XX000
如何在此处找到转换错误的位置?
答案 0 :(得分:4)
看起来postgres不喜欢你的常数'201405MASE04'。
SQL Fiddle Demo 会产生相同的错误。
SQL Fiddle Demo 显示强制转换数据类型修复了问题。
试试这个。我将其定义为文本
with A as
(
select cast('201405MASE04' as text) as TestID, Count(*) TotQ, Count(distinct Case When SE1 = '' then NULL else SE1 end) TotSE,
case when Count(*)=0 then 7 else Count(distinct RC) end TotRC
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Omit <> 1
),
B as
(
select cast('201405MASE04' as text) as TestID, Count(*) TotQ
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Omit = 1
),
C as
(
select cast('201405MASE04' as text) as TestID, Count(*) TotQ
from eivTestItems TI, eivTests T
where TI.Test_ID = T.Test_ID
and T.Test_Type_ID = 1
and T.Test_ID= '201405MASE04'
and TI.Question_Type_ID='2'
)
Select A.TestID, A.TotQ + coalesce(B.TotQ,0) - coalesce(C.TotQ,0) as TotQ, A.TotSE, A.TotRC
From A
left outer Join B on A.TestID = B.TestID
left outer Join C on A.TestID = C.TestID