基本上我想根据他们出现的其他表格对我的客户进行分类。我怀疑这种语法是远程正确的,但希望这个要点可以通过:
SELECT customer_id,
CASE customer_id
WHEN [customer_id is in tableA] THEN 'customer_segment_1'
WHEN [customer_id is in tableB] THEN 'customer_segment_2'
ELSE 'customer_segment_3' END
AS 'customer_segment'
FROM customers
答案 0 :(得分:1)
确定" customer_id
是否在tableA
"您只需要加入该表格,使用left outer
来计算tableA
中不的ID:
SELECT tableC.customer_id,
CASE
WHEN tableA.customer_id is not null THEN 'customer_segment_1'
WHEN tableB.customer_id is not null THEN 'customer_segment_2'
ELSE 'customer_segment_3' END
AS customer_segment
FROM tableC
LEFT OUTER JOIN tableA on tableC.customer_id = tableA.customer_id
LEFT OUTER JOIN tableB on tableC.customer_id = tableB.customer_id
答案 1 :(得分:0)
类似的东西:
SELECT tableC.customer_id,
CASE WHEN tableA.customer_id IS NOT NULL THEN 'SegmentA'
WHEN tableB.customer_id IS NOT NULL THEN 'SegmentB' ELSE 'SegmentC' END CASE AS Segment
FROM tableC LEFT JOIN tableA ON tableC.customer_id=tableA.customer_id
LEFT JOIN tableB ON tableC.customer_id=tableB.customer_id