Click here to monitor SSC
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 Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 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: 2577 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
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

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



beezell
beezell
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2042 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 (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2017 Visits: 796
~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
Say Hey Kid
Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)Say Hey Kid (683 reputation)

Group: General Forum Members
Points: 683 Visits: 440
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 (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)

Group: General Forum Members
Points: 415 Visits: 431
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 (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2017 Visits: 796
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 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.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2415 Visits: 8062
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