Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Queries with Aggregate Functions Expand / Collapse
Author
Message
Posted Thursday, May 21, 2009 1:06 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, May 05, 2010 10:47 PM
Points: 93, Visits: 98
Comments posted to this topic are about the item Queries with Aggregate Functions
Post #721145
Posted Thursday, May 21, 2009 5:56 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 17, 2014 12:38 PM
Points: 2,577, Visits: 95
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.



Post #721248
Posted Thursday, May 21, 2009 6:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 12:24 PM
Points: 2,835, Visits: 1,124
HumanResources is the schema, so all queries were correctly qualified. The question specifically states that the database is AdventureWorks.


Post #721264
Posted Thursday, May 21, 2009 6:39 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, September 24, 2012 8:10 AM
Points: 2,042, 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 AdventureWorks;) then the queries will execute (at least the ones that should!)

Cheers,
Brian
Post #721274
Posted Thursday, May 21, 2009 7:30 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 4:12 PM
Points: 2,007, Visits: 768
~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.
Post #721330
Posted Thursday, May 21, 2009 7:59 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, April 07, 2014 1:17 AM
Points: 500, Visits: 293
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.
Post #721359
Posted Thursday, May 21, 2009 9:09 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, April 04, 2014 1:13 PM
Points: 384, Visits: 376
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
Post #721425
Posted Thursday, May 21, 2009 9:31 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 4:12 PM
Points: 2,007, Visits: 768
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).
Post #721448
Posted Wednesday, May 27, 2009 2:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 11:08 AM
Points: 1,091, Visits: 1,207
Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

Mahesh


MH-09-AM-8694
Post #723876
Posted Wednesday, May 27, 2009 4:51 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 1,659, Visits: 6,008
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.
Post #723949
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse