Sql 需要正确显示联合数据

Sql 需要正确显示联合数据,sql,sql-server,tsql,Sql,Sql Server,Tsql,我正在尝试在该表和另一个表中显示患者选择的数据,但所有患者数据都显示为IsKeyAttrIsChecked标志 问题:- select a.KeyAttribute, a.PatKeyAttId, b.IsKeyAttrIsChecked, b.PatientUserId from PatientKeyAttributeMaster a Left join PatientKeyAttributeMap b on (a.PatKeyA

我正在尝试在该表和另一个表中显示患者选择的数据,但所有患者数据都显示为
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

这是上面的帖子

 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

恐怕我不知道你希望我们怎么回答这个问题。个别语句返回什么?他们给了你正确的行吗?我们这里没有任何原始数据。例如,为什么
工作愤怒
在预期输出中有
NULL
值?为什么值
177847
177849
不适用,因为它们显然是由数据集返回的。这是一个很好的开始:另外,请对齐您的列,您的数据很难像这样读取。而且,我最讨厌的一点是:。这在您的查询中尤其糟糕,其中
a
可以是第一个子查询或第二个子查询中的
PatientKeyAttributeMaster
,或者
a
可以是第一个子查询中返回的整个数据集<代码>b同样糟糕。我真的建议研究如何使用合适的别名。@Larnu不,先生,这个数据就是dumy数据,举个例子,所有这些值都在一列中。