[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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
0
HawaiiDragon
Asked:
HawaiiDragon
  • 6
  • 2
1 Solution
 
knightEknightCommented:
group by FirstName + ' ' + LastName
0
 
knightEknightCommented:
also, ORDER BY goes after GROUP BY
0
 
knightEknightCommented:
-- both changes:

select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
group by  FirstName + ' ' + LastName
order by LastName
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
knightEknightCommented:
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

0
 
HawaiiDragonAuthor Commented:
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.
0
 
knightEknightCommented:
yes, you will either need to group by ParkID also, or use DISTINCT.

Also, you will need to ORDER BY one of the selected columns.  You can use the alias in ORDER BY, but not in GROUP BY, so:

select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
group by ParkID, FirstName + ' ' + LastName
order by truename
0
 
knightEknightCommented:
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?
0
 
HawaiiDragonAuthor Commented:
select ParkID, LastName + ', ' + FirstName as truename
 from tbl_AdminUsers where active = 'Yes'
group by ParkID, LastName + ', ' + FirstName
order by truename

Thank you very much!!!!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now