sql server - Except Operator T-SQL -
task: display c_id , c_name customers have @ least 1 purchase in 2 of months not in 3 months.
first month = november second month = december third month = january select c_id, c_name books month(order_date) = 11 intersect select c_id, c_name books month(order_date) = 12 except select c_id, c_name books month(order_date) = 1
the current code have negates 3rd month doesn't fulfill task asking select 2 months interchangeably.
rule is: no count, no subquery, no join(s)
i managed come this.
select c_id,c_name books month(order_date) = 1 intersect select c_id,c_name books month(order_date) = 11 union select c_id,c_name books month(order_date) = 1 intersect select c_id,c_name books month(order_date) = 12 union select c_id,c_name books month(order_date) = 12 intersect select c_id,c_name books month(order_date) = 11 except select c_id,c_name books month(order_date) = 1 intersect select c_id,c_name books month(order_date) = 11 intersect select c_id,c_name books month(order_date) = 12
this equivalent expression:
(set intersect set b) union (set intersect set c) union (set c intersect set b) minus (set intersect set b intersect set c)
,
or can see in image, regions ab, ac , bc
this not elegant, given requirements, seems brute force valid approach.
Comments
Post a Comment