MS SQL 2008 join - 从许多结果中选择一个

时间:2012-06-20 09:21:23

标签: sql sql-server-2008 join

我正在尝试运行以下查询,但我不确定如何将其限制为仅一个结果。 在下面的查询中,clientcontactid 21901工作的客户端有2个地址,意味着2个结果返回。

查询:

select  cc.contactpersonid,
    cc.clientcontactid,
    ad.city,
    ad.addressid
from SavedList sl
inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
inner join Clients c on c.ClientID = cc.ClientId
inner join Address ad on c.ClientID = ad.ObjectId
where sl.SavedListId = 2117

结果:

contactpersonid clientcontactid city    addressid
87934           21901                   145186
87934           21901           London  1130705
89778           17275           Leeds   145368

我需要返回其中一个结果,以便客户联系 21901 ,优先考虑其中包含城市的结果。我已经尝试过选择 top(1),但我认为这取决于连接强制多条记录。关于如何仅返回1个结果的任何帮助,以及如何控制它将受到高度赞赏!

由于

5 个答案:

答案 0 :(得分:6)

尝试:

;WITH a AS (
select  cc.contactpersonid,
    cc.clientcontactid,
    ad.city,
    ad.addressid,
    ROW_NUMBER() OVER (PARTITION BY cc.clientcontactid ORDER BY ad.city DESC) AS RowNum
    from SavedList sl
    inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
    inner join Clients c on c.ClientID = cc.ClientId
    inner join Address ad on c.ClientID = ad.ObjectId
    where sl.SavedListId = 2117
)

SELECT  *
FROM    a
WHERE   RowNum = 1

答案 1 :(得分:4)

我通常对结果进行优先排序的方法是实际分配优先级值列。在这种情况下,它可以保持简单,因为只有一个优先级:城市的记录来自没有城市的城市:

with q as(
select  cc.contactpersonid,
    cc.clientcontactid,
    ad.city,
    ad.addressid,
    case when ad.city is null then 0 else 1 end prior
from SavedList sl
inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
inner join Clients c on c.ClientID = cc.ClientId
inner join Address ad on c.ClientID = ad.ObjectId
where sl.SavedListId = 2117
)
select top 1 * from q order by prior desc

答案 2 :(得分:1)

大但有效:

select  cc.contactpersonid,
        cc.clientcontactid,
        ad.city,
        ad.addressid
from SavedList sl
inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
inner join Clients c on c.ClientID = cc.ClientId
inner join Address ad on c.ClientID = ad.ObjectId
where sl.SavedListId = 2117 and (ad.city is not null or ad.city <> '')
UNION
select  cc.contactpersonid,
        cc.clientcontactid,
        ad.city,
        ad.addressid
from SavedList sl
inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
inner join Clients c on c.ClientID = cc.ClientId
inner join Address ad on c.ClientID = ad.ObjectId
where sl.SavedListId = 2117 and (ad.city is null or ad.city = '')
and cc.clientcontactid not in (
  select  cc.clientcontactid
  from SavedList sl
  inner join ClientContacts cc on cc.ContactPersonId = sl.ObjectId
  inner join Clients c on c.ClientID = cc.ClientId
  inner join Address ad on c.ClientID = ad.ObjectId
  where sl.SavedListId = 2117 and (ad.city is not null or ad.city <> '')
)

答案 3 :(得分:0)

您可以通过添加select top 1 record的条件来尝试city is not NULL

答案 4 :(得分:0)

试试这个:

;WITH cte
     AS (SELECT cc.contactpersonid,
                cc.clientcontactid,
                ad.city,
                ad.addressid,
                Row_number() OVER (PARTITION BY cc.clientcontactid ORDER BY CASE WHEN ad.city <> '' THEN 1 ELSE 0 END) rn
         FROM   SavedList sl
                INNER JOIN ClientContacts cc
                  ON cc.ContactPersonId = sl.ObjectId
                INNER JOIN Clients c
                  ON c.ClientID = cc.ClientId
                INNER JOIN Address ad
                  ON c.ClientID = ad.ObjectId
         WHERE  sl.SavedListId = 2117)
SELECT contactpersonid,
       clientcontactid,
       city,
       addressid
FROM   cte
WHERE  rn = 1