如何在SQL Server中查找和删除重复项

时间:2018-11-20 04:59:29

标签: sql sql-server tsql

Described about my issue in IMAGE

我正在使用以下联接来获取某些数据。我得到的是6行而不是3行。有人可以帮助我吗?

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)  

我需要帮助以了解如何停止获取重复的记录。

1 个答案:

答案 0 :(得分:1)

您能尝试一下吗?

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)  

DeliveryPlace必须使用codCustomerlblDeliveryplace进行联接,否则您将获得更多需要的行。

如果可以规范化数据并将地址移动到单独的表中,会更好。然后,您将能够使用integer列联接数据。