简体   繁体   中英

Combining the multiple records(rows) into single record(row)

I am trying to combine the multiple rows into a single row , i was able to get the half of it i tried many ways get the remaining half , below is the sql i wrote. any help will be appreciated.

DROP TABLE #TEST_DEPT_NAME 

CREATE TABLE #TEST_DEPT_NAME ([ID] [varchar](255) NULL,[CSN_ID] [varchar](50) NULL,[NOTE_ID] [varchar](50) NULL,[DEPARTMENT_NAME] [varchar](255) NULL,
[NOTE_CSN_ID] [varchar](50) NULL,[LINE] [varchar](50) NULL,[NOTE_TEXT] [nvarchar](max) NULL,[AUTHOR_USER_ID] [varchar](50) NULL,[AUTHOR_USER_NAME] [varchar](255) NULL,
[NOTE_TYPE_NAME] [varchar](255) NULL,[IS_ARCHIVED_YN] [varchar](255) NULL,[NOTE_STATUS_NAME] [varchar](255) NULL)

INSERT INTO #TEST_DEPT_NAME
VALUES ('123456','1234567','12345678' ,'TEST','001234' ,1 ,'NOTES_1.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','001234' ,1 ,'NOTES_1.1.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','0012345' ,2 ,'NOTES_1.2' ,'999999','TEST 1','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','00123456' ,3 ,'NOTES_1.3' ,'999999','TEST 1','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,1 ,'NOTES_2.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,2 ,'NOTES_2.2' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,3 ,'NOTES_2.3' ,'1234','TEST','Clinic Note','N','Signed')


SELECT distinct  ID,[CSN_ID],[NOTE_ID],[DEPARTMENT_NAME],[NOTE_TYPE_NAME],[IS_ARCHIVED_YN]
,[NOTE_TEXT] =  (select ' '+ case when [NOTE_TEXT]= ''  then null else [NOTE_TEXT]end   from  #TEST_DEPT_NAME P1 
WHERE P1.[ID] = P2.[ID]AND P1.[CSN_ID] =P2.[CSN_ID] AND P1.NOTE_ID = P2.NOTE_ID AND P1.DEPARTMENT_NAME = P2.DEPARTMENT_NAME
AND P1.LINE = P2.LINE AND P1.NOTE_TYPE_NAME =P2.NOTE_TYPE_NAME AND P1.NOTE_CSN_ID = P2.NOTE_CSN_ID AND P1.IS_ARCHIVED_YN =P2.IS_ARCHIVED_YN  FOR XML PATH(''))
FROM #TEST_DEPT_NAME  P2 WHERE [ID] = '123456' 
GROUP BY ID,CSN_ID,[NOTE_ID],[DEPARTMENT_NAME],[NOTE_TYPE_NAME],[IS_ARCHIVED_YN],LINE,NOTE_CSN_ID

when i run above SQL i am getting result set as below

ID       CSN_ID  NOTE_ID    DEPARTMENT_NAME    NOTE_TEXT
123456  1234567 12345678    TEST               NOTES_1.1 NOTES_1.1.1
123456  1234567 12345678    TEST               NOTES_1.2
123456  1234567 12345678    TEST               NOTES_1.3
123456  1234567 12345678    TEST               NOTES_2.1
123456  1234567 12345678    TEST               NOTES_2.2
123456  1234567 12345678    TEST               NOTES_2.3

But I want the result as shown below

ID      CSN_ID  NOTE_ID     DEPARTMENT_NAME    NOTE_TEXT
123456  1234567 12345678    TEST               NOTES_1.1 NOTES_1.1.1 NOTES_1.2 NOTES_1.3
123456  1234567 12345678    TEST               NOTES_2.1 NOTES_2.2  NOTES_2.3

My assumption is that this is foley data and the values of Note_Text will actually be long-form notes inputted by users. The values in the note_csn_id field and inclusion of Line in the group by are causing the query to separate out the separate "Note_1" records.

Your query produces the following results in #test_dept_name:

ID      CSN_ID      NOTE_ID     DEPARTMENT_NAME     NOTE_CSN_ID     LINE    NOTE_TEXT       AUTHOR_USER_ID  AUTHOR_USER_NAME    NOTE_TYPE_NAME  IS_ARCHIVED_YN  NOTE_STATUS_NAME
123456  1234567     12345678    TEST                1234            1       NOTES_1.1       1234            TEST                Clinic Note     N               Signed
123456  1234567     12345678    TEST                1234            1       NOTES_1.1.1     1234            TEST                Clinic Note     N               Signed
123456  1234567     12345678    TEST                12345           2       NOTES_1.2       999999          TEST 1              Clinic Note     N               Signed
123456  1234567     12345678    TEST                123456          3       NOTES_1.3       999999          TEST 1              Clinic Note     N               Signed
123456  1234567     12345678    TEST                66666           1       NOTES_2.1       1234            TEST                Clinic Note     N               Signed
123456  1234567     12345678    TEST                66666           2       NOTES_2.2       1234            TEST                Clinic Note     N               Signed
123456  1234567     12345678    TEST                66666           3       NOTES_2.3       1234            TEST                Clinic Note     N               Signed

Notice that note_csn_id changes when a note line changes. This might be redundant behavior as you have a separate line number. We can make the query come out as you desire by:

  • changing the group by clause to only consider the first X characters of note_csn_id (generally a bad idea as id fields tend to grow in length)
  • changing how note_csn_id is populated to make it unique to each grouping of notes (the better idea if you have control over how note_csn_id is populated)

In either case you will need to also remove the join on Line from the sub-select and remove line from the group by clause.

If you are able to make the change to how note_csn_id is populated such that it looks like this:

INSERT INTO #TEST_DEPT_NAME
VALUES ('123456','1234567','12345678' ,'TEST','001234' ,1 ,'NOTES_1.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','001234' ,1 ,'NOTES_1.1.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','001234' ,2 ,'NOTES_1.2' ,'999999','TEST 1','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','001234' ,3 ,'NOTES_1.3' ,'999999','TEST 1','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,1 ,'NOTES_2.1' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,2 ,'NOTES_2.2' ,'1234','TEST','Clinic Note','N','Signed')
,('123456','1234567','12345678' ,'TEST','66666' ,3 ,'NOTES_2.3' ,'1234','TEST','Clinic Note','N','Signed')

Then you can change the select to the below:

SELECT distinct  
ID,
[CSN_ID],
[NOTE_ID],
[DEPARTMENT_NAME],
[NOTE_TYPE_NAME],
[IS_ARCHIVED_YN],
[NOTE_TEXT] =  (select ' '+ case when [NOTE_TEXT]= ''  then null else [NOTE_TEXT]end   from  #TEST_DEPT_NAME P1 
WHERE P1.[ID] = P2.[ID]AND P1.[CSN_ID] =P2.[CSN_ID] AND P1.NOTE_ID = P2.NOTE_ID AND P1.DEPARTMENT_NAME = P2.DEPARTMENT_NAME
 AND P1.NOTE_TYPE_NAME =P2.NOTE_TYPE_NAME AND P1.NOTE_CSN_ID = P2.NOTE_CSN_ID AND P1.IS_ARCHIVED_YN =P2.IS_ARCHIVED_YN  FOR XML PATH(''))

FROM #TEST_DEPT_NAME  P2 WHERE [ID] = '123456' 

GROUP BY ID,CSN_ID,[NOTE_ID],[DEPARTMENT_NAME],[NOTE_TYPE_NAME],[IS_ARCHIVED_YN],NOTE_CSN_ID

and get this result:

ID      CSN_ID  NOTE_ID     DEPARTMENT_NAME NOTE_TYPE_NAME  IS_ARCHIVED_YN  NOTE_TEXT
123456  1234567 12345678    TEST            Clinic Note     N                NOTES_1.1 NOTES_1.1.1 NOTES_1.2 NOTES_1.3
123456  1234567 12345678    TEST            Clinic Note     N                NOTES_2.1 NOTES_2.2 NOTES_2.3

you can do it by using left and stuff functions with group by :

select t.ID ,t.CSN_ID,t.NOTE_ID,t.DEPARTMENT_NAME,
       STUFF((
        select ' ' + t1.NOTE_TEXT
        from #TEST_DEPT_NAME t1
        where t.ID =t1.ID  and t.CSN_ID=t1.CSN_ID and t.NOTE_ID=t1.NOTE_ID
              and t.DEPARTMENT_NAME=t1.DEPARTMENT_NAME 
              and left(t.NOTE_TEXT,8)=left(t1.NOTE_TEXT,8)
        for xml path(''), type
    ).value('.', 'varchar(max)'), 1, 1, '') NOTE_TEXT_
from #TEST_DEPT_NAME t
group by t.ID ,t.CSN_ID,t.NOTE_ID,t.DEPARTMENT_NAME,left(t.NOTE_TEXT,8)

Output:

ID      CSN_ID  NOTE_ID     DEPARTMENT_NAME NOTE_TEXT_
123456  1234567 12345678    TEST            NOTES_1.1 NOTES_1.1.1 NOTES_1.2 NOTES_1.3
123456  1234567 12345678    TEST            NOTES_2.1 NOTES_2.2 NOTES_2.3

Edit: if you have more than 9 NOTE_TEXT I mean NOTES_1..,NOTES_2...,NOTES_10...,NOTES_100... then you have to use charindex('.',t.NOTE_TEXT) instead of 8 in above query:

select t.ID ,t.CSN_ID,t.NOTE_ID,t.DEPARTMENT_NAME,
       STUFF((
        select ' ' + t1.NOTE_TEXT
        from #TEST_DEPT_NAME t1
        where t.ID =t1.ID  and t.CSN_ID=t1.CSN_ID and t.NOTE_ID=t1.NOTE_ID
              and t.DEPARTMENT_NAME=t1.DEPARTMENT_NAME 
              and left(t.NOTE_TEXT,charindex('.',t.NOTE_TEXT))
                  =left(t1.NOTE_TEXT,charindex('.',t1.NOTE_TEXT))
        for xml path(''), type
    ).value('.', 'varchar(max)'), 1, 1, '') NOTE_TEXT_
from #TEST_DEPT_NAME t
group by t.ID ,t.CSN_ID,t.NOTE_ID,t.DEPARTMENT_NAME,
         left(t.NOTE_TEXT,charindex('.',t.NOTE_TEXT))

I think this query is the desired one:

SELECT distinct  ID,[CSN_ID],[NOTE_ID],[DEPARTMENT_NAME],[NOTE_TEXT] =
(
select ' '+ case when [NOTE_TEXT]= ''  then null else [NOTE_TEXT]end
from  #TEST_DEPT_NAME P1 
WHERE P1.[ID] = P2.[ID]
AND P1.[CSN_ID] =P2.[CSN_ID] 
AND P1.NOTE_ID = P2.NOTE_ID 
AND P1.DEPARTMENT_NAME = P2.DEPARTMENT_NAME
AND P1.NOTE_TYPE_NAME =P2.NOTE_TYPE_NAME 
AND P1.IS_ARCHIVED_YN =P2.IS_ARCHIVED_YN  
AND SUBSTRING(P1.NOTE_TEXT, 0, CHARINDEX('.', P1.NOTE_TEXT)) = SUBSTRING(P2.NOTE_TEXT, 0, CHARINDEX('.', P2.NOTE_TEXT))
FOR XML PATH('')
)
FROM #TEST_DEPT_NAME  P2 WHERE [ID] = '123456' 
GROUP BY ID,CSN_ID,[NOTE_ID],[DEPARTMENT_NAME],[NOTE_TYPE_NAME],[IS_ARCHIVED_YN],LINE,NOTE_CSN_ID,SUBSTRING([NOTE_TEXT], 0, CHARINDEX('.', [NOTE_TEXT]))

Result:

ID      CSN_ID  NOTE_ID     DEPARTMENT_NAME    NOTE_TEXT
123456  1234567 12345678    TEST               NOTES_1.1 NOTES_1.1.1 NOTES_1.2 NOTES_1.3
123456  1234567 12345678    TEST               NOTES_2.1 NOTES_2.2  NOTES_2.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM