Hàm CASE trong SQL Server 2005 Phần 4
Số trang: 5
Loại file: pdf
Dung lượng: 170.58 KB
Lượt xem: 8
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, phần 2 và phần 3 của loạt bài này, chúng tôi đã giới thiệu cách sử dụng các hàm CASE đơn giản trong truy vấn.
Nội dung trích xuất từ tài liệu:
Hàm CASE trong SQL Server 2005 Phần 4Hàm CASE trong SQL Server 2005 (Phần 4)Nguồn:quantrimang.comTrong phần 1, phần 2 và phần 3 của loạt bài này, chúng tôi đã giới thiệucách sử dụng các hàm CASE đơn giản trong truy vấn. Trong phần tiếp theonày, tôi sẽ giải thích cách sử dụng các hàm CASE trong mệnh đề như‘Group by’Phương thức 7: Sử dụng hàm CASE đơn giản trong mệnh đề GROUP BYGiả sử chúng ta đã có bảng sauset quoted_identifier offgouse tempdbgoif exists (select * from dbo.sysobjects where id =object_id(N[emp])and OBJECTPROPERTY(id, NIsUserTable) = 1)drop table [emp]GOcreate table Emp (id int, [First name] varchar(50),[Last name] varchar(50), Salary money, state char(2))goinsert into Emp (id,[First name],[Last name], salary,State ) values (1,John,Smith,120000,WA)insert into Emp (id,[First name],[Last name], salary,State ) values (2,James,Bond,95000,OR)insert into Emp (id,[First name],[Last name], salary ,State) values (3,Alexa,Mantena,200000,WY)insert into Emp (id,[First name],[Last name], salary,State ) values (4,Shui,Qui,36000,CO)insert into Emp (id,[First name],[Last name], salary,State ) values (5,William,Hsu,39000,NE)insert into Emp (id,[First name],[Last name], salary ,State) values (6,Danielle,Stewart,50000,TX)insert into Emp (id,[First name],[Last name], salary ,State) values (7,Martha,Mcgrath,400000,PA)insert into Emp (id,[First name],[Last name], salary,State ) values (8,Henry,Fayol,75000,NJ)insert into Emp (id,[First name],[Last name], salary,State ) values (9,Dick,Watson,91000,NY)insert into Emp (id,[First name],[Last name], salary,State ) values (10,Helen,Foster,124000,AK)goVà bây giờ chúng ta cần có 6 bảng để lưu trữ các ID của nhân viên thuộc cácvùng thời gian khác nhau như trình bày sauif exists (select * from dbo.sysobjectswhere id = object_id(N[eastern])and objectproperty(id, Nisusertable) = 1)drop table [eastern]gocreate table eastern (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[mountain])and objectproperty(id, Nisusertable) = 1)drop table [mountain]gocreate table mountain (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[hawaii])and objectproperty(id, Nisusertable) = 1)drop table [hawaii]gocreate table hawaii (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[central])and objectproperty(id, Nisusertable) = 1)drop table [central]gocreate table central (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[alaskan])and objectproperty(id, Nisusertable) = 1)drop table [alaskan]gocreate table alaskan (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[pacific])and objectproperty(id, Nisusertable) = 1)drop table [pacific]gocreate table pacific (id int)goinsert into pacific (id) values (1)insert into pacific (id) values (2)insert into mountain (id) values (3)insert into mountain (id) values (4)insert into central (id) values (5)insert into central (id) values (6)insert into eastern (id) values (7)insert into eastern (id) values (8)insert into eastern (id) values (9)insert into alaskan (id) values (10)goNếu bạn muốn biết toàn bộ nhân viên thuộc vùng thời gian ở Eastern, bạn chắcchắn sẽ phải thực thi một câu lệnh truy vấn đơn giản như dưới đâyselect e.id,[First Name],[Last name], Salary, Statefrom emp e join eastern ee on e.id=ee.idCâu lệnh truy vấn trên sẽ trả về kết quả như sauid First name Last name salary Timezone--------------------------------------------------------7 Martha Mcgrath 400000.00 PA8 Henry Fayol 75000.00 NJ9 Dick Watson 91000.00 NYVậy bây giờ giả sử chúng ta cần tạo một kịch bản cho phép đưa một khu vựcthời gian vào trong một biến và hiển thị ra kết quả dựa trên giá trị của biến đó.Điều này hoàn toàn có thể làm được khi sử dụng mệnh đề và hàm CASE nhưsau:declare @group varchar(10)set @group=Pacificselect ee.id,ee.[First Name],ee.[Last Name],Salary, State,@group as TimeZone from emp eeleft join mountain m on m.[id]=ee.[id]left join alaskan a on a.[id]=ee.[id]left join hawaii h on h.[id]=ee.[id]left join central c on c.[id]=ee.[id]left join pacific p on p.[id]=ee.[id]left join eastern e on e.[id]=ee.[id]where ee.id in ( case @groupwhen Eastern then e.idwhen Mountain then m.idwhen Pacific then p.idwhen Alaskan then a.idwhen Hawaii then h.idwhen Central then c.idend)Đoạn kịch bản trên sẽ có kết quả như sau:id First name Last name salary state TimZone----------------------------------------------------------------1 John Smith 120000.00 WA Pacific2 James Bond 95000.00 OR PacificĐoạn script trên có thể được viết trong một thủ tục như sau:create procedure emp_ ...
Nội dung trích xuất từ tài liệu:
Hàm CASE trong SQL Server 2005 Phần 4Hàm CASE trong SQL Server 2005 (Phần 4)Nguồn:quantrimang.comTrong phần 1, phần 2 và phần 3 của loạt bài này, chúng tôi đã giới thiệucách sử dụng các hàm CASE đơn giản trong truy vấn. Trong phần tiếp theonày, tôi sẽ giải thích cách sử dụng các hàm CASE trong mệnh đề như‘Group by’Phương thức 7: Sử dụng hàm CASE đơn giản trong mệnh đề GROUP BYGiả sử chúng ta đã có bảng sauset quoted_identifier offgouse tempdbgoif exists (select * from dbo.sysobjects where id =object_id(N[emp])and OBJECTPROPERTY(id, NIsUserTable) = 1)drop table [emp]GOcreate table Emp (id int, [First name] varchar(50),[Last name] varchar(50), Salary money, state char(2))goinsert into Emp (id,[First name],[Last name], salary,State ) values (1,John,Smith,120000,WA)insert into Emp (id,[First name],[Last name], salary,State ) values (2,James,Bond,95000,OR)insert into Emp (id,[First name],[Last name], salary ,State) values (3,Alexa,Mantena,200000,WY)insert into Emp (id,[First name],[Last name], salary,State ) values (4,Shui,Qui,36000,CO)insert into Emp (id,[First name],[Last name], salary,State ) values (5,William,Hsu,39000,NE)insert into Emp (id,[First name],[Last name], salary ,State) values (6,Danielle,Stewart,50000,TX)insert into Emp (id,[First name],[Last name], salary ,State) values (7,Martha,Mcgrath,400000,PA)insert into Emp (id,[First name],[Last name], salary,State ) values (8,Henry,Fayol,75000,NJ)insert into Emp (id,[First name],[Last name], salary,State ) values (9,Dick,Watson,91000,NY)insert into Emp (id,[First name],[Last name], salary,State ) values (10,Helen,Foster,124000,AK)goVà bây giờ chúng ta cần có 6 bảng để lưu trữ các ID của nhân viên thuộc cácvùng thời gian khác nhau như trình bày sauif exists (select * from dbo.sysobjectswhere id = object_id(N[eastern])and objectproperty(id, Nisusertable) = 1)drop table [eastern]gocreate table eastern (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[mountain])and objectproperty(id, Nisusertable) = 1)drop table [mountain]gocreate table mountain (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[hawaii])and objectproperty(id, Nisusertable) = 1)drop table [hawaii]gocreate table hawaii (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[central])and objectproperty(id, Nisusertable) = 1)drop table [central]gocreate table central (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[alaskan])and objectproperty(id, Nisusertable) = 1)drop table [alaskan]gocreate table alaskan (id int)if exists (select * from dbo.sysobjectswhere id = object_id(N[pacific])and objectproperty(id, Nisusertable) = 1)drop table [pacific]gocreate table pacific (id int)goinsert into pacific (id) values (1)insert into pacific (id) values (2)insert into mountain (id) values (3)insert into mountain (id) values (4)insert into central (id) values (5)insert into central (id) values (6)insert into eastern (id) values (7)insert into eastern (id) values (8)insert into eastern (id) values (9)insert into alaskan (id) values (10)goNếu bạn muốn biết toàn bộ nhân viên thuộc vùng thời gian ở Eastern, bạn chắcchắn sẽ phải thực thi một câu lệnh truy vấn đơn giản như dưới đâyselect e.id,[First Name],[Last name], Salary, Statefrom emp e join eastern ee on e.id=ee.idCâu lệnh truy vấn trên sẽ trả về kết quả như sauid First name Last name salary Timezone--------------------------------------------------------7 Martha Mcgrath 400000.00 PA8 Henry Fayol 75000.00 NJ9 Dick Watson 91000.00 NYVậy bây giờ giả sử chúng ta cần tạo một kịch bản cho phép đưa một khu vựcthời gian vào trong một biến và hiển thị ra kết quả dựa trên giá trị của biến đó.Điều này hoàn toàn có thể làm được khi sử dụng mệnh đề và hàm CASE nhưsau:declare @group varchar(10)set @group=Pacificselect ee.id,ee.[First Name],ee.[Last Name],Salary, State,@group as TimeZone from emp eeleft join mountain m on m.[id]=ee.[id]left join alaskan a on a.[id]=ee.[id]left join hawaii h on h.[id]=ee.[id]left join central c on c.[id]=ee.[id]left join pacific p on p.[id]=ee.[id]left join eastern e on e.[id]=ee.[id]where ee.id in ( case @groupwhen Eastern then e.idwhen Mountain then m.idwhen Pacific then p.idwhen Alaskan then a.idwhen Hawaii then h.idwhen Central then c.idend)Đoạn kịch bản trên sẽ có kết quả như sau:id First name Last name salary state TimZone----------------------------------------------------------------1 John Smith 120000.00 WA Pacific2 James Bond 95000.00 OR PacificĐoạn script trên có thể được viết trong một thủ tục như sau:create procedure emp_ ...
Tìm kiếm theo từ khóa liên quan:
Cơ sở dữ liệu An ninh – Bảo mật Công nghệ thông tin Quản trị mạng Thủ thuật máy tínhGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
62 trang 401 3 0
-
Đề thi kết thúc học phần học kì 2 môn Cơ sở dữ liệu năm 2019-2020 có đáp án - Trường ĐH Đồng Tháp
5 trang 377 6 0 -
24 trang 353 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 312 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 299 0 0 -
74 trang 294 0 0
-
Giáo trình Cơ sở dữ liệu: Phần 2 - TS. Nguyễn Hoàng Sơn
158 trang 292 0 0 -
13 trang 292 0 0
-
96 trang 291 0 0