格式错误的记录文字,但所谓的文字是来自表格的数据

时间:2013-09-20 19:41:42

标签: sql postgresql

我有这个查询,返回诊所,医生和可用约会的三级hieararchy的平面表示:

select * from search_by_location(20,138.6,-34.9,null,0,0,1,99,current_date,7)

我正在尝试将其用作子查询来生成适当的三级层次结构:

with
a as (select * from search_by_location(20,138.6,-34.9,null,0,0,1,99,current_date,7)),
b as (
  select   business_name,
           address1,
           address2,
           suburb,
           postcode,
           state_acronym,
           phone_no,
           latitude,
           longitude,
           clinic_url_display_name,
           (first_name,
            preferred_name,
            last_name,
            gender,
            doctor_url_display_name,
            gravatar_md5,
            array_agg(appointment_start)
           )::doctor_appointments_row doctor
  from     a
  group by business_name,
           address1,
           address2,
           suburb,
           postcode,
           state_acronym,
           phone_no,
           latitude,
           longitude,
           clinic_url_display_name,
           first_name,
           preferred_name,
           last_name,
           gender,
           doctor_url_display_name,
           gravatar_md5
)
select   (business_name,
          (address1,
           address2,
           suburb,
           postcode,
           state_acronym
          )::address_row,
          phone_no,
          (latitude,
           longitude)::coords_row,
          clinic_url_display_name,
          array_agg(doctor)
         )::clinic_doctor_appointments_row clinic
from     b
group by business_name,
         address1,
         address2,
         suburb,
         postcode,
         state_acronym,
         phone_no,
         latitude,
         longitude,
         clinic_url_display_name

当我执行它时,我收到以下错误消息:

ERROR:  malformed record literal: "0811112222"
DETAIL:  Missing left parenthesis.

这很奇怪,因为我没有在查询中的任何位置指定该文字。它是来自基本查询的数据,特别是phone_no列。知道是什么导致了这个错误以及如何解决它?

更新

这样运行没有错误(只需在with之后替换部分查询):

with
a as (select * from search_by_location(20,138.6,-34.9,null,0,0,1,99,current_date,7)),
b as (
  select   business_name,
           address1,
           address2,
           suburb,
           postcode,
           state_acronym,
           phone_no,
           latitude,
           longitude,
           clinic_url_display_name,
           (first_name,
            preferred_name,
            last_name,
            gender,
            doctor_url_display_name,
            gravatar_md5,
            array_agg(appointment_start)
           )::doctor_appointments_row doctor
  from     a
  group by business_name,
           address1,
           address2,
           suburb,
           postcode,
           state_acronym,
           phone_no,
           latitude,
           longitude,
           clinic_url_display_name,
           first_name,
           preferred_name,
           last_name,
           gender,
           doctor_url_display_name,
           gravatar_md5
)
select * from b

在这种情况下,array_agg没有嵌套,我认为这可能是错误的。

1 个答案:

答案 0 :(得分:0)

我打赌你在运行你的函数时会得到同样的错误,并且你的真正问题在于返回类型。这是我唯一可以发现可能的地方,除非你提出的问题多于你所说的。

我的想法是你应该尝试运行

 select * from search_by_location(20,138.6,-34.9,null,0,0,1,99,current_date,7)

第一。确保有效。如果确实如此,那么......

仔细查看运行其他查询的上下文。我的猜测是它正在一个试图投射到记录或其他东西的函数内运行,