views:

34

answers:

2

Hi I have written this query in sql server 2005 but it still will show the name of products that are the same!!thanks

SELECT  DISTINCT ProductName 
FROM Products,Suppliers 
WHERE Products.SupplierID = Suppliers.SupplierID AND Fax IS NULL

for example I have two product names that are the same 'Chei' instead of returning one 'chei' it will return both of them

+1  A: 

What you have posted should work fine.

Is it possible you're selecting other stuff like this?

SELECT  DISTINCT ProductName, ProductId 
FROM Products,Suppliers 
WHERE Products.SupplierID = Suppliers.SupplierID AND Fax IS NULL 

Alternatively, you should be writing it like this (but this won't help you if you are selecting multiple columns).

SELECT  DISTINCT ProductName 
FROM Products 
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID 
WHERE Fax IS NULL 

If you are confused as to why selecting multiple columns wouldn't work:

SELECT  DISTINCT ProductName, ProductId
FROM Products 
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID 
WHERE Fax IS NULL 

Just think about it a moment as follows:

Products table:
ID  Name
1   Test
2   Test
3   Other
4   Random

You want:

Mixed:
ID  Name
4   Random
3   Other
?   Test

How should it pick that 'id' for Test in the Distinct mixed table? As such, it gives you all DISTINCT combinations of all the data you've asked for.

i.e.

Mixed:
ID  Name
4   Random
3   Other
2   Test
1   Test
Graphain
productID is unique!
in the product table some times I have 3 different product names with one supplierID but each product name has its own productID
and some times from those 3 product names ,two of them are as the same as each other
yes you are right but why inner join will answer this????
DISTINCT isn't per field it's per row. So "DISTINCT(productName) productid" is all DISTINCT combinations of both (so if the productid is different you'll get two rows), not just DISTINCT productNames
Graphain
Answer updated to clarify things.
Graphain
@lc thanks for making my SQL look nicely formatted :-)
Graphain
thanks a lot :)
+1  A: 
  • If you use EXISTS, you shouldn't need DISTINCT
  • ANSI PADDING means you may have trailing spaces: but why is ProductName repeated?
  • Is your post the actual query?
  • Use correct JOIN syntax

Options:

SELECT --DISTINCT if you need RTRIM
    P.ProductName  --RTRIM(ProductName, '')
FROM
    Products P
WHERE
    EXISTS (SELECT *
        FROM
           Suppliers S
        WHERE
           P.SupplierID = S.SupplierID
           AND
           Fax IS NULL -- belongs to Suppliers?
             )
gbn
This is an interesting way of doing things but would it optimize as well as an inner join? It's no matter really as I'm pretty sure the OP is trying to select multiple columns based on one DISTINCT, but I'm still curious.
Graphain
@Graphain: it would optimise better usually because you don't need a DISTINCT. http://stackoverflow.com/questions/2019958/joins-exists-or-in-which-is-better-few-questions-on-sql/2081909#2081909 http://stackoverflow.com/questions/1001543/in-vs-join-with-large-rowsets/1001578#1001578 http://stackoverflow.com/questions/991934/rewrite-t-sql-from-using-in-to-using-join-where/992338#992338
gbn
Nice - thanks for the clarification
Graphain