SQL Puzzles & Answers- P8

Tham khảo tài liệu 'sql puzzles & answers- p8', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 262 PUZZLE 65 AGE RANGES FOR PRODUCTS being so we should be safe. A quick way is to use your auxiliary Sequence table. SELECT FROM PriceByAge AS P Sequence AS S WHERE BETWEEN AND AND 150 GROUP BY HAVING COUNT seq 150 Please purchase PDF Split-Merge on to remove this watermark. PUZZLE 66 SUDOKU 263 PUZZLE 66 SUDOKU I thought that Sudoku the current puzzle fad would be a good SQL programming problem. You start with a 9x9 grid that is further divided into nine 3x3 regions. Some of the cells will have a digit from 1 to 9 in them at the start of the puzzle. Your goal is to fill in all the cells with more digits such that each row column and region contains one and only one instance of each digit. Strangely the puzzle appeared in the United States in 1979 then caught on in Japan in 1986 and became an international fad in 2005. Most newspapers today carry a daily Sudoku. How can we do this in SQL Well we can start by modeling the grid as an i j array with a value in the cell. The first attempt usually does not have the region information as one of the columns. The regions do not have names in the puzzle so we need a way to give them names. CREATE TABLE SudokuGrid i INTEGER NOT NULL CHECK i BETWEEN 1 AND 9 j INTEGER NOT NULL CHECK j BETWEEN 1 AND 9 val INTEGER NOT NULL CHECK val BETWEEN 1 AND 9 region_nbr INTEGER NOT NULL PRIMARY KEY i j val Now we need to fill it. Each i j cell needs to start with all nine digits so we build a table of the digits 1 to 9 and do CROSS JOINs. But how do we get a region number An obvious name would be the position of the region by x y coordinates where x 1 2 3 and y 1 2 3 . We can then make them into one number by making x the tens place and y the units place so we get 11 12 13 21 22 23 31 32 33 for the regions. The math for this depends on integer arithmetic but it is not really hard. INSERT INTO SudokuGrid i j val region_nbr SELECT 10 2 3 2 3

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã 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.