2

我想为没有的 CUST_ID 记录(表 A)选择一个可用的 PROMO_CODE(表 B)。我想我有每个表的基本 SQL,但不知道如何将这些组合起来输出。

-----表A -----

CUST_ID|CREATED_DATE|MY_CODE
XYZ111|1/1/2015|5
XYZ222|1/1/2015|6
XYZ333|7/12/2018|   
XYZ444|7/13/2018|   
XYZ555|7/14/2018|   
XYZ666|2/2/2012|3
XYZ777|2/2/2012|2
XYZ888|7/15/2018|   
XYZ999|6/6/2016|4
XYZ1110|6/6/2016|1

-----表B -----

PROMO_CODE|PROMO_CODE_STATUS
1|assigned
2|used
3|
4|used
5|assigned
6|
7|
8|assigned
9|assigned
10|

----期望的输出-----

CUST_ID|MY_NEW_CODE
XYZ333|3
XYZ444|6
XYZ555|7
XYZ888|10

----到目前为止我有什么但没有连接-----

Select a.CUST_ID, b.PROMO_CODE as MY_NEW_CODE  

(Select a.CUST_ID, a.CREATED_DATE, a.MY_CODE
from $A$ a where 
a.MY_CODE is NULL AND trunc(a.CREATED_DATE) >= trunc(sysdate -1))

(Select b.PROMO_CODE, b.PROMO_CODE_STATUS
from $B$ b where 
b.PROMO_CODE_STATUS is NULL)
4

2 回答 2

0

我最终在每个表上使用了 ROW_NUMBER() 并且有效:

SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY a.CUST_ID ASC) AS Row_CUST_ID, a.CUST_ID, a.MY_NEW_CODE FROM $A$ a 其中 a.MY_NEW_CODE 为 NULL 并且 trunc(a.LOADED_DATE) >= trunc(sysdate -1)) c INNER JOIN (SELECT ROW_NUMBER() OVER(ORDER BY b.PROMO_CODE ASC) AS Row_PROMO_CODE, b.PROMO_CODE, b.PROMO_CODE_STATUS FROM $B$ b 其中 b.PROMO_CODE_STATUS 为 NULL) p ON c.Row_CUST_ID = p .Row_PROMO_CODE


它产生预期的结果:

ROW_CUST_ID|CUST_ID|MY_NEW_CODE|ROW_PROMO_CODE|PROMO_CODE|PROMO_CODE_STATUS

1 | 111 | | 1 | 血脑屏障 |
2 | 222 | | 2 | DDD |
3 | 333 | | 3 | FFF |

从更新的表格中:

-----表A

CUST_ID | MY_NEW_CODE | LOADED_DATE

111 | | 2018-07-18 00:00:00.0
222 | | 2018-07-18 00:00:00.0
333 | | 2018-07-19 00:00:00.0
444 | | 2018-07-12 00:00:00.0

-----表B

促销代码 | PROMO_CODE_STATUS

AAA | 使用
BBB |
CCC | 分配
DDD |
电子电气设备 | 分配
FFF |

于 2018-07-19T04:35:39.107 回答
0

我会这样想:

Select a.CUST_ID, b.PROMO_CODE as MY_NEW_CODE  
FROM $A$ a, $B$ b
WHERE a.MY_CODE = b.PROMO_CODE 
AND trunc(a.CREATED_DATE) >= trunc(sysdate -1)
AND b.PROMO_CODE_STATUS is NULL
于 2018-07-18T01:54:03.893 回答