Jak zrobić wiele przypadków, gdy warunki za pomocą SQL Server 2008?
Próbuję użyć więcej niż jednego przypadku, gdy warunek dla tej samej kolumny.
Oto Mój kod do zapytania:
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)
Jednak chcę użyć więcej niż jednego, gdy dla tej samej kolumny "qty".
Jak w następującym kodzie:
IF
// CODE
ELSE IF
// CODE
ELSE IF
// CODE
ELSE
// CODE
8 answers
Istnieją dwa formaty wyrażenia case . Możesz zrobić CASE
z wieloma WHEN
jako;
CASE WHEN Col1 = 1 OR Col3 = 1 THEN 1
WHEN Col1 = 2 THEN 2
...
ELSE 0 END as Qty
lub proste CASE
wyrażenie
CASE Col1 WHEN 1 THEN 11 WHEN 2 THEN 21 ELSE 13 END
lub CASE
wewnątrz CASE
as;
CASE WHEN Col1 < 2 THEN
CASE Col2 WHEN 'X' THEN 10 ELSE 11 END
WHEN Col1 = 2 THEN 2
...
ELSE 0 END as Qty
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-13 12:30:46
Po prostu użyj tego, musisz użyć więcej, gdy są klasami.
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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-29 15:31:40
Możesz użyć poniższego przykładu przypadku, gdy z wieloma warunkami.
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-23 12:25:09
Może to być skuteczny sposób wykonywania różnych testów na jednym oświadczeniu
select
case colour_txt
when 'red' then 5
when 'green' then 4
when 'orange' then 3
else 0
end as Pass_Flag
To działa tylko na porównaniach równości!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-31 16:57:40
Miałem podobny, ale miał do czynienia z datami. Zapytanie, aby pokazać wszystkie pozycje z ostatniego miesiąca, działa świetnie bez warunków do Sty. Aby to działało poprawnie, należy dodać zmienną rok i miesiąc
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)
Teraz dodaję zmienną do warunku: ...
(year(CreationTime)=@yr and MONTH(creationtime)=@mth)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-09 15:13:54
Łączenie wszystkich warunków
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 )
)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-21 11:27:35