我有两个单独的查询,我写的并试图合并,但这对我来说太复杂了。第一个查询是一个子查询,它返回经销商编号列表,没有任何重复的数字。第二个查询返回经销商编号列表,其中包含多个重复的经销商编号和经销商余额。我需要查看第一个查询中的经销商编号出现在第二个查询中的次数。目前,我将查询结果复制并粘贴到Excel中,然后使用Countif功能查看第一个查询中的经销商在第二个查询中出现的次数。
有没有办法在SQL中执行整个过程?
查询1
SELECT DISTINCT(X.PARENTS) AS "UNIQUE PARENTS" FROM ( SELECT
CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR END AS "PARENTS",
CASE WHEN ((SUM(decode(CUSTMR_COLLAT_TYPE_DESCR,'New Car',1,0)) ) ) <> 0 THEN 'Y' ELSE 'N' END AS "FRANCHISE"
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL AND EXTRCT_FILE_DT = '30-JUN-2017'
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR END, CUSTMR_NBR
) X
WHERE X.FRANCHISE = 'N'
查询2
SELECT CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR END AS "DUPLICATE PARENTS",
SUM(CURR_PRINCPL_AMT)
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL
AND EXTRCT_FILE_DT = '30-JUN-2017'
AND (CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR END <>CUSTMR_NBR)
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR END, CUSTMR_NBR
答案 0 :(得分:0)
Select Count(PARENT_CUSTMR_NBR) From
(
SELECT DISTINCT(X.PARENTS) AS "UNIQUE PARENTS" FROM ( SELECT
CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR
END AS "PARENTS",
CASE WHEN ((SUM(decode(CUSTMR_COLLAT_TYPE_DESCR,'New Car',1,0)) ) ) <> 0
THEN 'Y' ELSE 'N' END AS "FRANCHISE"
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL AND EXTRCT_FILE_DT = '30-JUN-2017'
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END, CUSTMR_NBR ) X
WHERE X.FRANCHISE = 'N')
Where PARENT_CUSTMR_NBR
IN
(SELECT CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END AS "DUPLICATE PARENTS",
SUM(CURR_PRINCPL_AMT)
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL
AND EXTRCT_FILE_DT = '30-JUN-2017'
AND (CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END <>CUSTMR_NBR)
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END, CUSTMR_NBR)
答案 1 :(得分:0)
我得到了它的工作,感谢你的帮助Suresh!这是最后的查询:
Select Count(DUPPARENTS) From
(SELECT CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END AS "DUPPARENTS"
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL
AND EXTRCT_FILE_DT = '30-JUN-2017'
AND (CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END <>CUSTMR_NBR)
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END, CUSTMR_NBR)
Where (DUPPARENTS) IN
(SELECT DISTINCT(X.UNIPARENTS) FROM ( SELECT
CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE PARENT_CUSTMR_NBR
END AS "UNIPARENTS",
CASE WHEN ((SUM(decode(CUSTMR_COLLAT_TYPE_DESCR,'New Car',1,0)) ) ) <> 0
THEN 'Y' ELSE 'N' END AS "FRANCHISE"
FROM MONTH_END_TABLE
WHERE TRUST_ID IS NOT NULL AND EXTRCT_FILE_DT = '30-JUN-2017'
GROUP BY CASE WHEN PARENT_CUSTMR_NBR IS NULL THEN CUSTMR_NBR ELSE
PARENT_CUSTMR_NBR END, CUSTMR_NBR ) X
WHERE X.FRANCHISE = 'N')