4

我正在尝试运行以下查询,但不确定如何将其限制为仅一个结果。在下面的查询中,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,优先级是其中包含城市的结果。我试过 select top (1)但我认为这归结为强制多条记录返回的连接。任何有关如何仅返回 1 个结果以及如何控制该结果的帮助将不胜感激!

谢谢

4

5 回答 5

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
于 2012-06-20T09:28:45.890 回答
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
于 2012-06-20T09:26:41.077 回答
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 <> '')
)
于 2012-06-20T09:27:08.487 回答
0

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

于 2012-06-20T09:24:33.080 回答
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
于 2012-06-20T09:33:30.647 回答