Hàm CASE trong SQL Server 2005 (Phần 2)
Số trang: 5
Loại file: pdf
Dung lượng: 141.29 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Trong phần 1 của loạt bài này chúng tôi đã giải thích các sử dụng hàm CASE đơn giản trong truy vấn. Trong phần II này, chúng tôi sẽ tiếp tục thảo luận về cách sử dụng hàm CASE trong một số trường hợp khác. Phương thức 4: Sử dụng hàm CASE trong tìm kiếm Giả sử chúng ta có bảng sau use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[emp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [emp] GO create table Emp (id int, [First name] varchar(50), [Last name] varchar(50), Salary money) go insert into...
Nội dung trích xuất từ tài liệu:
Hàm CASE trong SQL Server 2005 (Phần 2) Hàm CASE trong SQL Server 2005 (Phần 2) Trong phần 1 của loạt bài này chúng tôi đã giải thích các sử dụng hàm CASE đơn giản trong truy vấn. Trong phần II này, chúng tôi sẽ tiếp tục thảo luận về cách sử dụng hàm CASE trong m ột số trường hợp khác. Phương thức 4: Sử dụng hàm CASE trong tìm kiếm Giả sử chúng ta có bảng sau use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[emp]') and OBJECTPRO PERTY(id, N'IsUserTable') = 1) drop table [emp] GO create table Emp (id int, [First name] varchar(50), [Last name] varchar(50), Salary money) go insert into Emp (id,[First name],[Last name], salary ) values (1,'John','Smith',120000) insert into Emp (id,[First name],[Last name], salary ) values (2,'James','Bond',95000) insert into Emp (id,[First name],[Last name], salary ) values (3,'Alexa','Mantena',200000) insert into Emp (id,[First name],[Last name], salary ) values (4,'Shui','Qui',36000) insert into Emp (id,[First name],[Last name], salary ) values (5,'William','Hsu',39000) insert into Emp (id,[First name],[Last name], salary ) values (6,'Danielle','Stewart',50000) insert into Emp (id,[First name],[Last name], salary ) values (7,'Martha','Mcgrath',400000) insert into Emp (id,[First name],[Last name], salary ) values (8,'Henry','Fayol',75000) insert into Emp (id,[First name],[Last name], salary ) values (9,'Dick','Watson',91000) insert into Emp (id,[First name],[Last name], salary ) values (10,'Helen','Foster',124000) go Và giờ muốn tạo thêm một cột Tax (thuế) dựa trên mức lương như sau Select [id],[Full Name]=[First name]+ [Last name],Salary,Tax = case When salary between 0 and 36000 then Salary*.24 When salary between 36000 and 450000 then Salary*.28 When salary between 45000 and 75000 then Salary *.30 When salary between 75000 and 150000 then Salary *.32 else Salary*.40 end from Emp Hàm này sẽ cho kết quả: id Full Name Salary Tax ----------- -------------------------------- --------------------- 1 JohnSmith 120000.00 33600.000000 2 JamesBond 95000.00 26600.000000 3 AlexaMantena 200000.00 56000.000000 4 ShuiQui 36000.00 8640.000000 5 WilliamHsu 39000.00 10920.000000 6 DanielleStewart 50000.00 14000.000000 7 MarthaMcgrath 400000.00 112000.000000 8 HenryFayol 75000.00 21000.000000 9 DickWatson 91000.00 25480.000000 10 HelenFoster 124000.00 34720.000000 Phương thức 5: Sử dụng hàm CASE trong mệnh đề ORDER BY Giả sử chúng ta có bảng d ưới trong Books: use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[Books]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Books] GO create table Books (Bookid int, Title varchar(100), Authorname varchar(100), state char(2)) go insert into Books (Bookid, Title, Authorname, state) values (1, 'The Third Eye','Lobsang Rampa','CA') insert into Books (Bookid, Title, Authorname, state) values (2, 'Service Oriented Architecture For Dummies', 'Judith Hurwitz','NJ') insert into Books (Bookid, Title, Authorname, state) values (3, 'Business Reference for Students and Professionals','Ray Myers','NY') insert into Books (Bookid, Title, Authorname, state) values (4, 'More Java Gems','Dwight Deugo', 'FL') insert into Books (Bookid, Title, Autho rname, state) values (5, 'Six Sigma Workbook For Dummies','Craig Gygi','FL') insert into Books (Bookid, Title, Authorname, state) values (6, 'Performance Appraisals: How to Achieve Top Results', 'Priscilla A. Glidden', 'NC' ) insert into Books (Bookid, Title, Authorname, state) values (7, 'Talent Management: From Competencies to Organizational Performance', 'John Smith','FL') insert into Books (Bookid, Title, Authorname, state) values (8, 'Using Unix','Howard Johnson','CT') insert into Books (Bookid, Title, Authorname, state) values (9, 'Mastering Oracle','Erina Zolotrova','CT') insert into Books (Bookid, Title, Authorname, state) values (10, 'How to become CEO','Olga Zohaskov','NY') go Để truy vấn tất cả các giá trị trong bảng ta sử dụng h àm truy vấn dưới: Select * from Books Hàm này sẽ cho kết quả như hình dưới Giả sử chúng ta muốn hiển thị toàn bộ số sách theo thứ tự các bang: đầu ti ên là NY, sau đó là CA, NJ, CT và FL. Bạn có thể thực hiện được điều này bằng cách sử dụng hàm CASE như dưới đây: select Title, Authorname, state from Books order by case when state ='NY' then 1 when state ='CA' then 2 when state ='NJ' then 3 when state ='CT' then 4 when state ='FL' then 5 else 6 end Hàm này sẽ cho kết quả như sau: Title Authorname state ------------------------------------------------------------------ ----------------------- ----- Business Reference for Students and Professionals Ray Myers NY How to become CEO Olga Zohaskov NY The Third Eye Lobsang Rampa CA Service Oriented Architecture For Dummies Judith Hurwitz NJ Using Unix Howard Johnson CT Mastering Oracle Erina Zolotrova CT More Java Gems Dwight Deugo FL Six Sigma Workbook For Dummies Craig Gygi FL Talent Management: From Competencies to Organizational Per John Smith FL Performance Appraisals: How to Achieve Top Results Priscilla A. Glidden NC Kết luận Trong phần một và phần hai của loạt bài này, chúng tôi đã hướng dẫn cách s ...
Nội dung trích xuất từ tài liệu:
Hàm CASE trong SQL Server 2005 (Phần 2) Hàm CASE trong SQL Server 2005 (Phần 2) Trong phần 1 của loạt bài này chúng tôi đã giải thích các sử dụng hàm CASE đơn giản trong truy vấn. Trong phần II này, chúng tôi sẽ tiếp tục thảo luận về cách sử dụng hàm CASE trong m ột số trường hợp khác. Phương thức 4: Sử dụng hàm CASE trong tìm kiếm Giả sử chúng ta có bảng sau use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[emp]') and OBJECTPRO PERTY(id, N'IsUserTable') = 1) drop table [emp] GO create table Emp (id int, [First name] varchar(50), [Last name] varchar(50), Salary money) go insert into Emp (id,[First name],[Last name], salary ) values (1,'John','Smith',120000) insert into Emp (id,[First name],[Last name], salary ) values (2,'James','Bond',95000) insert into Emp (id,[First name],[Last name], salary ) values (3,'Alexa','Mantena',200000) insert into Emp (id,[First name],[Last name], salary ) values (4,'Shui','Qui',36000) insert into Emp (id,[First name],[Last name], salary ) values (5,'William','Hsu',39000) insert into Emp (id,[First name],[Last name], salary ) values (6,'Danielle','Stewart',50000) insert into Emp (id,[First name],[Last name], salary ) values (7,'Martha','Mcgrath',400000) insert into Emp (id,[First name],[Last name], salary ) values (8,'Henry','Fayol',75000) insert into Emp (id,[First name],[Last name], salary ) values (9,'Dick','Watson',91000) insert into Emp (id,[First name],[Last name], salary ) values (10,'Helen','Foster',124000) go Và giờ muốn tạo thêm một cột Tax (thuế) dựa trên mức lương như sau Select [id],[Full Name]=[First name]+ [Last name],Salary,Tax = case When salary between 0 and 36000 then Salary*.24 When salary between 36000 and 450000 then Salary*.28 When salary between 45000 and 75000 then Salary *.30 When salary between 75000 and 150000 then Salary *.32 else Salary*.40 end from Emp Hàm này sẽ cho kết quả: id Full Name Salary Tax ----------- -------------------------------- --------------------- 1 JohnSmith 120000.00 33600.000000 2 JamesBond 95000.00 26600.000000 3 AlexaMantena 200000.00 56000.000000 4 ShuiQui 36000.00 8640.000000 5 WilliamHsu 39000.00 10920.000000 6 DanielleStewart 50000.00 14000.000000 7 MarthaMcgrath 400000.00 112000.000000 8 HenryFayol 75000.00 21000.000000 9 DickWatson 91000.00 25480.000000 10 HelenFoster 124000.00 34720.000000 Phương thức 5: Sử dụng hàm CASE trong mệnh đề ORDER BY Giả sử chúng ta có bảng d ưới trong Books: use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[Books]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Books] GO create table Books (Bookid int, Title varchar(100), Authorname varchar(100), state char(2)) go insert into Books (Bookid, Title, Authorname, state) values (1, 'The Third Eye','Lobsang Rampa','CA') insert into Books (Bookid, Title, Authorname, state) values (2, 'Service Oriented Architecture For Dummies', 'Judith Hurwitz','NJ') insert into Books (Bookid, Title, Authorname, state) values (3, 'Business Reference for Students and Professionals','Ray Myers','NY') insert into Books (Bookid, Title, Authorname, state) values (4, 'More Java Gems','Dwight Deugo', 'FL') insert into Books (Bookid, Title, Autho rname, state) values (5, 'Six Sigma Workbook For Dummies','Craig Gygi','FL') insert into Books (Bookid, Title, Authorname, state) values (6, 'Performance Appraisals: How to Achieve Top Results', 'Priscilla A. Glidden', 'NC' ) insert into Books (Bookid, Title, Authorname, state) values (7, 'Talent Management: From Competencies to Organizational Performance', 'John Smith','FL') insert into Books (Bookid, Title, Authorname, state) values (8, 'Using Unix','Howard Johnson','CT') insert into Books (Bookid, Title, Authorname, state) values (9, 'Mastering Oracle','Erina Zolotrova','CT') insert into Books (Bookid, Title, Authorname, state) values (10, 'How to become CEO','Olga Zohaskov','NY') go Để truy vấn tất cả các giá trị trong bảng ta sử dụng h àm truy vấn dưới: Select * from Books Hàm này sẽ cho kết quả như hình dưới Giả sử chúng ta muốn hiển thị toàn bộ số sách theo thứ tự các bang: đầu ti ên là NY, sau đó là CA, NJ, CT và FL. Bạn có thể thực hiện được điều này bằng cách sử dụng hàm CASE như dưới đây: select Title, Authorname, state from Books order by case when state ='NY' then 1 when state ='CA' then 2 when state ='NJ' then 3 when state ='CT' then 4 when state ='FL' then 5 else 6 end Hàm này sẽ cho kết quả như sau: Title Authorname state ------------------------------------------------------------------ ----------------------- ----- Business Reference for Students and Professionals Ray Myers NY How to become CEO Olga Zohaskov NY The Third Eye Lobsang Rampa CA Service Oriented Architecture For Dummies Judith Hurwitz NJ Using Unix Howard Johnson CT Mastering Oracle Erina Zolotrova CT More Java Gems Dwight Deugo FL Six Sigma Workbook For Dummies Craig Gygi FL Talent Management: From Competencies to Organizational Per John Smith FL Performance Appraisals: How to Achieve Top Results Priscilla A. Glidden NC Kết luận Trong phần một và phần hai của loạt bài này, chúng tôi đã hướng dẫn cách s ...
Tìm kiếm theo từ khóa liên quan:
Lý thuyết tin học SQL Tin học đại cương giáo trình Tin học đại cương bài giảng Tin học đại cương tài liệu Tin học đại cương lý thuyết Tin học đại cươngGợi ý tài liệu liên quan:
-
Ứng dụng công cụ Quizizz thiết kế trò chơi học tập trong giảng dạy học phần tin học đại cương
12 trang 298 0 0 -
Tài liệu hướng dẫn thực hành Tin học đại cương - ĐH Bách Khoa Hà Nội
40 trang 257 0 0 -
Giáo trình Tin học đại cương part 7
19 trang 231 0 0 -
Giáo trình Tin học đại cương: Phần 1 - ĐH Kinh tế Quốc Dân
130 trang 156 0 0 -
Giáo trình Tin học đại cương (Tái bản năm 2020): Phần 1 - PGS.TS. Nguyễn Thị Thu Thủy (Chủ biên)
105 trang 141 0 0 -
Hướng dẫn thực hành lập trình C trên Visual Studio
9 trang 126 0 0 -
Giáo trình Tin học đại cương: Phần 1 - Vi Hồng Thắm
90 trang 125 0 0 -
Giáo trình Tin học đại cương: Phần 2 - Trần Đình Khang
118 trang 115 0 0 -
Trắc nghiệm và đáp án hệ cơ sở dữ liệu - ĐH Công Nghiệp Tp. Hồ Chí Minh
63 trang 113 0 0 -
Quản trị người dùng trong Exchange 2007 bằng Powershell
9 trang 106 0 0