Link to home
Start Free TrialLog in
Avatar of HawaiiDragon
HawaiiDragon

asked on

sql group by problems

having group by problems

my code:
select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
order by LastName
group by truename;

my error:
Incorrect syntax near the keyword 'group'.

HELP please
Avatar of knightEknight
knightEknight
Flag of United States of America image

group by FirstName + ' ' + LastName
also, ORDER BY goes after GROUP BY
-- both changes:

select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
group by  FirstName + ' ' + LastName
order by LastName
but what is the purpose of the group by in this instance?  is it just to provide distinct records?  if so, you will either need to group by both columns, or just use distinct:

select distinct ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
-- group by  FirstName + ' ' + LastName
order by LastName

Avatar of HawaiiDragon
HawaiiDragon

ASKER

select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
group by  FirstName + ' ' + LastName
order by LastName

Column 'tbl_AdminUsers.ParkID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
again, I don't understand why you would need group by here at all, other than to get distinct records.  What do you need this query to show?
select ParkID, LastName + ', ' + FirstName as truename
 from tbl_AdminUsers where active = 'Yes'
group by ParkID, LastName + ', ' + FirstName
order by truename

Thank you very much!!!!