?
Solved

sql group by problems

Posted on 2011-09-29
8
Medium Priority
?
230 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

771 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