tailieunhanh - Hướng dẫn học Microsoft SQL Server 2008 part 34

Bởi vì bây giờ đã trở về SQL thông tin từ một tập hợp, thay vì xây dựng một hồ sơ đặt hàng, ngay sau khi một truy vấn bao gồm một chức năng tổng hợp, mỗi cột (trong danh sách cột, trong biểu thức, hoặc trong TỰ DO) phải tham gia một chức năng tổng hợp. | Part II Manipulating Data With Select Because SQL is now returning information from a set rather than building a record set of rows as soon as a query includes an aggregate function every column in the column list in the expression or in the ORDER BY must participate in an aggregate function. This makes sense because if a query returned the total number of order sales then it could not return a single order number on the summary row. Because aggregate functions are expressions the result will have a null column name. Therefore use an alias to name the column in the results. To demonstrate the mathematical aggregate functions the following query produces a SUM AVG MIN and MAX of the amount column. SQL Server warns in the result that null values are ignored by aggregate functions which are examined in more detail soon SELECT SUM Amount AS Sum AVG Amount AS Avg MIN Amount AS Min MAX Amount AS Max FROM RawData Result Sum Avg Min Max 946 47 11 91 Warning Null value is eliminated by an aggregate or other SET operation. There s actually more to the COUNT function than appears at first glance. The next query exercises four variations of the COUNT aggregate function SELECT COUNT AS CountStar COUNT RawDatalD AS CountPK COUNT Amount AS CountAmount COUNT DISTINCT Region AS Regions FROM RawData Result CountStar CountPK CountAmount Regions 24 24 20 4 Warning Null value is eliminated by an aggregate or other SET operation. To examine this query in detail the first column COUNT counts every row regardless of any values in the row. COUNT RawDataID counts all the rows with a non-null value in the primary key. Because primary keys by definition can t have any nulls this column also counts every row. These two methods of counting rows have the same query execution plan same performance and same result. The third column COUNT Amount demonstrates why every aggregate query includes a warning. It counts the number of rows with an actual value in the Amount column and it ignores any rows .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.