我希望添加' if else' where子句中的条件,但不知道我该怎么做。查看下面的表集,我试图过滤查询将提取所有产品系列子类型的结果,并仅在 [Product Line Subtype] =' Marine' 时添加条件STRONG>。当它是Marine时,它应该只考虑 Section 和 Profit Code 的两种组合,同时省略其他组合。
组合1 当Prod line Subtype = Marine时,Section = Inland和Profit Code = Builders
组合2 当Prod line Subtype = Marine时,Section = Ocean and Profit Code = Stocks
当Prod line Subtype = Marine时,我的实际表具有比下表所示更大的不同组合集,但我只想过滤上述两种组合到我的结果集。任何帮助将不胜感激!
主要表格
+--+------------------+-------------+-----------------+
| |Prod line Subtype | Section | Profit Code |
+--+------------------+-------------+-----------------+
| | Marine | Inland | Builders |
| | Marine | Ocean | Stock |
| | Property | General | Transport |
| | Energy | Source | Others |
| | Property | General | Transport |
| | Energy | Source | Transport |
| | Marine | Inland | Transport |
| | Marine | Floaters | Transport |
| | Marine | Cargo | Others |
+--+------------------+-------------+---------------- +
预期结果
+--+------------------+-------------+-----------------+
| |Prod line Subtype | Section | Profit Code |
+--+------------------+-------------+-----------------+
| | Marine | Inland | Builders |
| | Marine | Ocean | Stock |
| | Property | General | Transport |
| | Energy | Source | Others |
| | Property | General | Transport |
| | Energy | Source | Transport |
+--+------------------+-------------+---------------- +
我的查询尝试:
select *
from #Step1
where c1.row_ord = 1
and c1.[Prod Line Subtype] = 'Marine' AND (
(c1.[Section] = 'Inland' AND c1.[Profit Code] = 'Builder')
OR (c1.[Section] = 'Ocean' AND c1.[Profit Code] = 'Stock')
)
答案 0 :(得分:1)
怎么样:
Select * from [Your table]
Where ([Prod line Subtype]<>'Marine' Or
(Section='Inland' And [Profit Code]='Builders') Or
(Section='Ocean' And [Profit Code]='Stocks')
)
可以省略[Prod line Subtype] ='Marine'或条件