Como faço vários casos quando as condições usam o SQL Server 2008?
o que eu estou a tentar fazer é usar mais do que um caso quando condição para a mesma coluna.
Aqui está o meu código para a consulta:
SELECT Url='',
p.ArtNo,
p.[Description],
p.Specification,
CASE
WHEN 1 = 1 or 1 = 1
THEN 1
ELSE 0
END as Qty,
p.NetPrice,
[Status] = 0
FROM Product p (NOLOCK)
No entanto, o que eu quero fazer é usar mais de um quando para a mesma coluna "qty".
como no seguinte código:
IF
// CODE
ELSE IF
// CODE
ELSE IF
// CODE
ELSE
// CODE
126
Author: screechOwl, 2013-01-31
8 answers
Existem dois formatos de expressão de Caso . Você pode fazer CASE
com muitos WHEN
Como;
CASE WHEN Col1 = 1 OR Col3 = 1 THEN 1
WHEN Col1 = 2 THEN 2
...
ELSE 0 END as Qty
quer {[15] } Uma expressão simples CASE
CASE Col1 WHEN 1 THEN 11 WHEN 2 THEN 21 ELSE 13 END
Ou CASE
dentro CASE
como;
CASE WHEN Col1 < 2 THEN
CASE Col2 WHEN 'X' THEN 10 ELSE 11 END
WHEN Col1 = 2 THEN 2
...
ELSE 0 END as Qty
265
Author: Kaf, 2017-06-13 12:30:46
Basta usar este, você tem que usar mais quando são aulas.
SELECT Url='',
p.ArtNo,
p.[Description],
p.Specification,
CASE
WHEN 1 = 1 or 1 = 1
THEN 1
WHEN 2 = 2
THEN 2
WHEN 3 = 3
THEN 3
ELSE 0
END as Qty,
p.NetPrice,
[Status] = 0
FROM Product p (NOLOCK)
9
Author: Shankar, 2016-06-29 15:31:40
Pode usar abaixo do exemplo de caso quando tiver várias condições.
SELECT
id,stud_name,
CASE
WHEN marks <= 40 THEN 'Bad'
WHEN (marks >= 40 AND
marks <= 100) THEN 'good'
ELSE 'best'
END AS Grade
FROM Result
3
Author: Abhijeet Navgire, 2018-03-23 12:25:09
Esta pode ser uma forma eficiente de realizar testes diferentes numa única declaração
select
case colour_txt
when 'red' then 5
when 'green' then 4
when 'orange' then 3
else 0
end as Pass_Flag
Isto só funciona com comparações de igualdade!
2
Author: user2082785, 2017-09-22 01:15:03
case
when a.REASONID in ('02','03','04','05','06') then
case b.CALSOC
when '1' then 'yes'
when '2' then 'no'
else 'no'
end
else 'no'
end
1
Author: wennykikkok, 2015-03-17 17:17:35
case when first_condition
then first_condition_result_true
else
case when second_condition
then second_condition_result_true
else
second_condition_result_false
end
end
end as qty
0
Author: Jimoc, 2013-01-31 16:57:40
Eu tinha um similar, mas tratava-se de datas.
Consulta para mostrar todos os itens para o último mês, funciona muito bem sem condições até Janeiro.
Para que funcione correctamente, é necessário adicionar uma variável de ano e mês
declare @yr int
declare @mth int
set @yr=(select case when month(getdate())=1 then YEAR(getdate())-1 else YEAR(getdate())end)
set @mth=(select case when month(getdate())=1 then month(getdate())+11 else month(getdate())end)
Agora só adiciono a variável à condição.:
...
(year(CreationTime)=@yr and MONTH(creationtime)=@mth)
0
Author: Douglas Bentley, 2014-01-09 15:13:54
Combinação de todas as condições
select a.* from tbl_Company a
where a.Company_ID NOT IN (1,2)
AND (
(0 =
CASE WHEN (@Fromdate = '' or @Todate='')
THEN 0
ELSE 1
END
) -- if 0=0 true , if 0=1 fails (filter only when the fromdate and todate is present)
OR
(a.Created_Date between @Fromdate and @Todate )
)
0
Author: Arun Prasad E S, 2016-07-21 11:27:35