2
votes

Requête SQL pour rechercher les valeurs manquantes dans les tables

Tableau 1

Product  Product Description Order ID
ABC      Hardware            1234
ABC      Hardware            2345
ABC      Hardware            5678
BCQ      Component           7896
BCQ      Component           9681

Tableau 2

Product    product Description
ABC        Hardware
CBD        Software
BCQ        Component
DEF        License

Voici le contexte le tableau 1 contient les données produit, le tableau 2 contient les données produit avec les identifiants de commande Je veux savoir combien de produits ne sont pas commandés (dans ce scénario, "CBD Software" et "DEF License" n'ont pas de commandes passées et sont donc manquants dans le tableau 2). maintenant je veux savoir combien de produits n'ont jamais été placés sur une commande.


1 commentaires

Je vous demande de bien vouloir accepter la réponse car elle me motive à répondre à plus de questions.


4 Réponses :


2
votes

Voulez-vous simplement que n'existe pas ?

select p.*
from products p
where not exists (select 1 from orders o where o.product = p.product);


2 commentaires

Vous avez oublié l'alias commandes o


@fdkgfosfskjdlsjdlkfsf. . . Je vous remercie.



2
votes

Vous pouvez essayer la requête ci-dessous

ProdCd  ProdDes
---------------
CBD     Software
DEF     License

Voici l'exemple réel

Create table ProductDescription (ProdCd varchar(10), ProdDes Varchar(20))
insert into ProductDescription values ('ABC', 'Hardware'),
('CBD',     'Software'),
('BCQ',     'Component'),
('DEF',     'License')

Create table ProductOrders (ProdCd varchar(10), ProdDes Varchar(20), OrderId int)
insert into ProductOrders values ('ABC',      'Hardware',      1234),
('ABC',      'Hardware',      2345),
('ABC',      'Hardware',      5678),
('BCQ',      'Component',     7896),
('BCQ',      'Component',     9681)

SELECT * FROM ProductDescription
where ProdCd not in (SELECT ProdCd FROM ProductOrders)

Le résultat est comme indiqué ci-dessous

SELECT * FROM Table1
where ProductId not in (SELECT ProductId FROM ProductDescription)

Vous pouvez trouver la démo en direct ici Démo en direct


0 commentaires

2
votes

Vous pouvez également utiliser LEFT JOIN et IS NULL dans condition

SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2 ON t2.product = t1.product
WHERE t2.product IS NULL


0 commentaires

1
votes

Voici encore une autre façon, avec RIGHT JOIN et IS NULL :

declare @prod table
(
    Product varchar(100),
    [product Description] varchar(100)
)

insert into @prod
select 'ABC','Hardware' union
select 'CBD','Software' union
select 'BCQ','Component' union
select 'DEF','License'


declare @prodOrders table
(
    Product varchar(100),
    [product Description] varchar(100),
    [orderid] int
)
insert into @prodOrders
select 'ABC','Hardware',1234  union
select 'ABC','Hardware',2345  union
select 'ABC','Hardware',5678  union
select 'BCQ','Component',7896 union
select 'BCQ','Component',9681

SELECT t1.*
FROM @prodOrders t2 
right JOIN @prod t1 ON t2.product = t1.product
WHERE t2.product IS NULL

Démo en direct


0 commentaires