Wednesday, March 7, 2012

dont repeat the same fruilt a user ate...

This may get brought up again and again, but I don't know how to deal with it by heart yet... in fact I mean to say, I don't know how to deal with it.

With the given data set how do you only return one of the 2 rows where a given user ate the same fruit?

apples greg
peach greg
lemon greg
apples greg

peach bruce
peach bruce
lemon bruce
apples bruce

pear paul
peach paul
plumb paul
plumb paul

apples barb
peach barb
lemon barb
pear barb

What T-SQL is needed to produce the following dataset?

apples greg
peach greg
lemon greg

peach bruce
lemon bruce
apples bruce

pear paul
peach paul
plumb paul

apples barb
peach barb
lemon barb
pear barb

Won't a simple DISTINCT query give you that?|||

SELECT DISTINCT fruit,user

FROM ...

ORDER BY user,fruit

No comments:

Post a Comment