SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Queries with Aggregate Functions


Queries with Aggregate Functions

Author
Message
Manish Sinha
Manish Sinha
SSC-Enthusiastic
SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)SSC-Enthusiastic (165 reputation)

Group: General Forum Members
Points: 165 Visits: 98
Comments posted to this topic are about the item Queries with Aggregate Functions
Larry Briscoe
Larry Briscoe
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2583 Visits: 99
All queries failed due to missing Owner/Schema.

Example:
Invalid... "FROM HumanResources.Employee"
Valid... "FROM HumanResources..Employee"
Valid... "FROM HumanResources.dbo.Employee"

Please review this and award points back.



Scott Coleman
Scott Coleman
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3878 Visits: 1465
HumanResources is the schema, so all queries were correctly qualified. The question specifically states that the database is AdventureWorks.



beezell
beezell
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 499
The only reason you would need the three part name was if you were referencing a different database [DatabaseName].[SchemaName].[TableName] on the same instance. In this case HumanResource is the schema so it is referencing it in this way: [SchemaName].[TableName]. As long as you are using the AdventureWorks database (USE AdventureWorksWink then the queries will execute (at least the ones that should!)

Cheers,
Brian
Noel McKinney
Noel McKinney
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2081 Visits: 798
~Larry Briscoe (5/21/2009)
All queries failed due to missing Owner/Schema.

The queries that were supposed to run worked fine for me.

I think a potential issue in the question that could get tricky for some people is the SUM(VacationHours) in Query 1. The column VacationHours is defined as smallint. If someone is working on a modified their AdventureWorks database then the HumanResources.Employee table might have a substantial number of records added to it, or updates to the VacationHours column, such that summing VacationHours gives an overflow error.
mike.mcquillan
mike.mcquillan
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1157 Visits: 454
I have an unmodified AdventureWorks database and queries 1, 2, and 4 worked without issue for me. I was disappointed to find 1, 2, and 4 was not listed as an answer. I was forced to select all will work, which I knew was wrong but was the nearest answer to my findings.

More research should have been performed on this question.
ppcx
ppcx
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 439
I have an unmodified AdventureWorks database. 1 and 2 work fine. 3 and 4 fail.

3 gives:

Msg 207, Level 16, State 1, Line 2
Invalid column name 'EmployeeId'.

4 gives:

Msg 8127, Level 16, State 1, Line 2
Column "HumanResources.Employee.ManagerID" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

EDIT: this is MSSQL 2005 SP2 9.0.3073 64-bit
Noel McKinney
Noel McKinney
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2081 Visits: 798
ppcx (5/21/2009)

3 gives:

Msg 207, Level 16, State 1, Line 2
Invalid column name 'EmployeeId'.

Is your database collation case sensitive? That would be the reason "EmployeeId" is invalid, because it's actually "EmployeeID" (capital D) which is fine for case insensitive collation but an not for case sensitive.

Query 3 should give an error because EmployeeId is in the SELECT list but is not contained in the GROUP BY clause (or in an aggregate function within the SELECT list).
Mahesh Bote
Mahesh Bote
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1520 Visits: 1274
Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

Mahesh


MH-09-AM-8694
Toreador
Toreador
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2817 Visits: 8084
Mahesh Bote (5/27/2009)
Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

Mahesh


Look more carefully - the second query is 'GROUP BY ManagerID' and will work, the 4th is 'ORDER BY ManagerID' and will fail.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search