TAILIEUCHUNG - Joe Celko s SQL for Smarties - Advanced SQL Programming P50

Joe Celko s SQL for Smarties - Advanced SQL Programming P50. In the SQL database community, Joe Celko is a well-known columnist and purveyor of valuable insights. In Joe Celko's SQL for Smarties: Advanced SQL Programming, he picks up where basic SQL training and experience leaves many database professionals and offers tips, techniques, and explanations that help readers extend their capabilities to top-tier SQL programming. Although Celko denies that the book is about database theory, he nevertheless alludes to theory often to buttress his practical points. This title is not for novices, as the author points out. Instead, its intended audience. | 462 CHAPTER 21 AGGREGATE FUNCTIONS or SELECT MIN FROM Personnel AS P1 Personnel AS P2 WHERE AND GROUP BY HAVING COUNT DISTINCT 3 GREATEST and LEAST Functions Oracle has a proprietary pair of functions that return greatest and least values respectively a sort of horizontal max and MIN . The syntax is GREATEST list of values and LEAST list of values . Awkwardly DB2 allows MIN and MAX as synonyms for LEAST and GREATEST. If you have NULLs then you have to decide if they sort high or low if they will be excluded or will propagate the null so that you can define this function several ways. If you don t have nulls in the data CASE WHEN col1 col2 THEN col1 ELSE col2 END If you want the highest non-NULL value CASE WHEN col1 col2 THEN col1 ELSE COALESCE col2 col1 END If you want to return null where one of the cols is null CASE WHEN col1 col2 OR col1 IS NULL THEN col1 ELSE col2 END But for the rest of this section let s assume a b and NULL is high GREATEST a b b GREATEST a NULL NULL GREATEST NULL b NULL GREATEST NULL NULL NULL Extrema Functions 463 We can write this as GREATEST x y CASE WHEN COALESCE x y COALESCE y x THEN x ELSE y END The rules for least are LEAST a b a LEAST a NULL a LEAST NULL b b LEAST NULL NULL NULL This is written LEAST x y CASE WHEN COALESCE x y COALESCE y x THEN COALESCE x y ELSE COALESCE y x END This can be done in Standard SQL but takes a little bit of work. Let s assume that we have a table that holds the scores for a player in a series of five games and we want to get his best score from all five games. CREATE TABLE Games player CHAR 10 NOT NULL PRIMARY KEY score_1 INTEGER NOT NULL DEFAULT 0 score_2 INTEGER NOT NULL DEFAULT 0 score_3 INTEGER NOT NULL DEFAULT 0 score_4 INTEGER NOT NULL DEFAULT 0 score_5 INTEGER NOT NULL DEFAULT 0 and we want to find the GREATEST score_1 score_2 score_3 score_4 score_5 . SELECT player MAX

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.