SQL Query使用两个不同的数据库删除3个表内连接中的重复值

时间:2017-08-07 04:23:55

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

我正在使用SQL Server数据库,我编写了以下查询:

SELECT 
    "AM-Martin".dbo.CpCore_Site.Latitude,"AM-Martin".dbo.CpCore_Site.Longitude,"AM-Martin".dbo.CpSm_Face.RetiredOn,"AM-Martin_bin".dbo.CpCore_Image.Bytes, "AM-Martin".dbo.CpCore_Site.Name, "AM-Martin".dbo.CpCore_Site.Zipcode, "AM-Martin".dbo.CpCore_Site.Number
FROM
    "AM-Martin".dbo.CpCore_Site
INNER JOIN 
    "AM-Martin".dbo.CpSm_Face on "AM-Martin".dbo.CpSm_Face.SiteId = "AM-Martin".dbo.CpCore_Site.Oid
INNER JOIN 
    "AM-Martin_bin".dbo.CpCore_Image on "AM-Martin".dbo.CpSm_Face.Oid = "AM-Martin_bin".dbo.CpCore_Image.OwnerId
WHERE
    "AM-Martin".dbo.CpSm_Face.RetiredOn LIKE '%9999%'
     AND "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LA%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LC%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%BH%'
     AND "AM-Martin".dbo.CpCore_Site.Latitude > 0.0;

查询返回了我需要的数据,但它复制了一些数据。当我尝试添加Distinct以删除重复数据时...

SELECT DISTINCT 
    "AM-Martin".dbo.CpCore_Site.Latitude,"AM-Martin".dbo.CpCore_Site.Longitude,"AM-Martin".dbo.CpSm_Face.RetiredOn,"AM-Martin_bin".dbo.CpCore_Image.Bytes, "AM-Martin".dbo.CpCore_Site.Name, "AM-Martin".dbo.CpCore_Site.Zipcode, "AM-Martin".dbo.CpCore_Site.Number
FROM
    "AM-Martin".dbo.CpCore_Site
INNER JOIN 
    "AM-Martin".dbo.CpSm_Face on "AM-Martin".dbo.CpSm_Face.SiteId = "AM-Martin".dbo.CpCore_Site.Oid
INNER JOIN 
    "AM-Martin_bin".dbo.CpCore_Image on "AM-Martin".dbo.CpSm_Face.Oid = "AM-Martin_bin".dbo.CpCore_Image.OwnerId
WHERE
    "AM-Martin".dbo.CpSm_Face.RetiredOn LIKE '%9999%'
    AND "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LA%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LC%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%BH%'
    AND "AM-Martin".dbo.CpCore_Site.Latitude > 0.0;

由于此列"AM-Martin_bin".dbo.CpCore_Image.Bytes,此栏目是实际的图像数据,因此不允许我使用它。我得到的错误信息是:

  

错误:无法将图像数据类型选为DISTINCT,因为它无法比较。错误代码:421

我正试图摆脱所有重复的数据。要访问数据库,我使用的是RazorSQL,远程数据库是使用SQL Server的Microsoft Server 2008。

更新

我使用GROUP BY,它看起来像这样:

SELECT 
    "AM-Martin".dbo.CpCore_Site.Latitude,"AM-Martin".dbo.CpCore_Site.Longitude,"AM-Martin".dbo.CpSm_Face.RetiredOn,"AM-Martin_bin".dbo.CpCore_Image.Bytes, "AM-Martin".dbo.CpCore_Site.Name, "AM-Martin".dbo.CpCore_Site.Zipcode, "AM-Martin".dbo.CpCore_Site.Number
FROM
    "AM-Martin".dbo.CpCore_Site

INNER JOIN "AM-Martin".dbo.CpSm_Face on "AM-Martin".dbo.CpSm_Face.SiteId = "AM-Martin".dbo.CpCore_Site.Oid
INNER JOIN "AM-Martin_bin".dbo.CpCore_Image on "AM-Martin".dbo.CpSm_Face.Oid = "AM-Martin_bin".dbo.CpCore_Image.OwnerId

WHERE
    "AM-Martin".dbo.CpSm_Face.RetiredOn LIKE '%9999%'

     AND "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LA%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LC%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%BH%'


     AND "AM-Martin".dbo.CpCore_Site.Latitude > 0.0

GROUP BY "AM-Martin".dbo.CpCore_Site.Name;

现在我收到以下错误:

  

错误:列'AM-Martin.dbo.CpCore_Site.Latitude'无效   选择列表,因为它不包含在聚合中   函数或GROUP BY子句。错误代码:8120

现在,如果我向该组添加更多列,它会继续为下一列说同样的事情,直到我到达"AM-Martin_bin".dbo.CpCore_Image.Bytes我收到以下错误消息:

  

错误:无法比较text,ntext和image数据类型   排序,除非使用IS NULL或LIKE运算符。错误代码:306

1 个答案:

答案 0 :(得分:1)

我想通过group by clause is not working with Image我想要做的就是添加更改图像列的措辞,通过像这样将它CAST到VARBINARY:

SELECT 
    "AM-Martin".dbo.CpCore_Site.Number,"AM-Martin".dbo.CpCore_Site.Latitude,"AM-Martin".dbo.CpCore_Site.Longitude,"AM-Martin".dbo.CpSm_Face.RetiredOn,**CAST("AM-Martin_bin".dbo.CpCore_Image.Bytes as Varbinary)**, "AM-Martin".dbo.CpCore_Site.Name, "AM-Martin".dbo.CpCore_Site.Zipcode
FROM
    "AM-Martin".dbo.CpCore_Site

INNER JOIN "AM-Martin".dbo.CpSm_Face on "AM-Martin".dbo.CpSm_Face.SiteId = "AM-Martin".dbo.CpCore_Site.Oid
INNER JOIN "AM-Martin_bin".dbo.CpCore_Image on "AM-Martin".dbo.CpSm_Face.Oid = "AM-Martin_bin".dbo.CpCore_Image.OwnerId

WHERE
    "AM-Martin".dbo.CpSm_Face.RetiredOn LIKE '%9999%'

     AND "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LA%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%LC%' OR "AM-Martin".dbo.CpCore_Site.Number LIKE N'%BH%'


     AND "AM-Martin".dbo.CpCore_Site.Latitude > 0.0

GROUP BY "AM-Martin".dbo.CpCore_Site.Number,"AM-Martin".dbo.CpCore_Site.Latitude,"AM-Martin".dbo.CpCore_Site.Longitude,"AM-Martin".dbo.CpSm_Face.RetiredOn,**CAST("AM-Martin_bin".dbo.CpCore_Image.Bytes as Varbinary)**, "AM-Martin".dbo.CpCore_Site.Name, "AM-Martin".dbo.CpCore_Site.Zipcode;

我还必须给予@Myonara一些信誉,让我能够通过建议让我走上正确的道路。