Необходимо правильно отобразить данные объединения - PullRequest
0 голосов
/ 04 июня 2018

Я пытаюсь сопоставить данные, выбранные пациентом в этой таблице, и все другие таблицы показывают это, но есть все данные пациента, отображаемые с IsKeyAttrIsChecked флагом

Запрос: -

 select 
    a.KeyAttribute,
    a.PatKeyAttId,
    b.IsKeyAttrIsChecked,
    b.PatientUserId
from PatientKeyAttributeMaster a 
    Left join PatientKeyAttributeMap b 
        on (a.PatKeyAttId = b.PatKeyAttributeId)
UNION 
select 
    keyAttribute,
    PatKeyAttId,
    IsKeyAttrIsChecked,
    PatientUserId
from 
    (
        select 
            a.KeyAttribute,
            a.PatKeyAttId,
            b.IsKeyAttrIsChecked,
            b.PatientUserId
        from PatientKeyAttributeMaster a 
            inner join PatientKeyAttributeMap b 
                on (a.PatKeyAttId = b.PatKeyAttributeId)
        where b.PatientUserId = 176845 or b.IsKeyAttrIsChecked=1
    ) as a 
group by keyAttribute,PatKeyAttId,IsKeyAttrIsChecked,PatientUserId

вывод: -

   KeyAttribute             |   PatKeyAttId |IsKeyAttrIsChecked|PatientUserId
Anxiety                         4041            NULL            NULL
Drop in work performance        4039            1               177849
Drowsiness                      4032            NULL            NULL
Excess weight gain              4036            NULL            NULL
Irritability                    4040            1               171834
 Anger at work                  4040            1               177847
Anger at work                   4040            1               177849
Persistent backache             4034            1               171834
Persistent cough                4035            1               176845

Ожидаемый вывод: -

KeyAttribute            |   PatKeyAttId |IsKeyAttrIsChecked|PatientUserId
Anxiety                         4041            NULL            NULL
Drop in work performance        4039            0               NULL
Drowsiness                      4032            NULL            NULL
Excess weight gain              4036            NULL            NULL
Persistent cough                4035            1               176845
Irritability                    4040            0               NULL
 Anger at work                  4040            0               NULL
Anger at work                   4040            0               NULL
Persistent backache             4034            0               NULL

1 Ответ

0 голосов
/ 04 июня 2018

это ответ в вышеуказанном посте

 IF EXISTS (SELECT PatientUserId FROM PatientKeyAttributeMap WHERE PatientUserId = 177848)
        Begin


            DECLARE @TEMPDATA TABLE 
            (
             PatKeyAttId int
            ,KeyAttribute nvarchar(max)
            ,IsKeyAttrIsChecked bit 
            ,PatientUserId int
            ,KeyAttributeCategory nvarchar(800)
            )

            --select * from @TEMPDATA
            insert into @TEMPDATA ( PatKeyAttId ,KeyAttribute ,IsKeyAttrIsChecked ,PatientUserId ,KeyAttributeCategory)
            select a.PatKeyAttId,  a.KeyAttribute,0 as IsKeyAttrIsChecked,177848,b.KeyAttrCategory
            from PatientKeyAttributeMaster a 
            inner join KeyAttributeCategory b on (a.KeyAttributeCategoryId = b.KeyAttributeCategoryId ) 

            update a 
            set a.IsKeyAttrIsChecked = 1
            from @TEMPDATA a 
            where a.PatKeyAttId in (
            select PatKeyAttributeId from PatientKeyAttributeMap where PatientUserId = 177848 and  IsKeyAttrIsChecked = 1
            )
            --select * from @TEMPDATA


             WITH List AS(
            SELECT  ROW_NUMBER() OVER(ORDER BY PatKeyAttId)  as RowNumber,
            PatKeyAttId 
            ,KeyAttribute 
            ,IsKeyAttrIsChecked 
            ,PatientUserId
            ,KeyAttributeCategory
             from @TEMPDATA
             )

            select a.* ,b.TotalRecords as TotalRecords 
            from List a
            LEFT JOIN (
                Select max(RowNumber) TotalRecords from  List 
            ) b on (1 = 1)
    End
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...