Oracle/SQL-多表联接逻辑-不确定如何表达它
我要做的是找到所有在前一天购买了Oracle/SQL-多表联接逻辑-不确定如何表达它,sql,oracle,Sql,Oracle,我要做的是找到所有在前一天购买了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
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
下面是我想要得到的数据,忽略()
以下人员未显示在结果中
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
我不确定我是否完全理解您的要求,但请尝试:
SELECT cp_id, w_series
FROM (
SELECT rank() over (partition BY wo."c_id" ORDER BY decode(w."w_series",'Series X',1,'Series A',2)) rank,
wo."c_id" c_id,
c."cp_id" cp_id,
w."w_series" w_series
FROM widget_orders wo JOIN widgets w ON wo."w_sku"=w."w_sku"
JOIN customers c on c."c_id"=wo."c_id"
LEFT OUTER JOIN customer_data cd ON c."cp_id" = cd."cp_id"
WHERE w."w_series" IN ('Series A', 'Series X')
AND trunc(wo."o_date") = trunc(sysdate)-1
AND ( (cd."seriesA_fPurch" IS NULL AND w."w_series"='Series A')
OR (cd."seriesX_fPurch" IS NULL AND w."w_series"='Series X'))
)
WHERE rank = 1
他是一把小提琴
说明:根据步骤式编号-
1) +2)是在
左外连接
+cd中完成的。“seriesA_fpourch”为空
条件+cd。“seriex_fpourch”为空
条件,因为它也会找到那些没有记录的人,并将其置为空。3) 显而易见……
4)
trunc(wo.“o_日期”)=trunc(sysdate)-1
condition5)
w('series A'、'series X')中的“w_series”
6)
(cd.“seriesA_fpourch”为空,w.“w_series”='series A')
或(cd.“seriexx_fpourch”为空,w.“w_series”='series X')
条件7) 通过给记录赋予一个等级,并根据小提琴给出等级=1的条件,看起来这是可行的,但我确实有点说错了。customers表连接到另一个字段上的customer_数据表。您的查询似乎没有利用customer表,但最终需要这样做。我现在正在更新这个问题。@dscl,我认为你的句子应该以“感谢你努力理解我的场景,创建表格,并用数据填充它,使之成为废话…”开头。只有通过这份出色的工作,A.B.Cade才能获得+1。@danihp你绝对正确!感谢A.B.Cade关注我的问题并解决它。如果你能再看一眼,我当然会很感激。如果你不能,我理解并且仍然感激你已经付出的努力。@dscl,我更新了我的答案,在
客户
表中添加了另一个联接,并将左外部联接
条件更改为映射到客户。我补充的另一件事是只提及银行A.B.Cade的“昨天”日期的条件-你不知道我有多感激它!
SELECT cp_id, w_series
FROM (
SELECT rank() over (partition BY wo."c_id" ORDER BY decode(w."w_series",'Series X',1,'Series A',2)) rank,
wo."c_id" c_id,
c."cp_id" cp_id,
w."w_series" w_series
FROM widget_orders wo JOIN widgets w ON wo."w_sku"=w."w_sku"
JOIN customers c on c."c_id"=wo."c_id"
LEFT OUTER JOIN customer_data cd ON c."cp_id" = cd."cp_id"
WHERE w."w_series" IN ('Series A', 'Series X')
AND trunc(wo."o_date") = trunc(sysdate)-1
AND ( (cd."seriesA_fPurch" IS NULL AND w."w_series"='Series A')
OR (cd."seriesX_fPurch" IS NULL AND w."w_series"='Series X'))
)
WHERE rank = 1