How To Compare Two Columns in Same Table?

I have two tables with this structure:

First table: Document

Doc_Num Doc_Type Doc_Date Doc_Valide
BLA241913 BLA 01/07/2024 1

Second table: DocumentD

Doc_Num DocDOrigine
BLA241913 CMA241682

I try many queries with self join, inner join, left join but did not get desired result.

My query:

SELECT *
FROM Documentd 
INNER JOIN [Document] ON DocumentD.Doc_Num = [Document].Doc_Num 
WHERE ([Document].Doc_Type IN (N'bla', N'faca'))
  AND (DocumentD.DocD_QReliquat >= 0) 
  AND ([Document].Doc_Date BETWEEN '2024-07-01' AND '2024-07-31')

This query return as result:

Doc_Num Doc_Type Doc_Date DocDOrigine Doc_Valide
BLA241913 BLA 01/07/2024 CMA241682 1
BLA241913 BLA 01/07/2024 CMA241682 1
BLA241913 BLA 01/07/2024 CMA241682 1
BLA241914 BLA 01/07/2024 CMA241683 1
BLA241914 BLA 01/07/2024 CMA241683 1
BLA241915 BLA 01/07/2024 CMA241684 1
BLA241916 BLA 01/07/2024 CMA241685 1
BLA241917 BLA 01/07/2024 CMA241686 0
ACHAT240730008 FACA 01/07/2024 BLA241913 2
ACHAT240730008 FACA 01/07/2024 BLA241913 2
ACHAT240730008 FACA 01/07/2024 BLA241913 0
ACHAT240730010 FACA 01/07/2024 BLA241914 0
ACHAT240730010 FACA 01/07/2024 BLA241914 1
ACHAT240731025 FACA 01/07/2024 null 1

I want to show all [Document].Doc_num with [Document].Doc_Type = ‘BLA’ and [Document].Doc_Valide = 1 , with condition this Documents not exist in DocDOrigine with [Document].Doc_Type = ‘FACA’ like this:

Doc_Num Doc_Type Doc_Date DocDOrigine Doc_Valide
BLA241915 BLA 01/07/2024 CMA241684 1
BLA241916 BLA 01/07/2024 CMA241685 1

I wish I had been clear in posing my question, and I am here to clarify thank you.

Link : https://sqlfiddle.com/sql-server/online-compiler?id=e31a94d2-3778-4861-870f-7b0c6730a7d7

7

I think I understand the question. I’m not sure if this is the right answer, but it returns the records you specified.

select *
from Document
where 
  Document.Doc_Num not in (select DocDOrigine from DocumentD) and
  Doc_Type = 'BLA' and
  Doc_Valide = 1;

Basically, the first WHERE clause is what you were missing. This is one of the easiest ways to write that, but it could easily have performance issues or need its own filtering to match better with the Document table.

Here’s another way it can be done, using an outer join instead of a sub-query:

select Document.* 
from Document
  left outer join DocumentD on
    Document.Doc_Num = DocumentD.DocDOrigine
where
  DocumentD.DocDOrigine is null and
  Document.Doc_Type = 'BLA' and
  Document.Doc_Valide = 1;

Edit:

I missed the DocDOrigine column from the example result set. I had to add some missing data from your question to the SQL fiddle to get it to return the expected result set. The SQL Fiddle link is:
https://sqlfiddle.com/sql-server/online-compiler?id=8af184de-67b4-49cf-a4fe-50148dc9b4eb

The queries to return that are:

select
  Document.Doc_Num,
  Document.Doc_Type,
  Document.Doc_Date,
  DocumentD.DocDOrigine,
  Document.Doc_Valide
from Document
  inner join DocumentD on
    Document.Doc_Num = DocumentD.Doc_Num
where 
  Document.Doc_Num not in (select DocDOrigine from DocumentD) and
  Doc_Type = 'BLA' amd
  Doc_Valide = 1;

And, the outer join variant:

select
  Document.Doc_Num,
  Document.Doc_Type,
  Document.Doc_Date,
  PrimDocument.DocDOrigine,
  Document.Doc_Valide
from Document
  left outer join DocumentD as OrigDocument on
    Document.Doc_Num = OrigDocument.DocDOrigine
  inner join DocumentD as PrimDocument on
    Document.Doc_Num = PrimDocument.Doc_Num
WHERE
  OrigDocument.DocDOrigine is null and
  Document.Doc_Type = 'BLA' and
  Document.Doc_Valide = 1;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật