加入导致SQL Server R2中的重复记录

时间:2014-06-23 10:36:48

标签: sql-server-2008-r2

我正在加入两张桌子。我的表格如下

                  **Table_1**
**T_ID**     **CategoryID**      **Name**      .....
 01             219             ABC       .....
 02             218             MNO       .....
 03             219             SAO       .....
 04             222             JKO       .....
 05             222             SDF       .....
 06             222             VBC       .....
 07             222             KLJ       .....
  .              .              .           .
  .              .              .           .
  .              .              .           .

                  **Table_2**
 P_ID          NormalImage        IsProfile       T_ID
 01             <Binary Data>      1              01
 02             <Binary Data>      0              01
 03             <Binary Data>      0              01
 04             <Binary Data>      0              01
 05             <Binary Data>      1              04
 06             <Binary Data>      0              04
 07             <Binary Data>      1              01
 08             <Binary Data>      0              01
  .              .              .           .
  .              .              .           .
  .              .              .           .

和我的查询如下

   select   AdInstance.AdID
   ,AdInstance.Title
    ,CASE
       WHEN DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE()) < 24 THEN 
           CASE DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE()) 
               WHEN 1 THEN 
                  CONVERT(VARCHAR, DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE())) + ' hour ago' 
               ELSE 
                  CONVERT(VARCHAR, DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE())) + ' hours ago'
           END
      ELSE 
     REPLACE(CONVERT(VARCHAR, AdInstance.PublishedDate, 6), ' ', '-')
   END as PublishedDate 
    ,left(AdInstance.Description,120) +'...' as Description 
    ,PhotoDetails.NormalImage
    ,PhotoDetails.AdPhotoID
    ,count(*)
       from Table_1 as AdInstance
       left  join Table_2 as PhotoDetails on PhotoDetails.AdID = AdInstance.AdID

       Where Adinstance.CategoryID= 219 and PhotoDetails.IsProfileImage = 'true' 

此查询仅检索Table_2中的数据。我想检索Table_2中存在的数据,其中isProfile为true,而Table_2中没有的数据。如何在SQL SERVER 2008 R2中满足这种情况。

1 个答案:

答案 0 :(得分:0)

根据您的问题,我认为此代码可以执行您正在寻找的内容:返回记录,其中JOIN条件返回isProfile = 'true'或没有记录。过滤到isProfile = 'true'将排除任何认为isProfile为NULL的情况,例如表2中没有匹配的AdID

在我的查询中,我添加了短语PhotoDetails.AdID IS NULL以检查记录是否存在,因为它是JOIN条件的一部分 - 您必须验证它是否按预期工作。

SELECT 
     AdInstance.AdID
    ,AdInstance.Title
    ,CASE
        WHEN DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE()) < 24 
        THEN 
            CASE DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE()) 
                WHEN 1 
                THEN CONVERT(VARCHAR, DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE())) + ' hour ago' 
                ELSE CONVERT(VARCHAR, DATEDIFF(HOUR, AdInstance.PublishedDate, GETDATE())) + ' hours ago'
            END
        ELSE REPLACE(CONVERT(VARCHAR, AdInstance.PublishedDate, 6), ' ', '-')
     END as PublishedDate 
    ,LEFT(AdInstance.Description,120) +'...' as Description 
    ,PhotoDetails.NormalImage
    ,PhotoDetails.AdPhotoID
    ,count(*) TotalRecords -- added a column name
FROM 
    Table_1 as AdInstance
     LEFT JOIN 
    Table_2 as PhotoDetails ON 
        PhotoDetails.AdID = AdInstance.AdID
WHERE 
    Adinstance.CategoryID= 219 AND
      (
        PhotoDetails.IsProfileImage = 'true' OR
        PhotoDetails.AdID IS NULL -- the record does not exist in the PhotoDetails table
      )