我在PostgreSQL中有一个SQL表(3列:数字,数字,字符串) -
100226, 118, "{{5662,"IBM Certified for On Demand Business - Solution Advisor - 816"},{5663,"MS Windows XP Desktop Administration - Assessment - E1"},{5664,"MS Windows 95 Navigation - Assessment - E3"},{5671,"TestPrep Final Exam: Six Sigma Foundations"}"
现在我的要求是获取表格中的行,如下所示
100226 118 {5662,"IBM Certified for On Demand Business - Solution Advisor - 816"}
{5663,"MS Windows XP Desktop Administration - Assessment - E1"}
{5664,"MS Windows 95 Navigation - Assessment - E3"}
{5671,"TestPrep Final Exam: Six Sigma Foundations"}
以下是我正在检索值
的查询select
chs.lms_im_employee_id emp_num,
chs.lms_mr_cp_id complaince_no,
entity_name
from
lms.lms_im_cmplnc_objtemp obj,
lms.lms_im_cmplnc_history chs
where
obj.lms_mr_cp_id=chs.lms_mr_cp_id
and obj.lms_im_employee_id = chs.lms_im_employee_id
and chs.lms_im_ch_is_compliant = false
and chs.lms_im_employee_id = '66'
group by chs.lms_mr_cp_id, chs.lms_im_employee_id;
答案 0 :(得分:0)
首先,如果可能的话,存储你的字符串将是有益的,但如果没有, 尝试以下
现在,您的数据看起来有点格式不正确,因为它缺少最终}。在这个答案中,我认为这是一个错误,如果不是,你将不得不清理你的数据,或者重写我的查询以在原始数据的末尾附加'}'。
select
chs.lms_im_employee_id emp_num,
chs.lms_mr_cp_id complaince_no,
json_array_elements(array_to_json(entity_name::text[])) entity_name
from
lms.lms_im_cmplnc_objtemp obj,
lms.lms_im_cmplnc_history chs
where
obj.lms_mr_cp_id=chs.lms_mr_cp_id
and obj.lms_im_employee_id = chs.lms_im_employee_id
and chs.lms_im_ch_is_compliant = false
and chs.lms_im_employee_id = '66'
group by chs.lms_mr_cp_id, chs.lms_im_employee_id;
或者,如果您的数据实际上缺少结束}
select
chs.lms_im_employee_id emp_num,
chs.lms_mr_cp_id complaince_no,
json_array_elements(array_to_json((entity_name||'}')::text[])) entity_name
from
lms.lms_im_cmplnc_objtemp obj,
lms.lms_im_cmplnc_history chs
where
obj.lms_mr_cp_id=chs.lms_mr_cp_id
and obj.lms_im_employee_id = chs.lms_im_employee_id
and chs.lms_im_ch_is_compliant = false
and chs.lms_im_employee_id = '66'
group by chs.lms_mr_cp_id, chs.lms_im_employee_id;
当然所有这些都假定您的数据格式完美。如果不是,那么您将不得不清理数据,或编写自定义函数来解释它。