显示两列的不匹配值查询

时间:2019-05-27 12:06:17

标签: sql-server

尝试了此查询:


    SELECT     Movements.[date of movement], 
               Movements.[Date of value], 
               Movements.Description 
    FROM       Movements 
    INNER JOIN Documents 
    on         Movements.Description<> '%' + Documents.Costumer+ '%'  
    AND        Documents.[Value with IVA] <> Movements.Value 
    Group by   Movements.[date of Movement], 
               Movements.[Date of value], 
               Movements.Description, 
               Documents.Costumer, 
               Documents.[Value with IVA]

问题是他向我展示了与描述不匹配的所有Costumers。而我想要的是一次不匹配的描述。  我想向我展示:

Date of movement    Date of Value    Description
12-12-2011           1-12-2011       123123
12-12-2011           12-12-2011      121121
13-12-2011           13-12-2011      121121

向我展示的是这个

Date of movement    Date of Value    Description
  12-12-2011           1-12-2011       123123
  12-12-2011           12-12-2011      121121
  13-12-2011           13-12-2011      121121
  12-12-2011           1-12-2011       123123
  12-12-2011           12-12-2011      121121
  13-12-2011           13-12-2011      121121
  12-12-2011           1-12-2011       123123
  12-12-2011           12-12-2011      121121
  13-12-2011           13-12-2011      121121

2 个答案:

答案 0 :(得分:0)

我认为您的意思是使用LIKE运算符而不是<>

on Movements.Description not like '%' + Documents.Costumer + '%'

或者是另一种方式:

on Documents.Costumer not like '%' + Movements.Description + '%'

答案 1 :(得分:0)

为此:

 SELECT     Movements.[date of movement], 
           Movements.[Date of value], 
           Movements.Description 
FROM       Movements 
INNER JOIN Documents 
on         Movements.Description<> '%' + Documents.Costumer+ '%'  
AND        Documents.[Value with IVA] <> Movements.Value 
Group by   Movements.[date of Movement], 
           Movements.[Date of value], 
           Movements.Description, 
           Documents.Costumer, 
           Documents.[Value with IVA]

执行此操作:

 SELECT     Movements.[date of movement], 
           Movements.[Date of value], 
           Movements.Description 
FROM       Movements 
INNER JOIN Documents 
on         Movements.Description<> '%' + Documents.Costumer+ '%'  
AND        Documents.[Value with IVA] <> Movements.Value 
Group by   Movements.[date of Movement], 
           Movements.[Date of value], 
           Movements.Description

ps:所有学分归@Squirrel