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
Post a Comment