SQL查询返回记录

时间:2014-12-09 09:20:47

标签: sql

我的sp

中有两个查询

第一个查询返回类似

的值
ID   Name Department 

1     Sang    IT

2    Div       IT

3     SS      EEE

我有第二个查询返回值

1  Sang  IT

2  Div    IT

4   Pav   EEE

现在结合我需要得到的结果

ID   Name Department

1     Sang    IT

2    Div       IT

3     SS      EEE

4   Pav   EEE

我怎么能得到这个?我尝试使用union但它对我有用,它会返回重复值。

这是我的完整查询,我不知道如何删除重复项?

SELECT 
Riskdetail.RiskID,Riskdetail.[RiskTitle],a.RiskRegisterID,DivisionDesc,BusinessGroup.BusinessGroupDesc,a.RiskRegisterDesc,RiskGroup.RiskGroupDesc, 
dbo.GetUserName(RiskDetail.RiskOwnerID) as [Assigned RiskOwner],dbo.GetUserName(RiskDetail.RiskOwner2ID) as [Assigned RiskOwner2],
dbo.GetUserName(RiskDetail.ActionOwnerID) as [Assigned ActionOwner],dbo.GetUserNameFromLoginName(LoginName) as [Assigned User],
[Assigned UserRole] = 
    STUFF((SELECT distinct '','' + Role.RoleDesc 
           FROM UserRole b 
           inner join Role on Role.RoleID=b.RoleID
           WHERE b.RiskRegisterID = a.RiskRegisterID 
           and b.UserID in ('+ @UserID +') 
          FOR XML PATH('''')), 1, 1, '''')
FROM RiskRegister a
inner join RiskDetail on RiskDetail.RiskRegisterID=a.RiskRegisterID
INNER JOIN  RiskGroup ON a.RiskGroupID = RiskGroup.RiskGroupID
INNER JOIN Division ON RiskGroup.DivisionID=Division.DivisionID
INNER JOIN BusinessGroup ON Division.BusinessGroupID=BusinessGroup.BusinessGroupID
INNER JOIN [UserRole] on [UserRole].RiskRegisterID=a.RiskRegisterID
inner join [User] on [User].UserID=UserRole.UserID
where ([User].UserID in ('+ @UserID +') and RiskDetail.RiskStatusID=100 and a.Deleted=0 and a.Inactive=0)
GROUP BY 
a.RiskRegisterID,Riskdetail.RiskID,Riskdetail.[RiskTitle],
DivisionDesc,BusinessGroup.BusinessGroupDesc,a.RiskRegisterDesc,RiskGroup.RiskGroupDesc,
dbo.GetUserNameFromLoginName(LoginName),
dbo.GetUserName(RiskDetail.RiskOwnerID),dbo.GetUserName(RiskDetail.RiskOwner2ID),
dbo.GetUserName(RiskDetail.ActionOwnerID)
union 
select RiskDetail.RiskID,Riskdetail.RiskTitle,Riskregister.RiskRegisterID,Division.DivisionDesc,BusinessGroup.BusinessGroupDesc,RiskRegister.RiskRegisterDesc,RiskGroup.RiskGroupDesc,
    dbo.GetUserName(RiskDetail.RiskOwnerID) as [Assigned RiskOwner] ,
    dbo.GetUserName(RiskDetail.RiskOwner2ID) as [Assigned RiskOwner2],
    dbo.GetUserName(RiskDetail.ActionOwnerID) as [Assigned ActionOwner],NULL as [Assigned User], 
    Null as [Assigned UserRole]
    from RiskDetail 
    join RiskRegister on  Riskdetail.RiskRegisterID=RiskRegister.RiskRegisterID
    join RiskGroup ON RiskRegister.RiskGroupID = RiskGroup.RiskGroupID
    JOIN Division ON RiskGroup.DivisionID=Division.DivisionID  
    JOIN BusinessGroup ON Division.BusinessGroupID=BusinessGroup.BusinessGroupID 
    where RiskDetail.RiskStatusID=100 and RiskRegister.Deleted=0 and RiskRegister.Inactive=0 and (RiskDetail.ActionOwnerID in ('+ @UserID +') or RiskDetail.RiskOwnerID in ('+ @UserID +') or RiskDetail.RiskOwner2ID in ('+ @UserID +'))' 

5 个答案:

答案 0 :(得分:3)

使用union

select id, name, departement
from ... 
UNION
select id, name, departement
from ...

删除重复项尝试

select distinct id, name, departement
from ( 
    select id, name, departement
    from ... 
    UNION
    select id, name, departement
    from ...)

select * from ( 
    select id, name, departement
    from ... 
    UNION
    select id, name, departement
    from ...)
group by id, name, departement

答案 1 :(得分:0)

我会使用联盟,但你总是可以使用左联盟...

SELECT ISNULL(a.id,b.id) AS 'ID'
       ISNULL(a.Name,b.Name) AS 'Name'
       ISNULL(a.Department,b.Department) AS 'Department'
FROM TABLEA AS A LEFT JOIN
     TABLEB AS B ON a.ID = b.ID

答案 2 :(得分:0)

你也可以这样写:

select id, name, departement
from Query1
union 
(select id, name, departement
from Query2
except 
select id, name, departement
from Query1
)

答案 3 :(得分:0)

假设您的SELECTS将是:

SELECT a.a, a.b FROM whatever1 a WHERE a.a=foo
SELECT b.c, b.d FROM WHATEVER2 b WHERE b.c=bar

我会尝试加入两个,然后连接(无NULL)值。我想是这样的:

SELECT f.fc, CONCAT(nvl(f.fd, ""), nvl(g.gb, "")) FROM (SELECT b.c fc, b.d fd FROM WHATEVER2 b WHERE b.c=bar) f, (SELECT a.a ga, a.b gb FROM whatever1 a WHERE a.a=foo) g WHERE f.fc = g.gb

答案 4 :(得分:0)

Here is my complete query, i am not sure how to remove duplicates?

SELECT 
Riskdetail.RiskID,Riskdetail.[RiskTitle],a.RiskRegisterID,DivisionDesc,BusinessGroup.BusinessGroupDesc,a.RiskRegisterDesc,RiskGroup.RiskGroupDesc, 
dbo.GetUserName(RiskDetail.RiskOwnerID) as [Assigned RiskOwner],dbo.GetUserName(RiskDetail.RiskOwner2ID) as [Assigned RiskOwner2],
dbo.GetUserName(RiskDetail.ActionOwnerID) as [Assigned ActionOwner],dbo.GetUserNameFromLoginName(LoginName) as [Assigned User],
[Assigned UserRole] = 
    STUFF((SELECT distinct '','' + Role.RoleDesc 
           FROM UserRole b 
           inner join Role on Role.RoleID=b.RoleID
           WHERE b.RiskRegisterID = a.RiskRegisterID 
           and b.UserID in ('+ @UserID +') 
          FOR XML PATH('''')), 1, 1, '''')
FROM RiskRegister a
inner join RiskDetail on RiskDetail.RiskRegisterID=a.RiskRegisterID
INNER JOIN  RiskGroup ON a.RiskGroupID = RiskGroup.RiskGroupID
INNER JOIN Division ON RiskGroup.DivisionID=Division.DivisionID
INNER JOIN BusinessGroup ON Division.BusinessGroupID=BusinessGroup.BusinessGroupID
INNER JOIN [UserRole] on [UserRole].RiskRegisterID=a.RiskRegisterID
inner join [User] on [User].UserID=UserRole.UserID
where ([User].UserID in ('+ @UserID +') and RiskDetail.RiskStatusID=100 and a.Deleted=0 and a.Inactive=0)
GROUP BY 
a.RiskRegisterID,Riskdetail.RiskID,Riskdetail.[RiskTitle],
DivisionDesc,BusinessGroup.BusinessGroupDesc,a.RiskRegisterDesc,RiskGroup.RiskGroupDesc,
dbo.GetUserNameFromLoginName(LoginName),
dbo.GetUserName(RiskDetail.RiskOwnerID),dbo.GetUserName(RiskDetail.RiskOwner2ID),
dbo.GetUserName(RiskDetail.ActionOwnerID)
union 
select RiskDetail.RiskID,Riskdetail.RiskTitle,Riskregister.RiskRegisterID,Division.DivisionDesc,BusinessGroup.BusinessGroupDesc,RiskRegister.RiskRegisterDesc,RiskGroup.RiskGroupDesc,
    dbo.GetUserName(RiskDetail.RiskOwnerID) as [Assigned RiskOwner] ,
    dbo.GetUserName(RiskDetail.RiskOwner2ID) as [Assigned RiskOwner2],
    dbo.GetUserName(RiskDetail.ActionOwnerID) as [Assigned ActionOwner],NULL as [Assigned User], 
    Null as [Assigned UserRole]
    from RiskDetail 
    join RiskRegister on  Riskdetail.RiskRegisterID=RiskRegister.RiskRegisterID
    join RiskGroup ON RiskRegister.RiskGroupID = RiskGroup.RiskGroupID
    JOIN Division ON RiskGroup.DivisionID=Division.DivisionID  
    JOIN BusinessGroup ON Division.BusinessGroupID=BusinessGroup.BusinessGroupID 
    where RiskDetail.RiskStatusID=100 and RiskRegister.Deleted=0 and RiskRegister.Inactive=0 and (RiskDetail.ActionOwnerID in ('+ @UserID +') or RiskDetail.RiskOwnerID in ('+ @UserID +') or RiskDetail.RiskOwner2ID in ('+ @UserID +'))'