Solved

sql group by problems

Posted on 2011-09-29
8
221 Views
Last Modified: 2012-05-12
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
Comment
Question by:HawaiiDragon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 36816810
group by FirstName + ' ' + LastName
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36816816
also, ORDER BY goes after GROUP BY
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36816821
-- both changes:

select ParkID, FirstName + ' ' + LastName as truename
 from tbl_AdminUsers
group by  FirstName + ' ' + LastName
order by LastName
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 33

Expert Comment

by:knightEknight
ID: 36816833
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
 

Author Comment

by:HawaiiDragon
ID: 36816862
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
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 36816871
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 36816879
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
 

Author Closing Comment

by:HawaiiDragon
ID: 36816897
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 69
MS SQL / SQL Server Native Client -- how to prevent seeing other servers? 2 54
Negative isnull? 3 25
Please explain Equi-join 3 15
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question