我是SQL的新手,我在解决问题时遇到了一些麻烦。我正在重写一个脚本,我可以与收到电子邮件的客户进行匹配。以前我一直在使用会话表,如下所示:
DECLARE GLOBAL TEMPORARY TABLE EMAIL_SENDS
(
ACCOUNT_NUMBER INT
)
WITH REPLACE
ON COMMIT PRESERVE ROWS
;
INSERT INTO SESSION.EMAIL_SENDS
SELECT ACCOUNT_NUMBER
FROM SENT_DATA A
JOIN MAIN_TABLE B
ON A.CLIENT_ID_SUBKEY = B.CUST_ID
OR A.CLIENT_ID_SUBKEY = B.OLD_ID
WHERE SEND_ID = 123456
GROUP BY ACCOUNT_NUMBER
;
然后在提取数据时,我刚刚使用了CASE,而且C.ACCOUNT_NUMBER不为空,那么1 0结束加入MAIN_TABLE与EMAIL_SENDS标记谁已经收到电子邮件,但没有收到电子邮件。
但是我想改进这个过程,并在MAIN_TABLE中添加一个列,说明谁收到了一封电子邮件,但是没有收到。
到目前为止,这是我的脚本:
ALTER TABLE MAIN_TABLE
ADD EMAILED INT
;
INSERT INTO MAIN_TABLE
SELECT EMAILED
CASE WHEN ACCOUNT_NUMBER IS NOT NULL THEN 1 ELSE 0 END
FROM (SENT_DATA A
JOIN MAIN_TABLE B
ON A.CLIENT_ID_SUBKEY = B.CUST_ID
OR A.CLIENT_ID_SUBKEY = B.OLD_ID
WHERE SEND_ID = 123456
GROUP BY ACCOUNT_NUMBER
)
;
由于我是SQL的新手,我无法弄清楚我出错的地方(尽管对于精通它的人来说这可能是显而易见的。)
答案 0 :(得分:0)
试试这个。将主要表格中的列名称替换为(EMAILED,ACCOUNT_NUMBER)
INSERT INTO MAIN_TABLE (EMAILED,ACCOUNT_NUMBER)
SELECT EMAILED,
CASE WHEN ACCOUNT_NUMBER IS NOT NULL THEN 1 ELSE 0 END
FROM (SENT_DATA A
JOIN MAIN_TABLE B
ON A.CLIENT_ID_SUBKEY = B.CUST_ID
OR A.CLIENT_ID_SUBKEY = B.OLD_ID
WHERE SEND_ID = 123456
GROUP BY ACCOUNT_NUMBER
)
;