简体   繁体   中英

Case expression inside of a case expression

I am using a LIMS database software and trying to simplify things for our analysts.

Originally I had this direct case statement that works great for each "test" we use

CASE

WHEN ([Dry Fertilizer].[Phosphorus ICP (< 6.99 Guar)].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.67)) 

THEN -1 ELSE 0

END

Another one

CASE
    WHEN ([Dry Fertilizer].[Phosphorus ICP (10-13.99 Guar)].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.69)) THEN -1
    ELSE 0
END

It's purpose is to look at test result [Phosphorus], compare to a known number [Guarantee] and then calculate and output "-1" if it is below and thus fails or "0" and passes.

Now instead of having a different test for each "guarantee" I am looking at taking all those numbers and making a fairly large calculation but my software just hangs when I try to run the calculation. What can I do to simplify this code or make it work? Here is the code:

CASE    When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =4) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.67)) THEN -1 ELSE 0 End

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =5) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.67)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =6) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.67)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =7) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.68)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =8) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.68)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =9) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.68)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =10) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.69)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =12) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.69)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =14) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.7)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =16) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.7)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =18) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.71)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =20) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.72)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =22) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.72)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =24) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.73)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =26) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.73)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =28) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.74)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =30) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.75)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =32) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.76)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =34) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.794)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =36) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.828)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =38) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.862)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =40) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.896)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =42) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.93)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =44) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.964)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =46) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.998)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =48) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-1.032)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =50) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-1.066)) THEN -1 ELSE 0 End 

When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =52) Then
    Case When ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-1.1)) THEN -1 ELSE 0 End 


End

Why do you need a nested CASE, you could simply use AND

 SELECT CASE When ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee] =4) AND ([Dry Fertilizer].[Phosphorus ICP].[Phosphorus] < ([Dry Fertilizer].[Phosphorus Guarantee].[Phosphorus Guarantee]-0.67)) THEN -1 
 WHEN
 WHEN
 WHEN ETC
 ELSE 0 END AS [YOURCOL]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM