使用IF语句提取条目的布尔列标题

时间:2018-07-19 11:48:42

标签: sql if-statement boolean

我对IF语句一点也不擅长。我目前有一个时间表,如下所示:

Lot(Int) PartNum(Varchar50) Amount(Int) IsPainted(Bool) IsInspected(Bool) Finished(Bool)
1         xxx-0191          500          1               1                 0
2         xxx-0191          700          1               0                 0

我要完成的工作,而且我认为必须由IF语句处理,但我肯定愿意使用在这里最有效的方法进行查询我以下

Lot  PartNum  Amount  Status
1    xxx-0191 500     Inspected
2    xxx-0191 700     Painted

我需要做的就是在布尔值列中拉出“ True”或“ 1”的最后一个可用列,并在查询的“状态”列中显示该信息。

2 个答案:

答案 0 :(得分:1)

使用case。像这样:

select lot, partnum, amount,
       (case when Finished = 1 then 'Finished' 
             when IsInspected = 1 then 'Inspected'
             when IsPainted  = 1 then 'Painted' 
       ) as status

这将选择最后一个布尔作为状态选择的布尔。

答案 1 :(得分:1)

使用此代码

select 
    Lot, 
    PartNum, 
    Aamount, 
    Case when IsInspected=1 then 'Inspected' else 'Painted' end Status
from
    table