Joe Celko s SQL for Smarties - Advanced SQL Programming P15

Joe Celko s SQL for Smarties - Advanced SQL Programming P15. 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. | 112 CHAPTER 3 NUMERIC DATA IN SQL have one row with one column for each category and one column for the grand total of all customer purchases. The table is declared like this CREATE TABLE Customers cust_nbr INTEGER NOT NULL purchase_nbr INTEGER NOT NULL category CHAR 1 CONSTRAINT proper_category CHECK category IN A B C purchase_amt DECIMAL 8 2 NOT NULL . PRIMARY KEY cust_nbr purchase_nbr As an example of the use of coalesce create a table of payments made for each month of a single year. Yes this could be done with a column for the months but bear with me. CREATE TABLE Payments cust_nbr INTEGER NOT NULL jan DECIMAL 8 2 feb DECIMAL 8 2 mar DECIMAL 8 2 apr DECIMAL 8 2 may DECIMAL 8 2 jun DECIMAL 8 2 jul DECIMAL 8 2 aug DECIMAL 8 2 sep DECIMAL 8 2 oct DECIMAL 8 2 nov DECIMAL 8 2 dec DECIMAL 8 2 DEC is a reserved word PRIMARY KEY cust_nbr The problem is to write a query that returns the customer and the amount of the last payment he made. Unpaid months are shown with a null in them. We could use a coalesce function like this SELECT cust_nbr COALESCE dec nov oct sep aug jul jun may apr mar feb jan FROM Customers Vendor Math Functions 113 Of course this query is a bit incomplete since it does not tell you in what month this last payment was made. This can be done with the rather ugly-looking expression that will turn a month s non NULL payment into a character string with the name of the month. The general case for a column called mon which holds the number of a month within the year is NULLIF COALESCE NULLIF 0 mon-mon Month 0 where Month is replaced by the string for the actual name of the particular month. A list of these statements in month order in an COALESCE will give us the name of the last month with a payment. The way this expression works is worth working out in detail. Case 1 mon is a numeric value NULLIF COALESCE NULLIF 0 mon-mon Month 0 NULLIF COALESCE NULLIF 0 0 Month 0 NULLIF COALESCE NULL Month 0 NULLIF Month 0 Month Case 2 mon is NULL NULLIF COALESCE

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
24    19    1    27-11-2024
Đã 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.