简体   繁体   English

SQL Server存储过程-将多行中的列合并为一

[英]SQL Server stored procedure - pull columns from multiple rows into one

I'm working on a SQL stored procedure to pull patient billing data to a dataset (which will then be returned to a VB.net application). 我正在研究一个SQL存储过程,以将患者账单数据提取到数据集(然后将其返回到VB.net应用程序)。

My problem: one patient may have multiple rows in the table for one billing period/item, but only 3 of the fields in the rows will be different. 我的问题是:一位患者可能在一个计费期/项目中表中有多行,但是这些行中只有3个字段会有所不同。 I don't want to pull each row in its entirety every time. 我不想每次都拉整行。 I want all fields the first time around, then just the different fields the next time around. 我希望所有字段都在第一次,然后在不同字段下一次。 I would like all records in one row. 我希望所有记录都在一行中。

I have no way of knowing ahead of time how many rows this patient will have in the table for this one billing period/item, but the maximum is 10. 我无法提前知道该患者在此一个计费期/项目表中将有几行,但最大值为10。

There is a claim_id field in the table that is patient specific to each billing period/item, so it can be repeated (and is what I'm using to pull the records). 表格中有一个Claim_id字段,该字段特定于每个计费周期/项目的患者,因此可以重复(这是我用来提取记录的内容)。 There is also an ID field in the table that is an Identity column, so it is unique for every record. 表中还有一个ID字段,它是Identity列,因此对于每个记录来说都是唯一的。

My current stored procedure: 我当前的存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [BillClm] 
@ClaimId varchar(20),
@AccountCode integer

AS

SET NOCOUNT ON;

declare @PatientGridTable table
(
    Claim_Submitters_Id varchar(20),
    Claim_Status_Code varchar(2),
    Total_Amount_of_Claim decimal(18,2),
    Claim_Payment_Amt decimal(18,2),
    Claim_Patient_Resp decimal(18,2),
    Claim_Filing_Ind_Code varchar(2),
    Payor_Claim_Control_Num varchar(30),
    Bill_Type integer,
    Claim_Adjustment_Group_Reason_Code varchar(10),
    Claim_Adjustment_Amount decimal(18,2),
    Claim_Adjustment_Quantity integer,  
    Patient_Name varchar(60),
    Patient_ID_Num varchar(50),
    Insured_Name varchar(60),
    Insured_ID_Num varchar(50),
    Other_Payor_Name varchar(60),
    Other_Payor_ID varchar(80),
    Other_Subscriber_Name varchar(60),
    Other_Subscriber_ID varchar(80),
    Covered_Quantity integer,
    Covered_Amount decimal(18,2),
    Reimbursement_Rate decimal(6,2),
    Remark_Code varchar(2500),  
    Statement_Period_Start_End varchar(25)
)

insert into @PatientGridTable
(
    Claim_Submitters_Id,
    Claim_Status_Code,
    Total_Amount_of_Claim,
    Claim_Payment_Amt,
    Claim_Patient_Resp,
    Claim_Filing_Ind_Code,
    Payor_Claim_Control_Num,
    Bill_Type,
    Claim_Adjustment_Group_Reason_Code,
    Claim_Adjustment_Amount,
    Claim_Adjustment_Quantity,  
    Patient_Name,
    Patient_ID_Num,
    Insured_Name,
    Insured_ID_Num,
    Other_Payor_Name,
    Other_Payor_ID,
    Other_Subscriber_Name,
    Other_Subscriber_ID,
    Covered_Quantity,
    Covered_Amount,
    Reimbursement_Rate,
    Remark_Code,    
    Statement_Period_Start_End
)
select  
    d.submitter_id, d.claim_status, d.claim_amt, d.payment_amt,
    d.patient_resp, d.claim_ind, d.ref_num, d.bill_type,
    (a.adj_group_code+' '+a.adj_reason_code), a.adj_amount,
    a.adj_qty, d.patient_name, d.patient_id, d.insured_name,
    d.insured_id, d.other_payor, d.other_payor_id, d.other_subscriber,
    d.other_subscriber_id, d.days, d.amount, d.percent,
    (d.adj_ref+'_'+c.code_text),
    (CONVERT(varchar(12), d.claim_start_date, 101)+' '+CONVERT(varchar(12),         d.claim_end_date,101))
    FROM clm_detail_patient d  WITH(NOLOCK)
    INNER JOIN code_list c  WITH(NOLOCK) 
    ON d.adj_ref = c.map_id
    INNER JOIN clm_adj a  WITH(NOLOCK)
    ON d.claim_id = a.claim_id    
    WHERE d.claim_id = @ClaimId     

-- -----------------------------------------------------------------------
select * from @PatientGridTable

clm_adj is the table that may return 0 to 10 rows - tied to the clm_detail_patient table via the claim_id field. clm_adj是可返回0至10行的表-通过Claim_id字段绑定到clm_detail_ Patient表。 (clm_detail_patient will always have only one row.) (clm_detail_ Patient将始终只有一行。)

The three fields in the clm_adj table that would be different in each row are: Claim_Adjustment_Group_Reason_Code, Claim_Adjustment_Amount, Claim_Adjustment_Quantity clm_adj表中的三行各行各不相同,分别是:Claim_Adjustment_Group_Reason_Code,Claim_Adjustment_Amount,Claim_Adjustment_Quantity

What I'd like is for those three fields to appear next to each other in one row for each different row in clm_adj. 我想要的是让这三个字段在clm_adj中的每个不同行中彼此相邻出现。 I'm not sure how to accomplish that. 我不确定如何做到这一点。

I'm not tied to doing a temp table, if there is a better way to go here. 如果有更好的方法,我不打算做一个临时表。 Any help would be much appreciated! 任何帮助将非常感激!

Sample clm_detail_patient table data: clm_detail_ Patient表数据样本:

id     code      amt        payment    pat_resp   clm_ind    ref_num          bill     pat_name          pat_id      days    amount      pct   adj_ref  start_dt      end_date       claim_id
12345    19    15344.43    14962.12    2456.50    MA        20122580231ABC    213    DOE, JANE           123456789B    0    17418.62    0.00    MA01    2012-10-01    2012-10-31    1234-A00000000000001
12346    19    9157.21     8128.58     289.00     MA        20122580231ABC    212    SMITH, JOHN         987654321B    0    8417.58     0.00    MA01    2012-10-10    2012-10-31    4567-A00000000000001
12347    1     2522.99     1143.66     0.00       MA        20122580231ABC    211    JONES, MARY         987123456B    0    1143.66     0.00    MA01    2012-10-14    2012-10-17    9876-A00000000000001

Sample clm_adj table data: 示例clm_adj表数据:

adj  reason   amt      qty   claim_id
CO    45    -2074.19    0    1234-A00000000000001
PR    2     2456.50     0    1234-A00000000000001
CO    45    739.63      0    4567-A00000000000001
PR    2     289.00      0    4567-A00000000000001
CO    45    1379.33     0    9876-A00000000000001

Last field in each table is the claim_id. 每个表中的最后一个字段是Claim_id。

So the first two patients have two records in the clm_adj table, the third patient has just one. 因此,前两个患者在clm_adj表中有两个记录,第三个患者只有一个。

If I were to run my current stored procedure for patient # 1, I'd get two rows back. 如果我要为1号患者运行当前的存储过程,我将返回两行。 What I'd like to see for the first patient: 我想为第一位患者看到的是:

12345    19    15344.43    14962.12    2456.50    MA        20122580231ABC    213    CO 45    -2074.19    0    PR 2    2456.50    0    DOE, JANE           123456789B    0    17418.62    0.00    MA01    2012-10-01  2012-10-31    1234-A00000000000001

So the two adj/reason, amount, and quantity field records appear on one row, next to each other (starting with the CO 45 in the ninth field here). 因此,两个调整/原因,金额和数量字段记录显示在一行上,彼此相邻(从第九个字段中的CO 45开始)。

Following the links provided by Esoteric Screen Name I was able to get the problem solved. 通过Esoteric屏幕名称提供的链接,我能够解决问题。 I inserted the following into my stored procedure prior to declaring the patient grid table: 在声明患者表格之前,我将以下内容插入存储过程:

DECLARE @combinedClaimString VARCHAR(MAX)
SELECT @combinedClaimString = COALESCE(@combinedClaimString + '; ', '') + adj_group_code + ', ' + adj_reason_code + ', ' + (SELECT CONVERT(varchar(22), adj_amount)) + ', ' + (SELECT CONVERT(varchar(22), adj_qty))
FROM (SELECT  adj_group_code, adj_reason_code, adj_amount, adj_qty, claim_id 
FROM br549.clm_adj
WHERE claim_id = @ClaimId)
AS temp

This results in one string with each part of the claim separated by commas, with each individual claim separated by a semi-colon. 这将导致一个字符串,要求的每个部分都用逗号分隔,每个单独的要求都用分号分隔。

I then modified the PatientGridTable structure to include just one field for all claim information: 然后,我修改了PatientGridTable结构,使其仅包含一个用于所有理赔信息的字段:

declare @PatientGridTable table
(
    Claim_Submitters_Id varchar(20),
    Claim_Status_Code varchar(2),
    Total_Amount_of_Claim decimal(18,2),
    Claim_Payment_Amt decimal(18,2),
    Claim_Patient_Resp decimal(18,2),
    Claim_Filing_Ind_Code varchar(2),
    Payor_Claim_Control_Num varchar(30),
    Bill_Type integer,
    Claim_Adjustment varchar(MAX),  
    Patient_Name varchar(60),
    Patient_ID_Num varchar(50),
    Insured_Name varchar(60),
    Insured_ID_Num varchar(50),
    Other_Payor_Name varchar(60),
    Other_Payor_ID varchar(80),
    Other_Subscriber_Name varchar(60),
    Other_Subscriber_ID varchar(80),
    Covered_Quantity integer,
    Covered_Amount decimal(18,2),
    Reimbursement_Rate decimal(6,2),
    Remark_Code varchar(2500),  
    Statement_Period_Start_End varchar(25)
)

I then selected the @combinedClaimString variable into the PatientGridTable: 然后,我将@combinedClaimString变量选择到PatientGridTable中:

insert into @PatientGridTable
(
    Claim_Submitters_Id,
    Claim_Status_Code,
    Total_Amount_of_Claim,
    Claim_Payment_Amt,
    Claim_Patient_Resp,
    Claim_Filing_Ind_Code,
    Payor_Claim_Control_Num,
    Bill_Type,
    Claim_Adjustment,  
    Patient_Name,
    Patient_ID_Num,
    Insured_Name,
    Insured_ID_Num,
    Other_Payor_Name,
    Other_Payor_ID,
    Other_Subscriber_Name,
    Other_Subscriber_ID,
    Covered_Quantity,
    Covered_Amount,
    Reimbursement_Rate,
    Remark_Code,    
    Statement_Period_Start_End
)
select  
    d.submitter_id, d.claim_status, d.claim_amt, d.payment_amt,
    d.patient_resp, d.claim_ind, d.ref_num, d.bill_type,
    (SELECT @combinedClaimString as adj_group_code), d.patient_name, d.patient_id, d.insured_name, d.insured_id, d.other_payor, d.other_payor_id, d.other_subscriber,
    d.other_subscriber_id, d.days, d.amount, d.percent,
    (d.adj_ref+'_'+c.code_text),
    (CONVERT(varchar(12), d.claim_start_date, 101)+' '+CONVERT(varchar(12),         d.claim_end_date,101))
    FROM clm_detail_patient d  WITH(NOLOCK)
    INNER JOIN code_list c  WITH(NOLOCK) 
    ON d.adj_ref = c.map_id
    INNER JOIN clm_adj a  WITH(NOLOCK)
    ON d.claim_id = a.claim_id    
    WHERE d.claim_id = @ClaimId

The dataset is returned to my application with just one row per patient. 数据集返回给我的应用程序,每个患者只有一行。 I can then split the claim string to format appropriately. 然后,我可以将声明字符串拆分为适当的格式。

Many thanks to Esoteric Screen Name! 非常感谢神秘的屏幕名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL Server中将行转换为列的存储过程 - Stored Procedure for converting rows to columns in SQL Server SQL Server存储过程,从多个表中选择行 - SQL Server Stored Procedure, selecting rows from multiple tables 如何使用存储过程从 SQL 服务器数据库中提取数据 - How to pull data from SQL Server database using a stored procedure 如何在SQL Server 2012中使用存储过程将多行合并为一行? - How to merge multiple rows into one row using stored procedure in sql server 2012? 在存储过程中使用SQL Server中的JOIN计算来自多个表的多个列 - Counting multiple columns from multiple tables with a JOIN in SQL Server within a stored procedure SQL Server 2005将存储过程CSV值分成多个列? - SQL Server 2005 seperate stored procedure CSV value into multiple columns? SQL Server 2005:存储过程将行从一个表移动到另一个表 - SQL Server 2005: stored procedure to move rows from one table to another 如何在 Jupyter Notebook 中使用 Pyodbc 从 SQL 服务器存储过程中检索多行 - How to retrieve multiple rows from a SQL Server stored procedure using Pyodbc in Jupyter Notebook 将多个查询的结果合并为SQL Server存储过程中的一个结果 - Combine results of multiple queries as one result in a SQL Server stored procedure SQL Server 2008:一个存储过程中的多个查询 - SQL Server 2008 : Multiple Queries in One Stored Procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM