1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Naman If the answer is working and as expected please click tick mark near answer and make it green

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.