SQL Conditional基于表达式

时间:2018-03-28 02:30:36

标签: sql conditional where-clause

我有这个SQL查询。我想添加条件,以便

如果UTLI.gfc_linetransactiontypeidname ='SF',添加一个WHERE子句和UTLI.gfc_accountid ='fooGuid'

t2 AS (
SELECT     
    SUM(UTLI.gfc_numberofunits) AS SubUnits, 
    UTLI.gfc_zoneidname AS SubZone, 
    UTLI.gfc_seasonidname AS SubSeason, 
    UTLI.gfc_linetransactiontypeidname AS SubType, 
    COUNT(UT.gfc_trans_id) as SubCount
FROM          
    Filteredgfc_UnitTransactionsLineItems AS UTLI INNER JOIN Filteredgfc_unittransactions AS UT ON UT.gfc_trans_id = UTLI.gfc_unittransactionsidname
WHERE      
    (UTLI.gfc_seasonidname = @CurrentSeason) AND (UT.gfc_trackingstatusidname IN ('PRONTO','CONTRACT PRINTED','READY TO POST')) 
    AND (UTLI.gfc_linetransactiontypeidname IN ('SBL', 'CF', 'SF'))
GROUP BY UTLI.gfc_zoneidname, UTLI.gfc_seasonidname, UTLI.gfc_linetransactiontypeidname)

你是怎么做到的。我试过像下面那样拆分gfc_linetransactiontypeidname,但这并不会返回任何SF结果。实际数据确实有一些结果。

AND(
(UTLI.gfc_linetransactiontypeidname = 'SF' AND UTLI.gfc_accountid = 'fooGuid' ) 
OR
(UTLI.gfc_linetransactiontypeidname IN ('SBL', 'CF'))
)

我可以利用CASE WHEN或其他东西来做这件事吗?

0 个答案:

没有答案