sql server - Problem with unique SQL query -


i want select records, have query return single record per product name. table looks similar to:

sellid   productname comment 1          cake        dasd  2          cake        dasdasd 3          bread       dasdasdd   

where product name not unique. want query return single record per productname results like:

sellid   productname comment 1          cake        dasd  3          bread       dasdasdd  

i have tried query,

select distict productname,comment ,sellid tbl#sells 

but returning multiple records same productname. table not realy simple this, sample. solution? clear?

select  productname,  min(comment) , min(sellid) tbl#sells group productname 

if y ou want 1 record per productname, ofcourse have choose value want other fields.

if aggregate (using group by) can choose aggregate function, htat's function takes list of values , return 1 : here have chosen min : smallest walue each field.

note : comment , sellid can come different records, since min taken...

othter aggregates might find useful :

first : first record encountered last : last record encoutered avg : average  count : number of records 

first/last have advantage fields same record.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -