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

Để xem các lý thuyết sẽ bay trong một kịch bản thực tế từ cơ sở dữ liệu mẫu OBXKites, các mã sau đây là một truy vấn khác biệt lập mà nằm tất cả các địa chỉ liên lạc những người chưa đặt hàng. | Part II Manipulating Data With Select To see if the theory will fly in a real-world scenario from the OBXKites sample database the following code is a set difference query that locates all contacts who have not yet placed an order. The Contact table is the divisor and the set difference query removes the contacts with orders the dividend . The left outer join produces a data set with all contacts and matching orders. The WHERE condition restricts the result set to only those rows without a match in the Order table USE OBXKites SELECT FROM LEFT OUTER JOIN dbo. Order ON Order .ContactID WHERE Order .OrderID IS NULL The result is the difference between the Contact table and the Order table that is all contacts who have not placed an order LastName FirstName Andrews Boston Earl Hanks Harrison Ed Dave Betty Nickolas Charlie The set difference query could be written using a subquery covered in the next chapter . The WHERE NOT IN condition shown in the following example removes the subquery rows the divisor from the outer query the dividend . However be aware that while this works logically it doesn t perform well with a large data set. SELECT LastName FirstName FROM WHERE ContactID NOT IN SELECT ContactID FROM dbo. Order ORDER BY LastName FirstName Either form of the query LEFT OUTER JOIN or NOT IN subquery works well with very similar query execution plans as shown in Figure 10-13. Full set difference queries I often use a modified version of this technique to clean up bad data during conversions. A full set difference query is the logical opposite of an inner join. It identifies all rows outside the intersection from either data set by combining a full outer join with a WHERE restriction that accepts only nulls in either primary key SELECT Thing1 Thing2 FROM One 252 Merging Data with Joins and Unions 10 FULL OUTER JOIN Two ON WHERE IS NULL OR

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.