tailieunhanh - Hướng dẫn học Microsoft SQL Server 2008 part 32
Kết quả này bao gồm P. luật, nhưng không phải là tên hoặc mô tả. Tất nhiên, có thể chỉ đơn giản là nhóm của mỗi cột phải được trả lại, nhưng đó là cẩu thả. Các truy vấn sau đây thực hiện việc tổng kết tổng hợp trong một subquery đó là sau đó gia nhập với các bảng sản phẩm để mỗi cột có sẵn không có việc làm thêm: | Part II Manipulating Data With Select The result includes but not the name or description. Of course it s possible to simply group by every column to be returned but that s sloppy. The following query performs the aggregate summation in a subquery that is then joined with the Product table so that every column is available without additional work SELECT FROM AS P JOIN SELECT ProductID SUM Quantity AS QuantitySold FROM GROUP BY ProductID AS Sales ON ORDER BY If you use SQL Server Management Studio s Query Designer a derived table may be added to the query. Figure 11-1 illustrates the previous query being constructed using the GUI tool. FIGURE 11-1 Derived tables may be included within Query Designer by using the context menu and selecting Add Derived Table. 272 Including Data with Subqueries and CTEs 11 The query is fast and efficient it provides the required aggregate data and all the product columns can be added to the output columns. The result is as follows Code ProductName QuantitySold 1002 Dragon Flight 1003 Sky Dancer 1004 Rocket Kite 1012 Falcon F-16 Another example of using a derived table to solve a problem answers the question How many children has each mother borne from the Family sample database USE Family SELECT FROM AS p JOIN SELECT COUNT AS Children FROM AS m WHERE IS NOT NULL GROUP BY AS c ON ORDER BY DESC The subquery performs the aggregate summation and the columns are joined with the Person table to present the final results as follows PersonID FirstName LastName Children 6 Audry Halloway 4 8 Melanie Campbell 3 12 Alysia Halloway 3 20 Grace Halloway 2 Row constructors New for SQL Server 2008 row constructors provide a convenient way to supply hard-coded values .
đang nạp các trang xem trước