2013-04-08 25 views
10
(case [dbo].[YearsInService]([DateEngaged],getdate()) 
    when (0) then (0) 
    when (1) then (4) 
    when (2) then (8) 
    when (3) then (12) 
    when (4) then (32) 
    when (5) then (40) 
    when (6) then (48) 
    when (7) then (56) 
    when (8) then (104) 
    when (9) then (117) 
    when (10) then (150) else (-1) end) 

Bây giờ trong dòng cuối cùng, làm thế nào tôi có thể nói từ 10 trở lên phải được trả lại là 150?Sử dụng tsql lớn hơn ký hiệu trong Biểu thức CASE

+0

Điều khoản ELSE' hiện tại của bạn có nghĩa là gì? –

+0

@Damien_The_Unbeliever: cho khi DateEngaged là NULL – StackTrace

Trả lời

11

Bạn không thể, CASE YourFunction WHEN ... chỉ dành cho mức độ cân bằng. Nếu bạn cần phải sử dụng "lớn hơn", bạn sẽ cần phải viết lại biểu hiện của bạn theo cách này:

CASE WHEN [dbo].[YearsInService]([DateEngaged],getdate()) = 0 THEN 0 
WHEN [dbo].[YearsInService]([DateEngaged],getdate()) = 1 THEN 4 
WHEN..... 
WHEN [dbo].[YearsInService]([DateEngaged],getdate()) >= 10 THEN 150 ELSE -1 END 
8

Bạn đang sử dụng Simple Case statement nơi biểu thức logic không được phép. Bạn cần sử dụng Searched CASE expression. Nhưng trong trường hợp của bạn kể từ khi bạn đang sử dụng một chức năng nó sẽ có chút tốn kém để có được giá trị trả về từ chức năng cho mỗi biểu thức.

Here is MSDN Link for both Simple Case and Searched CASE Syntax

tôi sẽ đề nghị bạn sử dụng một sub query with a Searched case như dưới đây.

select case when results = 0 then 0 
      when results = 1 then 4 
      ... 
      when results >= 10 then 150 
      else -1 end as CaseResults 
from (select [dbo].[YearsInService]([DateEngaged],getdate()) results 
     from yourTable 
    ) Temp 
1

từ http://msdn.microsoft.com/en-us/library/ms181765.aspx

SELECT 
    CASE 
    WHEN MIN(value) <= 0 THEN 0 
    WHEN MAX(1/value) >= 100 THEN 1 
    END 
FROM Data 

bạn có thể sử dụng bất kỳ biểu thức boolean trong mệnh đề KHI

case 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 0) then (0) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 1) then (4) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 2) then (8) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 3) then (12) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 4) then (32) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 5) then (40) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 6) then (48) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 7) then (56) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 8) then (104) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) = 9) then (117) 
    when ([dbo].[YearsInService]([DateEngaged],getdate()) >= 10) then (150) 
    else (-1) end 

bạn nên lưu [dbo]. [YearsInService] ([DateEngaged], getdate()) trong biến trước khi thoa.

1

Giả sử (từ bình luận của bạn) rằng khi DateEngagedNULL, nó gây ra YearsInServiceNULL, sau đó tôi muốn loại bỏ ELSE khoản hiện tại của bạn, và sau đó sử dụng cho all other cases, một cái gì đó như:

case COALESCE([dbo].[YearsInService]([DateEngaged],getdate()),-1) 
when (-1) then (-1) 
when (0) then (0) 
when (1) then (4) 
when (2) then (8) 
when (3) then (12) 
when (4) then (32) 
when (5) then (40) 
when (6) then (48) 
when (7) then (56) 
when (8) then (104) 
when (9) then (117) 
else (150) end 

Nếu có sự lo ngại về giá trị trong tương lai của ngày DateEngaged, tôi sẽ xử lý điều đó bên trong số YearsInService thay vì cố xử lý nó trong biểu thức CASE.

1

Tôi thích @kaf 's câu trả lời, chỉ muốn thêm rằng bạn có thể giảm số lượng các trường hợp của thành viên này

select case when results BETWEEN 0 AND 3 then results * 4 
      when results BETWEEN 4 AND 7 then results * 8 
      when results BETWEEN 8 AND 9 then results * 13 
      when results >= 10 then 150 
      else -1 
     end as CaseResults 
from (select [dbo].[YearsInService]([DateEngaged],getdate()) results 
     from yourTable 
    ) Temp 
0
case [dbo].[YearsInService]([DateEngaged],getdate()) 
    when (0) then (0) 
    when (1) then (4) 
    when (2) then (8) 
    when (3) then (12) 
    when (4) then (32) 
    when (5) then (40) 
    when (6) then (48) 
    when (7) then (56) 
    when (8) then (104) 
    when (9) then (117) 
    when IIF(case [dbo].[YearsInService]([DateEngaged],getdate()) >= 10 
      ,[dbo].[YearsInService]([DateEngaged],getdate()) 
      ,10) 
     then (150) 
    else (-1) end 

này tương đương với tất cả các trường hợp trên 10 với giá trị của hàm.

Các vấn đề liên quan