加入

时间:2015-08-23 09:10:29

标签: sql sql-server database tsql

我在SQL Server 2012中有这三个表。

CREATE TABLE [dbo].[ClientInfo]
(
    [ClientID] [int] IDENTITY(1,1) NOT NULL,
    [ClientName] [varchar](50) NULL,
    [ClientAddress] [varchar](50) NULL,
    [City] [varchar](50) NULL,
    [State] [varchar](50) NULL,
    [DOB] [date] NULL,
    [Country] [varchar](50) NULL,
    [Status] [bit] NULL,
    PRIMARY KEY (ClientID)
)

CREATE TABLE [dbo].[ClientInsuranceInfo]
(
    [InsID] [int] IDENTITY(1,1) NOT NULL,
    [ClientID] [int] NULL,
    [InsTypeID] [int] NULL,
    [ActiveDate] [date] NULL,
    [InsStatus] [bit] NULL,    
    PRIMARY KEY (InsID)
)

CREATE TABLE [dbo].[TypeInfo]
(
    [TypeID] [int] IDENTITY(1,1) NOT NULL,
    [TypeName] [varchar](50) NULL,
    PRIMARY KEY (TypeID)
)    

执行查询的一些示例数据

insert into ClientInfo (ClientName, State, Country, Status) 
values ('Lionel Van Praag', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Bluey Wilkinson', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Jack Young', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Keith Campbell', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Tom Phillis', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Barry Smith', 'NSW', 'Australia', 'True')
insert into ClientInfo (ClientName, State, Country, Status) 
values ('Steve Baker', 'NSW', 'Australia', 'True')

insert into TypeInfo (TypeName) values ('CarInsurance')
insert into TypeInfo (TypeName) values ('MotorcycleInsurance')
insert into TypeInfo (TypeName) values ('HeavyVehicleInsurance')

insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus)
values ('1', '1', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('1', '2', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('2', '1', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('2', '2', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('2', '3', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('3', '1', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('4', '1', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('4', '3', '2000-01-11', 'True')
insert into ClientInsuranceInfo (ClientID, InsTypeID, ActiveDate, InsStatus) 
values ('5', '2', '2000-01-11', 'True')

我编写了以下查询,该查询仅返回具有“MotorcycleInsurance”类型的客户:

select distinct 
    ClientInfo.ClientID, ClientInfo.ClientName, TypeInfo.TypeName
from 
    ClientInfo 
left join 
    ClientInsuranceInfo on ClientInfo.ClientID = ClientInsuranceInfo.ClientID
left join 
    TypeInfo on ClientInsuranceInfo.InsTypeID = TypeInfo.TypeID 
             and TypeInfo.TypeID = 2
where 
    typeinfo.TypeName is not null

但我想做以下事情

  • 我想让所有拥有'MotorcycleInsurance'的客户以及其他没有'MotorcycleInsurance'的客户退回。
  • TypeName将返回为没有“MotorcycleInsurance”
  • 的NULL
  • ClientID必须在结果集中是唯一的。
  • 不想使用UNION / UNION ALL

我该怎么做?

我的答案如下:

enter image description here

3 个答案:

答案 0 :(得分:1)

只需删除where子句并稍微更改连接条件

select distinct ClientInfo.ClientID, ClientInfo.ClientName, TypeInfo.TypeName
from ClientInfo 
left join ClientInsuranceInfo 
         on ClientInfo.ClientID = ClientInsuranceInfo.ClientID 
            and ClientInsuranceInfo.InsTypeID = 2 
left join TypeInfo on ClientInsuranceInfo.InsTypeID = TypeInfo.TypeID 

答案 1 :(得分:1)

使用ROW_NUMBER

试试这个
select a.ClientID, a.ClientName, a.TypeName
from 
(
select distinct ClientInfo.ClientID, ClientInfo.ClientName, TypeInfo.TypeName, ROW_NUMBER() over(partition by ClientInfo.ClientID order by case when TypeName is null then 1 else 0 end) as rn
from ClientInfo  
left join ClientInsuranceInfo on ClientInfo.ClientID = ClientInsuranceInfo.ClientID
left join TypeInfo on ClientInsuranceInfo.InsTypeID = TypeInfo.TypeID and TypeInfo.TypeID = 2
) a
where rn = 1

编辑:按case中的Row_Number语句更新了排序:)

答案 2 :(得分:0)

SELECT CI.ClientID, CI.ClientName, TI.TypeName
FROM dbo.ClientInfo AS CI
LEFT JOIN (SELECT ClientID, InsTypeID
           FROM dbo.ClientInsuranceInfo
           WHERE InsTypeID = 2 
           GROUP BY ClientID, InsTypeID ) AS CII ON CI.ClientID = CII.ClientID
LEFT JOIN dbo.TypeInfo AS TI  ON CII.InsTypeID = TI.TypeID;