sql - Why cant I use GROUP BY in this Query -
when executed following query in ms sql management studio produced error.
query:
select u.username, r.rolename user u join userrole ur on ur.userid = u.userid join role r on r.roleid = ur.roleid group r.rolename
error:
msg 8120, level 16, state 1, line 2 column 'user.username' invalid in select list because not contained in either aggregate function or group clause.
its been while had used sql , supposed basic command. still cant figure out reason.
note:
there no error if use
group r.rolename, u.username
this demonstrates single-value rule
, prohibits undefined results when run group by
query, , include columns in select-list neither part of grouping criteria, nor appear in aggregate functions (sum, min, max, etc.)
. hope u understand.
i dont know may want this..
select distinct u.username, r.rolename user u join userrole ur on ur.userid = u.userid join role r on r.roleid = ur.roleid
Comments
Post a Comment