tags:

views:

69

answers:

3

I have this:

SELECT Product.ProductID, Product.Name, Product.GroupID, Product.GradeID,
       AVG(tblReview.Grade) AS Grade
FROM   Product left Join tblReview ON Product.GroupID = tblReview.GroupID
WHERE  (Product.CategoryID = @CategoryID)
GROUP  BY Product.ProductID, Product.Name, Product.GroupID, Product.GradeID

I would like to return only the rows where Product.Name is unique. If I make a SELECT DISTINCT the ProductID is different on every row so all the rows are unique.

A: 

You could do something like this (see below), but this assumes that you don't care if 2 products with the same name have a different GroupID, etc. because you can't really list those unless you use a different approach (multiple queries).

SELECT        Product.Name
            , max(Product.ProductID) as ProductID
            , max(Product.GroupID) as GroupID
            , max(Product.GradeID) as GradeID,
            , AVG(tblReview.Grade) AS Grade
FROM            Product left Join tblReview ON Product.GroupID = tblReview.GroupID

WHERE        (Product.CategoryID = @CategoryID)

GROUP BY Product.Name

HAVING COUNT(distinct Product.Name) = 1
dcp
It works! I got a couple of more tables then I wrote here, do you know how efficient this method is?
Nicklas
Glad to hear it worked :). As for efficiency, I'd make sure to have an index on Product.Name for starters. You can always look at the query plan also, if things are not performing as you like. You can use the Show Execution Plan option, here's an article for reference (http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx).
dcp
@dcp: you should add `HAVING COUNT(*) = 1` for this to work correctly.
Quassnoi
@Quassnoi - Good point, I misunderstood "unique product name" to mean the returned result should consist of unique product names. Thanks for pointing it out.
dcp
When I add HAVING COUNT(*) = 1 or HAVING COUNT(Product.Name) = 1, I get 0 rows returnd unfortunately.
Nicklas
Try it with my latest update "HAVING Count(distinct Product.Name) = 1"
dcp
Great! That works! Thanks alot!
Nicklas
Great! Please be sure to accept the answer (click the checkmark) if it solved your problem, thanks.
dcp
A: 
SELECT  p.ProductID, p.Name, p.GroupID, p.GradeID,
        (
        SELECT  AVG(grade)
        FROM    tblReview r
        WHERE   r.GroupID = p.GroupID
        ) AS Grade
FROM    (
        SELECT  *,
                ROW_NUMBER() OVER (PARTITION BY name ORDER BY productID) AS rna,
                ROW_NUMBER() OVER (PARTITION BY name ORDER BY productID DESC) AS rnd
        FROM    Product
        WHERE   CategoryID = @CategoryID
        ) p
WHERE   rna = rnd
Quassnoi
I dont get it to work I get complains on cant find grade, but it looks efficient!
Nicklas
@Nicklas: could you please post the exact error message?
Quassnoi
DataBinding: System.Data.DataRowView does not contain property Grade
Nicklas
@Nicklas: try now
Quassnoi
Now it filters nicely, but displays products from all Categorys.
Nicklas
@Nicklas: did you use the query from the last update (with `WHERE CategoryID = @CategoryID`)?
Quassnoi
Now the Categories are right, but some products arent displayed, I dont get an error messages but I can see that some products are missing. The Products got the same kind of information.
Nicklas
@Niclkas: which products are not displayed?
Quassnoi
I know it doesnt help you a lot, but it is two diffrent boards, just diffrent name. But one of them isnt displayed, really strange... I think I go for the other solution further down. Thanks alot for the time you spent on this.
Nicklas
A: 

Should this work for you?

SELECT Product.ProductID, Product.Name, Product.GroupID, Product.GradeID, 
       AVG(tblReview.Grade) AS Grade 
FROM   Product left Join tblReview ON Product.GroupID = tblReview.GroupID 
WHERE  (Product.CategoryID = @CategoryID) 
GROUP  BY Product.ProductID, Product.Name, Product.GroupID, Product.GradeID 
HAVING COUNT(Product.Name)=1
Daria Barteneva
Nice and simple, but it doesnt filter anything, all names are displayed
Nicklas
So it must be the ugly way:)SELECT Product.ProductID, Product.Name, Product.GroupID, Product.GradeID, AVG(tblReview.Grade) AS Grade FROM Product left Join tblReview ON Product.GroupID = tblReview.GroupID WHERE Product.CategoryID = @CategoryIDAND Product.Name in (SELECT P.Name FROM Product as P WHERE P.CategoryId = @CategoryID GROUP BY P.Name HAVING COUNT(*)=1)GROUP BY Product.ProductID, Product.Name, Product.GroupID, Product.GradeID
Daria Barteneva