4

次のクエリを実行しようとしていますが、結果を 1 つだけに制限する方法がわかりません。以下のクエリでは、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について、これらの結果の 1 つを返す必要があります。都市が含まれる結果が優先されます。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

結果に優先順位を付けるために通常行うことは、実際に優先値列を割り当てることです。この場合、優先順位が 1 つしかないため、シンプルに保つことができます。都市があるレコードは、都市がない都市よりも優先されます。

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

これを試して:

;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 に答える
0

select top 1 record条件 where を追加して試すことができcity is not NULLます。

于 2012-06-20T09:24:33.080 に答える