私がやろうとしているのは、ウィジェットを購入するのが初めてである限り、前日にSeries X
またはウィジェットを購入したすべての顧客を見つけることです。Series A
彼らが複数の適格なウィジェットを購入した場合、Series X
ランクが付けられ、私はそれの記録のみを取り戻す必要があります。うまくいけば、それは理にかなっているので、ここにデータがあります
[customers]
c_id cp_id
--------------
1 cp1
2 cp2
3 cp3
4 cp4
5 cp5
6 cp6
7 cp7
8 cp8
9 cp9
10 cp10
[widget_orders - c_id maps to c_id in customers]
c_id w_sku o_date
----------------------
1 w1 2012-10-10
2 w1 2012-10-10
2 w2 2012-10-10
3 w1 2012-10-10
3 w2 2012-10-10
4 w1 2012-10-10
4 w2 2012-10-10
5 w1 2012-10-10
5 w2 2012-10-10
6 w1 2012-10-10
6 w2 2012-10-10
7 w2 2012-10-10
8 w1 2012-10-10
9 w3 2012-10-10
[widgets - w_sku maps to w_sku in widget_orders]
w_sku w_series
------------------
w1 Series A
w2 Series X
w3 Series C
[customer_data - c_id maps to c_id in customers]
cp_id seriesA_fPurch seriesX_fPurch
--------------------------------------
cp3
cp4 2012-09-15
cp5 2012-09-15
cp6 2012-09-15 2012-09-15
cp7 2012-09-15
cp8 2012-09-15
そして、これが()の説明を無視して取り戻したいデータです
cp_id series
--------------
cp1 Series A (bought series A and had NO prior purchase history)
cp2 Series X (bought both series, but X has rank - no purchase history)
cp3 Series X (bought both series, but X has rank - has purchase history recors albeit not for these)
cp4 Series X (bought both series, but X has rank - already had A history anyways)
cp5 Series A (bought both series, although X has rank they had previously bought series X)
次の人は結果に表示されません
cp6 - they had previously bought both series
cp7 - bought a series x, but had in the past
cp8 - bought a series a, but had in the past
cp9 - bought a widget in neither series
cp10 - didnt buy anything
それがすべて理にかなっていて、誰かがここで私を助けてくれることを願っています!
したがって、ロジックを要約し、おそらくもう少し明確に定義するために、ここでは、ステップのような方法で発生する必要があることを説明する方法を説明します。
1) Find all customers who have no matching records in the customer_data table
2) Find all customers who have a null value in either *purch column in the customer_data table
3) Combine these results together
4) Take the results and find the customers who made a purchase yesterday
5) Take the results and find the customers who purchased Series A or Series X
6) Take the results and do the following
6a) If the purchase was Series A and they have a value for series A purch already drop them from results
6b) If the purchase was Series X and they have a value for series X purch already drop them from results
7) Take the results and remove duplicate records based on the cp_id - Series X takes presedence over Series A