1

任何人都可以帮助我使用 SQL Plus 的“case”语句的正确语法吗?

Select 
sum(case "REQUEST AGE" when >= 60 then 1 else 0) as "60+ Days",
sum(case "REQUEST AGE"  when >= 30 and case when "REQUEST AGE" < 60 then 1 else 0) as "30-    60 Days",
sum (case "REQUEST AGE" when < 30 then 1 else 0) as "Less Than 30 Days",
"SECTOR"
FROM Schema.APPDATA
Where Schema.APPDATA."SECTOR" like '%X%'

我对 SQL 比较陌生,而且我从来没有写过这样的查询。我想要列请求年龄大于 60、介于 30 和 60 之间且小于 30 的情况的总和,因为 3 列按列扇区分组

4

2 回答 2

1

每个CASE语句都必须END(). 您可以在CASE WHEN <condition> THEN此处使用语法:

Select 
  sum(case when "REQUEST AGE" >= 60 then 1 else 0 END) as "60+ Days",
  /* And in here, don't repeat the CASE keyword. Both parts of the expression are connected by AND */
  sum(case when "REQUEST AGE" >= 30 and "REQUEST AGE" < 60 then 1 else 0 END) as "30-    60 Days",
  sum (case when "REQUEST AGE" < 30 then 1 else 0 END) as "Less Than 30 Days",
  "SECTOR"
FROM Schema.APPDATA
Where Schema.APPDATA."SECTOR" like '%X%'
于 2012-12-10T21:26:00.040 回答
1

您需要使用CASE WHEN [expression]... THEN而不是CASE [variable] WHEN [value] THEN ...

Select 
sum(case WHEN "REQUEST AGE" >= 60 then 1 else 0 end) as "60+ Days",
....

此外,在您的情况下,这似乎countsum您根本不需要更合适else

于 2012-12-10T21:27:05.907 回答