2014-09-10 21 views
2

我有以下PostgreSQL的查詢: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 

如何找出我在哪裏得到轉換錯誤?

回答

3

它看起來像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 
+0

真棒謝謝你..工作很棒 – Abhishek 2014-09-10 17:11:28