如果两列都不显示数据的情况

时间:2013-10-04 15:18:50

标签: sql database sql-server-2008

我有两列IsHomeDelivery和Ispurchase,其值可以是0和1.

当值为1时,我必须显示文字。

表示ishomedelevery = 1然后'homedelivery ispurchase = 1然后'购买'

我已形成以下查询:

SELECT
        LocationName,
        BranchName,
        AddLine1,
        AddLine2,
        Tel1,
        CASE
            WHEN LocationType='CC' THEN 'Call Center'
            WHEN LocationType='CL' THEN 'Operation'
        END AS 'Location Type',
        CASE
            WHEN IsPurchase= 1 THEN 'Purchase'
            WHEN IsHomeDelivery = 1 THEN 'Home Delivery'
            WHEN IsPurchase=1 AND IsHomeDelivery=1 THEN 'Purchase And Home Delevery'
        END AS 'Type'
    FROM PescaLocation

但不幸的是,它不适用于两个列都是真的。

意味着何时isHomedelivery和isPurchase都是1然后它显示'购买'而它应该显示'送货上门和购买'

请帮助我。

1 个答案:

答案 0 :(得分:2)

你在第二种情况下错过了一个条件。当IsPurchase和IsHomeDelivery都为1时,“当IsPurchase = 1时”将为真。

这是更新的代码。

select LocationName
    ,BranchName
    ,AddLine1
    ,AddLine2
    ,Tel1
    ,case 
        when LocationType = 'CC'
            then 'Call Center'
        when LocationType = 'CL'
            then 'Operation'
        end as 'Location Type'
    ,case 
        when IsPurchase = 1 and IsHomeDelivery = 0
            then 'Purchase'
        when IsPurchase = 0 and IsHomeDelivery = 1
            then 'Home Delivery'
        when IsPurchase = 1 and IsHomeDelivery = 1
            then 'Purchase And Home Delevery'
        end as 'Type'
from PescaLocation