需要正确显示联合数据

时间:2018-06-04 09:20:40

标签: sql sql-server tsql

我试图反对此表中患者选择的数据,而另一个表都显示它,但是所有患者数据都显示为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 :(得分:1)

这篇文章在上面的帖子中

 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