[英]HOW to structure SQL CASE STATEMENT with multiple conditions
I would like to convert the below logic into a SQL query, and i know that it can be acheived using a CASE WHEN THEN ELSE statement , but i am not sure how i can put it together in the query. 我想将以下逻辑转换为SQL查询,并且我知道可以使用CASE WHEN THEN ELSE语句来实现,但是我不确定如何将其组合到查询中。 Please help.
请帮忙。
If point 5 is null then use AG_AGENTS.SALES_AREA_DESC 如果点5为空,则使用AG_AGENTS.SALES_AREA_DESC
The new field can be called as SALES_AREA_DESC_2
The general logic is: 一般逻辑是:
select
CASE WHEN test1 = 'a' THEN 1
WHEN test1 = 'b' THEN 2
WHEN (test1 = 'c') AND (test2 is null) THEN 3
ELSE 0 END as myfield
from mytable
In your case, if I understand your query correctly it is: 就您而言,如果我正确理解您的查询,则为:
CASE WHEN (AG_AGENTS.SALES_AREA_DESC = Dom. - NAT)
AND (ISNULL(PX_PAXWEB.COUNTRY,'') = AUT) THEN PX_PAXWEB.POSTCODE
WHEN (AG_AGENTS.SALES_AREA_DESC = Dom. - NAT)
AND (ISNULL(PX_PAXWEB.COUNTRY,'') <> AUT) THEN vwPxPaxweb.SALES_AREA
WHEN (AG_AGENTS.SALES_AREA_DESC = Dom. - NAT)
AND (PX_PAXWEB.COUNTRY is null) THEN AG_AGENTS.SALES_AREA_DESC
WHEN (AG_AGENTS.SALES_AREA_DESC = Int. – Inbound)
THEN vwPxPaxweb.SALES_AREA
WHEN (AG_AGENTS.SALES_AREA_DESC = Int. – Inbound)
THEN AG_AGENTS.SALES_AREA_DESC
WHEN (AG_AGENTS.SALES_AREA_DESC is null)
OR (Int. is null) OR (Inbound is null)
THEN AG_AGENTS.SALES_AREA_DESC
ELSE AG_AGENTS.SALES_AREA_DESC -- or some other exit value
END as SALES_AREA_DESC_2 END:SALES_AREA_DESC_2
Note: a few thing are not clear from your questions so I had to make assumptions: - Assumed "Dom.", "NAT", etc are fields (bit odd w the dot..) - I'm not sure which fields can be NULL (re: "If point 2 and/or 3 is null ", etc), so I picked all for the last branch of the case. 注意:您的问题尚不清楚,因此我必须做一些假设:-假设“ Dom。”,“ NAT”等是字段(点的奇数点..)-我不确定哪个字段可以为NULL(例如:“如果点2和/或3为空”,等等),所以我为案例的最后一个分支选择了全部。 So you can figure out the rest.
这样您就可以找出其余的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.