Click here to Skip to main content
15,917,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Quote:
I have a table UserType as follows:
Quote:


HTML
ID	UserType
1	Admin
2	Marketing Manager
3	Hotel Manager
4	Playlist Programmer
5	Support Manager


Quote:
And another table User as follows:

HTML
ID  Name UserType LocationID
1   TT      1         1
2   AA	    2	      1
3   BB      1         2
4   CC      2         3


Quote:
I want to group by on single column UserType and not by all the column in the select list.
When I use the following query its giving error.How to do that?

SQL
Select		usr.ID,
			usr.FirstName,
			usr.LastName,
			usr.Email,
			usr.ContactNo,
			lctn.LocName ,
			IsActive,
			CreatedBy,
			utyp.UserType
			
	 from	[User] usr,
		    Location lctn,UserType utyp
     where usr.Location_id=lctn.LocID	
     and  usr.UserType_ID=utyp.ID	
     --group by  utyp.UserType
Posted
Comments
[no name] 29-May-13 10:40am    
"the following query its giving error", sorry I have used up all of my mind reading credits for the week, perhaps you can share which error, out the of the many errors that it could be, the one that you are getting.
CHill60 29-May-13 10:49am    
The simple answer is that you can't http://msdn.microsoft.com/en-us/library/ms177673.aspx[^]. Why are you trying to group when you are not retrieving anything such as count or sum. Use the Improve question link to tell us what you are actually trying to achieve
David_Wimbley 29-May-13 10:54am    
You can do a group by without having to do count or sum. Group By comes in hand with a join that returns multiple similar rows.
CHill60 29-May-13 11:01am    
True ... but we still need the OP to explain what he is actually trying to do

1 solution

Based on your question/sample query you are wanting to group by the single Column userType.

If you wish to group by one column your select query can only be for one column (or an aggregate function like COUNT or SUM).

So this

SQL
Select		usr.ID,
			usr.FirstName,
			usr.LastName,
			usr.Email,
			usr.ContactNo,
			lctn.LocName ,
			IsActive,
			CreatedBy,
			utyp.UserType
			
	 from	[User] usr,
		    Location lctn,UserType utyp
     where usr.Location_id=lctn.LocID	
     and  usr.UserType_ID=utyp.ID	
     --group by  utyp.UserType


Would need to be changed to this

SQL
Select utyp.UserType
from [User] usr,
     Location lctn,UserType utyp
     where usr.Location_id=lctn.LocID	
     and  usr.UserType_ID=utyp.ID	
     group by  utyp.UserType



This will allow you to group by the single column based on how i understand your question. For every column you wish to group by you must add it to both your group by and your select statement.

So if you have 2 columns in your group by, those 2 columns should be in your select statement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900