Because grouping happens before the "division" alia is assigned to the column as the dataset is finally generated. So, you have to: Select... .... Group By divisioncode_i,jobtitle,department [quoted text, click to view] "Cindy Mikeworth" <cindy@tas-in.com> wrote in message news:DAF5AEF9-A696-49DD-AB31-7934FC4A8209@microsoft.com... > What's wrong with this statement? I'm getting an invalid column name on > Division and Department. I'm trying to get a count of all unique > combinations of the three columns. > > select count(employid), left(divisioncode_i,1) as division, jobtitle, > left(deprtmnt,2) as department > from upr00100 > group by division, jobtitle, department
[quoted text, click to view] On Oct 1, 6:53 pm, "Cindy Mikeworth" <ci...@tas-in.com> wrote: > What's wrong with this statement? I'm getting an invalid column name on > Division and Department. I'm trying to get a count of all unique > combinations of the three columns. > > select count(employid), left(divisioncode_i,1) as division, jobtitle, > left(deprtmnt,2) as department > from upr00100 > group by division, jobtitle, department
use this: select count(employid), left(divisioncode_i,1) as division, jobtitle, left(deprtmnt,2) as department from upr00100 group by left(divisioncode_i,1), jobtitle, left(deprtmnt,2)
Cindy, SQL Server doesn't permit aliases in the order by or group by clauses. Try changing your group by clause to: group by left(divisioncode_i,1) , jobtitle, left(deprtmnt,2) -- Bill [quoted text, click to view] "Cindy Mikeworth" <cindy@tas-in.com> wrote in message news:DAF5AEF9-A696-49DD-AB31-7934FC4A8209@microsoft.com... > What's wrong with this statement? I'm getting an invalid column name on > Division and Department. I'm trying to get a count of all unique > combinations of the three columns. > > select count(employid), left(divisioncode_i,1) as division, jobtitle, > left(deprtmnt,2) as department > from upr00100 > group by division, jobtitle, department
What's wrong with this statement? I'm getting an invalid column name on Division and Department. I'm trying to get a count of all unique combinations of the three columns. select count(employid), left(divisioncode_i,1) as division, jobtitle, left(deprtmnt,2) as department from upr00100 group by division, jobtitle, department
[quoted text, click to view] "Cindy Mikeworth" <cindy@tas-in.com> wrote in message news:DAF5AEF9-A696-49DD-AB31-7934FC4A8209@microsoft.com... > What's wrong with this statement? I'm getting an invalid column name on > Division and Department. I'm trying to get a count of all unique > combinations of the three columns. > > select count(employid), left(divisioncode_i,1) as division, jobtitle, > left(deprtmnt,2) as department > from upr00100 > group by division, jobtitle, department >
From a logical point of view there 'nothing' wrong. This makes perfect sense and there are systems that follow this exact logic in grouping. [quoted text, click to view] "Cindy Mikeworth" <cindy@tas-in.com> wrote in message news:A3D7BF11-B562-47F6-BE23-7A02434AC8F3@microsoft.com... > Thanks everybody!
Lets be clear about the 'Thanks'. Here someone is thanking people for taking them a step 'back' logically! Perhaps William Shakespear said it best: "Where ignorance is bliss, 'tis folly to be wise." Welcome to the world of sql :-) www.beyondsql.blogspot.com
Cindy, Well you spelled Department two different ways so I don't know if that is the correct spelling or not without seeing the DDL. But you need to have the same expression for the group by as the ones in the select list. -- Andrew J. Kelly SQL MVP Solid Quality Mentors [quoted text, click to view] "Cindy Mikeworth" <cindy@tas-in.com> wrote in message news:DAF5AEF9-A696-49DD-AB31-7934FC4A8209@microsoft.com... > What's wrong with this statement? I'm getting an invalid column name on > Division and Department. I'm trying to get a count of all unique > combinations of the three columns. > > select count(employid), left(divisioncode_i,1) as division, jobtitle, > left(deprtmnt,2) as department > from upr00100 > group by division, jobtitle, department
AlterEgo, [quoted text, click to view] > SQL Server doesn't permit aliases in the order by or group by clauses.
You *can* use an alias in ORDER BY. In fact, if SQL were a bit stricter, then alias would be what you could use (and not the original column name or expression). ORDER BY if performed after the SELECT list (which is performed after GROUP BY) so at this point (the ORDER BY point) we have the column aliases. -- Tibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://sqlblog.com/blogs/tibor_karaszi [quoted text, click to view] "AlterEgo" <someone@dslextreme.com> wrote in message news:%238iD3qIBIHA.2004@TK2MSFTNGP06.phx.gbl... > Cindy, > > SQL Server doesn't permit aliases in the order by or group by clauses. Try changing your group by > clause to: > group by left(divisioncode_i,1) , jobtitle, left(deprtmnt,2) > > -- Bill > > "Cindy Mikeworth" <cindy@tas-in.com> wrote in message > news:DAF5AEF9-A696-49DD-AB31-7934FC4A8209@microsoft.com... >> What's wrong with this statement? I'm getting an invalid column name on Division and Department. >> I'm trying to get a count of all unique combinations of the three columns. >> >> select count(employid), left(divisioncode_i,1) as division, jobtitle, left(deprtmnt,2) as >> department >> from upr00100 >> group by division, jobtitle, department > >
Don't see what you're looking for? Try a search.
|