我有以下代码:
([L_D]='mc' and [L_D]='mb' or [L_D]='f1' or [L_D]='fs' or [L_D]='f2' or [L_D]='f3' or [L_D]='ma' or [L_D]='m*' or [L_D]='f*' or [L_D]='**')
如果此记录包含L_D='MC'
和任何其他代码。跳过此记录。然后返回[L_D]='mc'
我在想一个嵌套的'If语句'会起作用。但我的结果不正确。
答案 0 :(得分:1)
我会假设你有以下内容:
Customer
(
id Int IDENTITY,
last_name Varchar(40),
first_name Varchar(30),
)
CustomerEvents
(
id Int IDENTITY,
cust_id Int,
l_d Varchar(2)
)
在这种情况下,(似乎)满足您的问题的查询将如下所示:
SELECT CUST.id AS cust_id,
CUST.first_name,
CUST.last_name,
FROM Customer CUST
INNER JOIN
(
SELECT cust_id,
SUM ( CASE
WHEN l_d = 'mc' THEN 1
ELSE 0
END
) AS mc_count,
SUM( CASE
WHEN l_d in ('mb', 'fl', 'fs', 'f2',
'f3', 'ma', 'm*', 'f*', '**' ) THEN 1
ELSE 0
END
) AS other_count
FROM CustomerEvents
GROUP BY cust_id
) CustScore
ON CustScore.cust_id = CUST.id
WHERE CustScore.mc_count > 0
AND CustScore.other_count = 0