Naman
Naman

Reputation: 37

How to find and remove duplicates in SQL Server

Described about my issue in IMAGE

I am using below joins to get certain data. I am getting 6 rows instead of 3. Can anyone help me out?

ALTER PROCEDURE [dbo].[psGetClientDropdownResult]   
    @lblCustomer VARCHAR(40),
    @lblDeliveryplace VARCHAR(41),
    @lblPackedProduct VARCHAR(41)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT DISTINCT             
        Customer.lblCustomer As ClientDestinataire,
        DeliveryPlace.lblDeliveryplace As LieuDeLivraison,
        DeliveryPlace.codCountry AS Pays,
        CustomerProduct.codCustomerProduct As CodeProduitClient,
        CustomerProduct.codCustomerProductColor AS CodeCouleurClient,
        CustomerProduct.codAqp AS AQP,
        DeliveryPlace.codEMSupplier AS CodeFournisseurEMPourClient,
        CustomerProduct.datApplication AS DateDApplication,
        CustomerProduct.codPackedProduct AS Material, 
        Product.lblPProduct AS  Produit, 
        Product.lblProduct AS RefFournisseur, 
        PackedProduct.lblPackedProduct AS MaterialDescription,
        Packaging.nbrPackagingNetWeight AS PoidsNet,
        (Packaging.nbrPackagingNetWeight) + (Packaging.nbrAddWeightFromNetToGross) AS PoidsBrut,
        Customer.codCustomer as CodClient,
        PackedProduct.codPackedProduct as Material,
        Packaging.codPackaging as CodPackaging,
        Packaging.nbrWeightCoefFromNetToGross as CoefNetBrut    
    FROM   
        Customer 
    INNER JOIN
        CustomerProduct ON Customer.codCustomer = CustomerProduct.codCustomer  
    INNER JOIN
        DeliveryPlace ON Customer.codCustomer = DeliveryPlace.codCustomer 
    INNER JOIN
        PackedProduct ON CustomerProduct.codPackedProduct = PackedProduct.codPackedProduct 
    INNER JOIN
        Product ON PackedProduct.codProduct = Product.codProduct 
    INNER JOIN 
        Packaging on Packaging.codPackaging = PackedProduct.codPackaging
    WHERE  
        (Customer.lblCustomer = @lblCustomer  or @lblCustomer IS NULL )
        AND (DeliveryPlace.lblDeliveryplace = @lblDeliveryplace OR @lblDeliveryplace IS NULL)
        AND (PackedProduct.lblPackedProduct = @lblPackedProduct OR @lblPackedProduct IS NULL)  

I need help to understand how can i stop getting my records duplicates.

Upvotes: 1

Views: 70

Answers (1)

gotqn
gotqn

Reputation: 43636

Could you try this:

ALTER PROCEDURE [dbo].[psGetClientDropdownResult]   
    @lblCustomer VARCHAR(40),
    @lblDeliveryplace VARCHAR(41),
    @lblPackedProduct VARCHAR(41)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT DISTINCT             
        Customer.lblCustomer As ClientDestinataire,
        DeliveryPlace.lblDeliveryplace As LieuDeLivraison,
        DeliveryPlace.codCountry AS Pays,
        CustomerProduct.codCustomerProduct As CodeProduitClient,
        CustomerProduct.codCustomerProductColor AS CodeCouleurClient,
        CustomerProduct.codAqp AS AQP,
        DeliveryPlace.codEMSupplier AS CodeFournisseurEMPourClient,
        CustomerProduct.datApplication AS DateDApplication,
        CustomerProduct.codPackedProduct AS Material, 
        Product.lblPProduct AS  Produit, 
        Product.lblProduct AS RefFournisseur, 
        PackedProduct.lblPackedProduct AS MaterialDescription,
        Packaging.nbrPackagingNetWeight AS PoidsNet,
        (Packaging.nbrPackagingNetWeight) + (Packaging.nbrAddWeightFromNetToGross) AS PoidsBrut,
        Customer.codCustomer as CodClient,
        PackedProduct.codPackedProduct as Material,
        Packaging.codPackaging as CodPackaging,
        Packaging.nbrWeightCoefFromNetToGross as CoefNetBrut    
    FROM   
        Customer 
    INNER JOIN
        CustomerProduct ON Customer.codCustomer = CustomerProduct.codCustomer  
    INNER JOIN
        DeliveryPlace 
            ON Customer.codCustomer = DeliveryPlace.codCustomer 
            AND CustomerProduct.lblDeliveryplace = DeliveryPlace.lblDeliveryplace 
    INNER JOIN
        PackedProduct ON CustomerProduct.codPackedProduct = PackedProduct.codPackedProduct 
    INNER JOIN
        Product ON PackedProduct.codProduct = Product.codProduct 
    INNER JOIN 
        Packaging on Packaging.codPackaging = PackedProduct.codPackaging
    WHERE  
        (Customer.lblCustomer = @lblCustomer  or @lblCustomer IS NULL )
        AND (DeliveryPlace.lblDeliveryplace = @lblDeliveryplace OR @lblDeliveryplace IS NULL)
        AND (PackedProduct.lblPackedProduct = @lblPackedProduct OR @lblPackedProduct IS NULL)  

The DeliveryPlace must be join using codCustomer and lblDeliveryplace or you will get more rows then needed.

It will be better if you can normalized your data and move the address in separate table. Then you will be able to join the data using integer column for example.

Upvotes: 1

Related Questions