【发布时间】:2020-02-03 04:58:21
【问题描述】:
我正在从一个数据库中提取一份报告,该数据库在一个字符串列中存储了等效的表单。表单每次都有相同的部分标题。我已将这些列标题转换为“|”然后想用它作为分隔符将各自的响应放在单独的列中。我已经完成了枢轴,但随后整个结果在每一列中重复,而不是正确解析。带有最终列标题的示例列文本以粗体 ** 显示。
我觉得我需要更改我的分区语句并包含一个案例,然后更改“|”到 1、2、3 等。
社区卫生工作者初步外展 XYZ 是 48 岁。女性于 99/99/19 联系。 外展原因:作者因 XYZ 而致电 推荐来源: CCM NCM 初始联系/状态:同意注册 已安排 PCP 进行 7 天随访: 否 您在前往 PCP 办公室的交通方面是否需要帮助: 否 已确定的问题或疑虑:
WITH C AS(
SELECT DISTINCT hnt.note_id, hnt.line, replace(replace(replace(replace(replace(replace(replace(replace
(note_text, 'CHW Initial Contact/Status:', 'Initial Status'),'Community Health Worker Initial Outreach', '|'), 'Reason for Outreach:', '|')
, 'Referral Source:', '|'), 'Initial Contact/Status:', '|'), 'Was 7 day follow-up with PCP scheduled:','|'),'Do you need assistance with transportation to the PCP office:','|'),
'Identified Issues or Concerns:','|') as field , row_number () over (partition by hnt.note_id order by hnt.contact_date desc) rn
From hno_note_text hnt
inner join note_smartphrase_ids nsi on hnt.NOTE_CSN_ID = nsi.NOTE_CSN_ID
CROSS APPLY string_split(replace(replace(replace(replace(replace(replace(replace(replace
(note_text, 'CHW Initial Contact/Status:', 'Initial Status'),'Community Health Worker Initial Outreach', '|'), 'Reason for Outreach:', '|')
, 'Referral Source:', '|'), 'Initial Contact/Status:', '|'), 'Was 7 day follow-up with PCP scheduled:','|'),'Do you need assistance with transportation to the PCP office:','|'),
'Identified Issues or Concerns:','|') , '|') as separated
where smartphrases_id = '151325' and hnt.line = '1'
)
SELECT note_id,
[1] AS Initial_Outreach
,[2] AS Reason
,[3] AS RefSource
,[4] AS Contact_Status
,[5] AS SevenDay
,[6] AS Transportation
,[7] AS Issues
FROM C
PIVOT(
MAX(field)
FOR RN IN([1],[2],[3],[4],[5],[6],[7])
) as PVT
--根据建议的更改,将代码修改为(我不知道为什么修改后的代码的最后一部分没有显示!):
DECLARE @mockupTable TABLE(ID varchar(254), YourString VARCHAR(MAX));
INSERT INTO @mockupTable
select distinct hnt.note_id, note_text
From hno_note_text hnt
inner join note_smartphrase_ids nsi on hnt.NOTE_CSN_ID = nsi.NOTE_CSN_ID
where smartphrases_id = '151325' and hnt.line = '1'
SELECT *
FROM @mockupTable t
CROSS APPLY(SELECT CONCAT('{'
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(t.YourString
,'Community Health Worker Initial Outreach','"Initial_Outreach":"')
,'Reason for Outreach:','","Reason":"')
,'Referral Source:','","RefSource":"')
,'Was 7 day follow-up with PCP scheduled:','","SevenDay":"')
,'Do you need assistance with transportation to the PCP office:','","Transportation":"')
,' Identified Issues or Concerns:','","Issues":"'), 'ACO PRAPARE Screening Questionnaire','","PRAPARE":"'),
'Next Steps:','","Next Step":"')
,'"}')) A(CastedToJSON)
CROSS APPLY OPENJSON(A.CastedToJSON)
WITH(Initial_Outreach VARCHAR(MAX)
,Reason VARCHAR(MAX)
,RefSource VARCHAR(MAX)
,SevenDay VARCHAR(MAX)
,Transportation VARCHAR(MAX)
,Issues VARCHAR(MAX)
,Prapare VARCHAR(MAX)
,NextSteps VARCHAR(MAX)
) B;
【问题讨论】:
-
通常对于这样的 SQL 问题,显示原始数据的小样本(如果敏感,则使用虚假值)以及您希望它的样子会很有帮助。也有助于了解您正在使用的 SQL 的风格。谢谢。
-
谢谢。根据要求更新了描述。使用 SSMS 2016
-
SQL-Server 的哪个版本?
标签: sql-server tsql pivot