SQL Server - 每个id的多个图像需要限制每个id一个图像

时间:2017-08-09 00:37:17

标签: sql sql-server sql-server-2008

我有以下社区帮助我的查询:

SELECT DISTINCT
    a.*
    , cast(b.Bytes as varbinary(max)) as Bytes

FROM
    (
SELECT 
    a_inner.Number
    , a_inner.Latitude
    , a_inner.Longitude
    , b_inner.RetiredOn
    , a_inner.Name
    , a_inner.Zipcode
    , b_inner.Oid

FROM
    "AM-Martin".dbo.CpCore_Site a_inner
    INNER JOIN "AM-Martin".dbo.CpSm_Face b_inner on b_inner.SiteId = a_inner.Oid

WHERE
    b_inner.RetiredOn LIKE '%9999%'
     AND b_inner.Number LIKE N'%LA%' OR b_inner.Number LIKE N'%LC%' OR b_inner.Number LIKE N'%BH%'
     AND b_inner.Latitude > 0.0


) AS a 
INNER JOIN "AM-Martin_bin".dbo.CpCore_Image b on a.Oid = b.OwnerId 

这给了我以下结果:

enter image description here

每个Oid都分配了多个图像,因此看起来我正在获得重复。我试图将我的结果限制为每个oid一个图像,所以我不会重复。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果这是您的Core表:

SELECT 
    a_inner.Number
    , a_inner.Latitude
    , a_inner.Longitude
    , b_inner.RetiredOn
    , a_inner.Name
    , a_inner.Zipcode
    , b_inner.OiD
FROM
    "AM-Martin".dbo.CpCore_Site a_inner

尝试改变:

LEFT JOIN Instead of Inner Join as well as below query.