Queries with Aggregate Functions

  • Comments posted to this topic are about the item Queries with Aggregate Functions

  • 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.

  • HumanResources is the schema, so all queries were correctly qualified. The question specifically states that the database is AdventureWorks.

  • 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

  • ~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.

  • 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.

  • 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

  • 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).

  • [font="Verdana"]Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

    Mahesh[/font]

    MH-09-AM-8694

  • Mahesh Bote (5/27/2009)


    [font="Verdana"]Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

    Mahesh[/font]

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

  • [font="Verdana"]

    Toreador (5/27/2009)


    Mahesh Bote (5/27/2009)


    [font="Verdana"]Answer should be 1, 2 and 4 as well as 2nd and 4th query are same.

    Mahesh[/font]

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

    oops !!!, you are right. my mistake. [/font]

    MH-09-AM-8694

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply