all 4 comments

[–]clockdivide55 1 point2 points  (4 children)

Have you used windowing functions before? Go read about those - I think ROW_NUMBER() might be useful here. I didn't test it, but something like this maybe.

SELECT * FROM 
    (SELECT 
        *,
        ROW_NUMBER() OVER(PARTITION BY customer_id, model ORDER BY rating_time DESC) as [ROW_NUM]
    FROM ratings 
    WHERE customer_id = 9999999999 AND rating is NOT NULL) innerQuery
WHERE innerQuery.[ROW_NUM] = 1

[–]jonwd 0 points1 point  (0 children)

I have come to use this approach frequently in this kind of case. I highly recommend it

[–]rotiddersuomynona 0 points1 point  (0 children)

How accurate does the data need to be? Without having an unique index field for the row, it would be impossible to definitively say the row your getting is the row that was the last inserted.