使用连接MSSQL过滤值

时间:2016-12-21 09:50:53

标签: sql sql-server sql-server-2014

我有一个左连接,想过滤一些值。但是如果我执行查询,仍会返回值。

这是查询:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],A.[G_L Account No_], A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
LEFT JOIN
[Verploegen POC$G_L Entry] A 
--LEFT JOIN [Verploegen POC$Sales Header] SH
ON SIH.No_  = A.[Document No_]
-- JOIN
--[Verploegen POC$G_L Account] GLA
--ON A.[G_L Account No_] = GLA.No_
--ON SIH.No_  = A.No_
WHERE --A.[Document No_] = '2295848' AND
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' AND  
A.[Posting Date]  BETWEEN '2016-05-30' AND  '2016-07-18' AND  
 (A.[G_L Account No_] = '1031' OR a.[G_L Account No_] = '1011' 
OR a.[G_L Account No_] = '1021' OR A.[G_L Account No_] = '1041' OR A.[G_L Account No_] = '1051')
AND
(A.[G_L Account No_] <> '2210' OR a.[G_L Account No_] <> '2220' OR A.[G_L Account No_] <> '2230'
OR A.[G_L Account No_] <> '2240' OR A.[G_L Account No_] <> '2250'
)

但仍然返回值2210。

见图片:

enter image description here

OKE。所以我试着这样:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],A.[G_L Account No_], A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
LEFT JOIN
[Verploegen POC$G_L Entry] A 
--LEFT JOIN [Verploegen POC$Sales Header] SH
ON SIH.No_  = A.[Document No_]
-- JOIN
--[Verploegen POC$G_L Account] GLA
--ON A.[G_L Account No_] = GLA.No_
--ON SIH.No_  = A.No_
WHERE --A.[Document No_] = '2295848' AND
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' AND  
A.[Posting Date]  BETWEEN '2016-05-30' AND  '2016-07-18' AND  
 (A.[G_L Account No_] = '1031' OR a.[G_L Account No_] = '1011' 
OR a.[G_L Account No_] = '1021' OR A.[G_L Account No_] = '1041' OR A.[G_L Account No_] = '1051')
AND
A.[G_L Account No_] NOT IN('2210','2220', '2230','2240', '2250')

但仍然可以看到值2210。

这是销售发票标题表:

Enabled Field No.   Field Name  Data Type   Length  Description
Yes 2   Sell-to Customer No.    Code    20  
Yes 3   No. Code    20  
Yes 4   Bill-to Customer No.    Code    20  
Yes 5   Bill-to Name    Text    50  
Yes 6   Bill-to Name 2  Text    50  
Yes 7   Bill-to Address Text    50  
Yes 8   Bill-to Address 2   Text    50  
Yes 9   Bill-to City    Text    30  
Yes 10  Bill-to Contact Text    50  
Yes 11  Your Reference  Text    30  
Yes 12  Ship-to Code    Code    10  
Yes 13  Ship-to Name    Text    50  
Yes 14  Ship-to Name 2  Text    50  
Yes 15  Ship-to Address Text    50  
Yes 16  Ship-to Address 2   Text    50  
Yes 17  Ship-to City    Text    30  
Yes 18  Ship-to Contact Text    50  
Yes 19  Order Date  Date        
Yes 20  Posting Date    Date        
Yes 21  Shipment Date   Date        
Yes 22  Posting Description Text    50  
Yes 23  Payment Terms Code  Code    10  
Yes 24  Due Date    Date        
Yes 25  Payment Discount %  Decimal     
Yes 26  Pmt. Discount Date  Date        
Yes 27  Shipment Method Code    Code    10  
Yes 28  Location Code   Code    10  
Yes 29  Shortcut Dimension 1 Code   Code    20  
Yes 30  Shortcut Dimension 2 Code   Code    20  
Yes 31  Customer Posting Group  Code    10  
Yes 32  Currency Code   Code    10  
Yes 33  Currency Factor Decimal     
Yes 34  Customer Price Group    Code    10  
Yes 35  Prices Including VAT    Boolean     
Yes 37  Invoice Disc. Code  Code    20  
Yes 40  Customer Disc. Group    Code    10  
Yes 41  Language Code   Code    10  
Yes 43  Salesperson Code    Code    10  
Yes 44  Order No.   Code    20  
Yes 46  Comment Boolean     
Yes 47  No. Printed Integer     
Yes 51  On Hold Code    3   
Yes 52  Applies-to Doc. Type    Option      
Yes 53  Applies-to Doc. No. Code    20  
Yes 55  Bal. Account No.    Code    20  
Yes 60  Amount  Decimal     
Yes 61  Amount Including VAT    Decimal     
Yes 70  VAT Registration No.    Text    20  
Yes 73  Reason Code Code    10  
Yes 74  Gen. Bus. Posting Group Code    10  
Yes 75  EU 3-Party Trade    Boolean     
Yes 76  Transaction Type    Code    10  
Yes 77  Transport Method    Code    10  
Yes 78  VAT Country/Region Code Code    10  
Yes 79  Sell-to Customer Name   Text    50  
Yes 80  Sell-to Customer Name 2 Text    50  
Yes 81  Sell-to Address Text    50  
Yes 82  Sell-to Address 2   Text    50  
Yes 83  Sell-to City    Text    30  
Yes 84  Sell-to Contact Text    50  
Yes 85  Bill-to Post Code   Code    20  
Yes 86  Bill-to County  Text    30  
Yes 87  Bill-to Country/Region Code Code    10  
Yes 88  Sell-to Post Code   Code    20  
Yes 89  Sell-to County  Text    30  
Yes 90  Sell-to Country/Region Code Code    10  
Yes 91  Ship-to Post Code   Code    20  
Yes 92  Ship-to County  Text    30  
Yes 93  Ship-to Country/Region Code Code    10  
Yes 94  Bal. Account Type   Option      
Yes 97  Exit Point  Code    10  
Yes 98  Correction  Boolean     
Yes 99  Document Date   Date        
Yes 100 External Document No.   Code    20  
Yes 101 Area    Code    10  
Yes 102 Transaction Specification   Code    10  
Yes 104 Payment Method Code Code    10  
Yes 105 Shipping Agent Code Code    10  
Yes 106 Package Tracking No.    Text    30  
Yes 107 Pre-Assigned No. Series Code    10  
Yes 108 No. Series  Code    10  
Yes 110 Order No. Series    Code    10  
Yes 111 Pre-Assigned No.    Code    20  
Yes 112 User ID Code    20  
Yes 113 Source Code Code    10  
Yes 114 Tax Area Code   Code    20  
Yes 115 Tax Liable  Boolean     
Yes 116 VAT Bus. Posting Group  Code    10  
Yes 119 VAT Base Discount % Decimal     
Yes 131 Prepayment No. Series   Code    10  
Yes 136 Prepayment Invoice  Boolean     
Yes 137 Prepayment Order No.    Code    20  
Yes 151 Quote No.   Code    20  
Yes 5050    Campaign No.    Code    20  
Yes 5052    Sell-to Contact No. Code    20  
Yes 5053    Bill-to Contact No. Code    20  
Yes 5700    Responsibility Center   Code    10  
Yes 5900    Service Mgt. Document   Boolean     
Yes 7001    Allow Line Disc.    Boolean     
Yes 7200    Get Shipment Used   Boolean     
Yes 50000   Counter Order   Boolean     COUNTER
Yes 50002   Job Receipt No. Text    30  COUNTER
Yes 50004   Counter Pay Card 1  Decimal     COUNTER
Yes 50005   Counter Pay Card 2  Decimal     COUNTER
Yes 50006   Counter Pay Cash    Decimal     COUNTER
Yes 50010   Counter E-Mail  Text    80  COUNTER
Yes 50016   Ship-to Mobile No. Contact  Text    30  COUNTER
Yes 50017   Shipment Yes/No Boolean     COUNTER
Yes 50018   Shipment Type   Option      COUNTER
Yes 50019   Sell-to Contact Name    Text    50  GAC WSU 02-03-2011, Issue 74/Vergroot 17-11-11 Kees VPL
Yes 50099   Invoice Type Code VPL   Code    10  VPL
Yes 11000000    Transaction Mode    Code    20  
Yes 11000001    Bank Account    Code    10  
Yes 11025025    Allow Condition Disc.   Boolean     GS10
Yes 11025026    Allow Prod. Gr. Cond. Disc. Boolean     GS10
Yes 11025027    Customer Price List Group   Code    10  GS10
Yes 11025028    Customer Condition Group    Code    10  GS10
Yes 11025029    Cust. Prod. Gr. Condition Gr.   Code    10  GS10
Yes 11025036    Find Best Price Boolean     GS10
Yes 11025041    Print Conditions    Boolean     GS10
Yes 11025051    Representative Code Code    10  GS10
Yes 11025073    Customer Bonus Group    Code    10  GS11
Yes 11025100    Transport Region    Code    10  GS6
Yes 11025210    Shipping Service Code   Code    20  GS01
Yes 11025211    Print Shipping Charge   Option      GS01
Yes 11025330    Intercompany Prepayment Boolean     GS18
Yes 11025332    Intercompany Shipment Allowed   Boolean     GS18
Yes 11025350    Rental Status   Code    10  GS20
Yes 11025351    Rental Start Date   Date        GS20
Yes 11025352    Rental End Date Date        GS20
Yes 11028120    Invoice Type Code   Code    10  N120F000
Yes 11028121    Comb. Ship. Header Text Code    Code    10  N120F000
Yes 99008509    Date Sent   Date        
Yes 99008510    Time Sent   Time        
Yes 99008516    BizTalk Sales Invoice   Boolean     
Yes 99008519    Customer Order No.  Code    20  
Yes 99008521    BizTalk Document Sent   Boolean     

这是G_L Entry表:

Enabled Field No.   Field Name  Data Type   Length  Description
Yes 1   Entry No.   Integer     
Yes 3   G/L Account No. Code    20  
Yes 4   Posting Date    Date        
Yes 5   Document Type   Option      
Yes 6   Document No.    Code    20  
Yes 7   Description Text    50  
Yes 10  Bal. Account No.    Code    20  
Yes 17  Amount  Decimal     
Yes 23  Global Dimension 1 Code Code    20  
Yes 24  Global Dimension 2 Code Code    20  
Yes 27  User ID Code    20  
Yes 28  Source Code Code    10  
Yes 29  System-Created Entry    Boolean     
Yes 30  Prior-Year Entry    Boolean     
Yes 41  Job No. Code    20  
Yes 42  Quantity    Decimal     
Yes 43  VAT Amount  Decimal     
Yes 45  Business Unit Code  Code    10  
Yes 46  Journal Batch Name  Code    10  
Yes 47  Reason Code Code    10  
Yes 48  Gen. Posting Type   Option      
Yes 49  Gen. Bus. Posting Group Code    10  
Yes 50  Gen. Prod. Posting Group    Code    10  
Yes 51  Bal. Account Type   Option      
Yes 52  Transaction No. Integer     
Yes 53  Debit Amount    Decimal     
Yes 54  Credit Amount   Decimal     
Yes 55  Document Date   Date        
Yes 56  External Document No.   Code    20  
Yes 57  Source Type Option      
Yes 58  Source No.  Code    20  
Yes 59  No. Series  Code    10  
Yes 60  Tax Area Code   Code    20  
Yes 61  Tax Liable  Boolean     
Yes 62  Tax Group Code  Code    10  
Yes 63  Use Tax Boolean     
Yes 64  VAT Bus. Posting Group  Code    10  
Yes 65  VAT Prod. Posting Group Code    10  
Yes 68  Additional-Currency Amount  Decimal     
Yes 69  Add.-Currency Debit Amount  Decimal     
Yes 70  Add.-Currency Credit Amount Decimal     
Yes 71  Close Income Statement Dim. ID  Integer     
Yes 72  IC Partner Code Code    20  
Yes 73  Reversed    Boolean     
Yes 74  Reversed by Entry No.   Integer     
Yes 75  Reversed Entry No.  Integer     
Yes 76  G/L Account Name    Text    50  
Yes 5400    Prod. Order No. Code    20  
Yes 5600    FA Entry Type   Option      
Yes 5601    FA Entry No.    Integer     
Yes 11301   Open    Boolean     
Yes 11302   Remaining Amount    Decimal     
Yes 11303   Closed by Entry No. Integer     
Yes 11304   Closed at Date  Date        
Yes 11305   Closed by Amount    Decimal     
Yes 11306   Applies-to ID   Code    20  
Yes 11025375    Calculation Model   Code    10  GS21
Yes 11025376    Order Type  Option      GS21
Yes 11025377    Order No.   Code    20  GS21
Yes 11025378    Order Line No.  Integer     GS21

如果我这样做:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],A.[G_L Account No_], A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
JOIN
[Verploegen POC$G_L Entry] A 
--LEFT JOIN [Verploegen POC$Sales Header] SH
ON SIH.No_  = A.[Document No_]
-- JOIN
--[Verploegen POC$G_L Account] GLA
--ON A.[G_L Account No_] = GLA.No_
--ON SIH.No_  = A.No_
WHERE --A.[Document No_] = '2295848' AND
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' AND  
A.[Posting Date]  BETWEEN '2016-05-30' AND  '2016-06-1' AND  
A.[G_L Account No_] IN ('1031', '1011','1021', '1041', '1051' ) 
AND
-- (A.[G_L Account No_] = '1031' OR a.[G_L Account No_] = '1011' 
--OR a.[G_L Account No_] = '1021' OR A.[G_L Account No_] = '1041' OR A.[G_L Account No_] = '1051')
--AND
A.[G_L Account No_] NOT IN('2210','2220', '2230','2240', '2250')

这是输出:

6.80000000000000000000  0.00000000000000000000  1011    2273308 2016-05-30 00:00:00.000
34.79000000000000000000 0.00000000000000000000  1011    2273496 2016-05-30 00:00:00.000
149.87000000000000000000    0.00000000000000000000  1011    2273441 2016-05-30 00:00:00.000
179.56000000000000000000    0.00000000000000000000  1011    2273379 2016-05-30 00:00:00.000
0.94000000000000000000  0.00000000000000000000  1011    2273536 2016-05-30 00:00:00.000
97.32000000000000000000 0.00000000000000000000  1011    2274011 2016-05-31 00:00:00.000
108.68000000000000000000    0.00000000000000000000  1011    2274058 2016-05-31 00:00:00.000
0.01000000000000000000  0.00000000000000000000  1011    2274193 2016-06-01 00:00:00.000
0.02000000000000000000  0.00000000000000000000  1011    2274287 2016-06-01 00:00:00.000
19.36000000000000000000 0.00000000000000000000  1011    2274295 2016-06-01 00:00:00.000
300.00000000000000000000    0.00000000000000000000  1021    2273476 2016-05-30 00:00:00.000
0.01000000000000000000  0.00000000000000000000  1021    2273891 2016-05-31 00:00:00.000
29.31000000000000000000 0.00000000000000000000  1031    2273153 2016-05-30 00:00:00.000
40.66000000000000000000 0.00000000000000000000  1031    2273511 2016-05-30 00:00:00.000
72.25000000000000000000 0.00000000000000000000  1031    2273752 2016-05-31 00:00:00.000
783.63000000000000000000    0.00000000000000000000  1041    2273191 2016-05-30 00:00:00.000
46.93000000000000000000 0.00000000000000000000  1041    2274260 2016-06-01 00:00:00.000
25.89000000000000000000 0.00000000000000000000  1041    2273741 2016-05-31 00:00:00.000
153.71000000000000000000    0.00000000000000000000  1041    2273868 2016-05-31 00:00:00.000
126.73000000000000000000    0.00000000000000000000  1041    2274186 2016-06-01 00:00:00.000
17.23000000000000000000 0.00000000000000000000  1051    2273152 2016-05-30 00:00:00.000
8.24000000000000000000  0.00000000000000000000  1051    2273537 2016-05-30 00:00:00.000
19.72000000000000000000 0.00000000000000000000  1051    2273730 2016-05-31 00:00:00.000
27.78000000000000000000 0.00000000000000000000  1051    2274199 2016-06-01 00:00:00.000
673.87000000000000000000    0.00000000000000000000  1051    2274266 2016-06-01 00:00:00.000
0.49000000000000000000  0.00000000000000000000  1051    2274509 2016-06-01 00:00:00.000

如果我这样做:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],cast([G_L Account No_] as varbinary(max)), A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
INNER JOIN
[Verploegen POC$G_L Entry] A 
ON SIH.No_  = A.[Document No_]
WHERE --A.[Document No_] = '2295848' AND
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' AND  
A.[Posting Date]  BETWEEN '2016-05-30' AND  '2016-06-1' AND  
A.[G_L Account No_] IN ('1031', '1011','1021', '1041', '1051' ) 
AND
A.[G_L Account No_] NOT IN('2210','2220', '2230','2240', '2250')

输出如下:

6.80000000000000000000  0.00000000000000000000  0x31303131  2273308 2016-05-30 00:00:00.000
179.56000000000000000000    0.00000000000000000000  0x31303131  2273379 2016-05-30 00:00:00.000
149.87000000000000000000    0.00000000000000000000  0x31303131  2273441 2016-05-30 00:00:00.000
34.79000000000000000000 0.00000000000000000000  0x31303131  2273496 2016-05-30 00:00:00.000
0.94000000000000000000  0.00000000000000000000  0x31303131  2273536 2016-05-30 00:00:00.000
97.32000000000000000000 0.00000000000000000000  0x31303131  2274011 2016-05-31 00:00:00.000
108.68000000000000000000    0.00000000000000000000  0x31303131  2274058 2016-05-31 00:00:00.000
0.01000000000000000000  0.00000000000000000000  0x31303131  2274193 2016-06-01 00:00:00.000
0.02000000000000000000  0.00000000000000000000  0x31303131  2274287 2016-06-01 00:00:00.000
19.36000000000000000000 0.00000000000000000000  0x31303131  2274295 2016-06-01 00:00:00.000
300.00000000000000000000    0.00000000000000000000  0x31303231  2273476 2016-05-30 00:00:00.000
0.01000000000000000000  0.00000000000000000000  0x31303231  2273891 2016-05-31 00:00:00.000
29.31000000000000000000 0.00000000000000000000  0x31303331  2273153 2016-05-30 00:00:00.000
40.66000000000000000000 0.00000000000000000000  0x31303331  2273511 2016-05-30 00:00:00.000
72.25000000000000000000 0.00000000000000000000  0x31303331  2273752 2016-05-31 00:00:00.000
783.63000000000000000000    0.00000000000000000000  0x31303431  2273191 2016-05-30 00:00:00.000
25.89000000000000000000 0.00000000000000000000  0x31303431  2273741 2016-05-31 00:00:00.000
153.71000000000000000000    0.00000000000000000000  0x31303431  2273868 2016-05-31 00:00:00.000
126.73000000000000000000    0.00000000000000000000  0x31303431  2274186 2016-06-01 00:00:00.000
46.93000000000000000000 0.00000000000000000000  0x31303431  2274260 2016-06-01 00:00:00.000
17.23000000000000000000 0.00000000000000000000  0x31303531  2273152 2016-05-30 00:00:00.000
8.24000000000000000000  0.00000000000000000000  0x31303531  2273537 2016-05-30 00:00:00.000
19.72000000000000000000 0.00000000000000000000  0x31303531  2273730 2016-05-31 00:00:00.000
27.78000000000000000000 0.00000000000000000000  0x31303531  2274199 2016-06-01 00:00:00.000
673.87000000000000000000    0.00000000000000000000  0x31303531  2274266 2016-06-01 00:00:00.000
0.49000000000000000000  0.00000000000000000000  0x31303531  2274509 2016-06-01 00:00:00.000

你的意思是这个查询:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],cast([G_L Account No_] as varbinary(max)), A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
INNER JOIN
[Verploegen POC$G_L Entry] A 
ON SIH.No_  = A.[Document No_]
WHERE --A.[Document No_] = '2295848' AND
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' AND  
A.[Posting Date]  BETWEEN '2016-05-30' AND  '2016-06-1' AND  
A.[G_L Account No_] IN ('1031', '1011','1021', '1041', '1051' ) 
AND
A.[G_L Account No_] NOT IN('2210','2220', '2230','2240', '2250')

2 个答案:

答案 0 :(得分:0)

您的WHERE条款有点问题,您是否可以使用NOT IN

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT  SIH.[Counter Pay Card 1], SIH.[Counter Pay Card 2],A.[G_L Account No_], A.[Document No_], CAST(CONVERT(VARCHAR, A.[Posting Date])AS DATETIME)
FROM  [Verploegen POC$Sales Invoice Header] SIH
LEFT JOIN
[Verploegen POC$G_L Entry] A 
ON SIH.No_  = A.[Document No_]
WHERE
SIH.[Counter Pay Card 1] <> '0.00000000000000000000' 
AND A.[Posting Date] BETWEEN '2016-05-30' AND  '2016-07-18' 
AND A.[G_L Account No_] NOT IN ('1031', '1011', '1021', '1041', '1051', '2210', '2220', '2230', '2240', '2250')

如果不看输入数据,很难知道这一点。此外,最好只包含与问题相关的代码。注释掉代码并不是必需的,并且会让一些人失望。

这种逻辑不起作用的原因;

A.[G_L Account No_] <> '2210' OR a.[G_L Account No_] <> '2220'

如果您的值为2210,那么它会使第一个子句失败,但因为您使用的是OR而不是AND,所以它会传递第二个条件(因为{ {1}})并返回该值。您可以将这些更改为2201 <> 2220以查看更改,但我肯定会使用AND

答案 1 :(得分:0)

由于数据或代码没有任何问题,唯一的结论是没有执行A.[G_L Account No_] NOT IN的行。
你可能没有错误地标记它......: - )