SQL Server MVP Deep Dives- P5

40 326 0
SQL Server MVP Deep Dives- P5

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

116 CHAPTER Avoiding three common query mistakes Incorrect GROUP BY clauses Figuring out which columns belong in the GROUP BY clause in an aggregate query often aggravates T-SQL developers The rule is that any column that is not part of an aggregate expression in the SELECT or ORDER BY clauses must be listed in the GROUP BY clause That rule seems pretty simple, but I have seen many questions on forums about this very point If a required column is missing from the GROUP BY clause, you will not get incorrect results—you will get no results at all except for an error message If extra columns are listed in the GROUP BY clause, no warning message will appear, but the results will probably not be what you intended The results will be grouped at a more granular level than expected I have even seen code that incorrectly included the aggregated column in the GROUP BY clause The query in listing 10 is missing the GROUP BY clause Listing 10 Missing the GROUP BY clause SELECT COUNT(*), CustomerID FROM Sales.SalesOrderHeader Msg 8120, Level 16, State 1, Line Column 'Sales.SalesOrderHeader.CustomerID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause Listing 11 contains a query that lists the count of orders by CustomerID The query includes the order date in the GROUP BY clause so that the results not make sense Figure shows that there are multiple rows for each CustomerID value Listing 11 An extra column in the GROUP BY clause SELECT COUNT(*) AS CountOfOrders, CustomerID FROM Sales.SalesOrderHeader GROUP BY CustomerID, OrderDate ORDER BY CustomerID Another issue to watch out for is including only the column in the GROUP BY clause when the column is used in an expression in the SELECT list Say you want the results grouped by the year in which the orders were placed If you leave the order date out of the GROUP BY clause, an error will result If you add the column, the error goes away, but the results are not grouped as expected Figure An extra column in the GROUP BY clause causes unexpected results Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross Summary Figure Invalid results because OrderDate was included instead of the expression 117 Figure The results when the expression is included in the GROUP BY clause The query in listing 12 will not produce an error, but the results will not be as intended We want a total for each year; therefore, there should only be one row per year Figure shows multiple rows for 2001 because the results are grouped by the order date Listing 12 This query runs, but the results are invalid SELECT COUNT(*) AS CountOfOrders, YEAR(OrderDate) AS OrderYear FROM Sales.SalesOrderHeader GROUP BY OrderDate ORDER BY YEAR(OrderDate) The way to correct the query is to include the exact expression in the GROUP BY clause, not only the column Listing 13 shows the corrected query with only four rows returned this time, one for each year (see figure 9) Listing 13 Writing the query so that the expression is used in the GROUP BY clause SELECT COUNT(*) AS CountOfOrders, YEAR(OrderDate) AS OrderYear FROM Sales.SalesOrderHeader GROUP BY YEAR(OrderDate) ORDER BY YEAR(OrderDate) Summary Learning to write T-SQL queries is not a skill you gain overnight You must overcome many challenges along the way in order to write queries that return the expected results Hopefully, this chapter will help you avoid three common mistakes Make sure you always think about NULL, especially when NOT, not equal to, or less than (, !=, or

Ngày đăng: 29/10/2013, 02:15

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan