如何连接两个查询结果并根据条件过滤掉数据?
How to join two query result and filter out the data based on the condition?
我是 运行 两个 sql 正在提供一些输出的查询。我想把它们结合起来,根据需求筛选出结果。
说明-
查询 1:
select `Project Number` from vw_onco_pharma onco_pharma union select `Project Number` from vw_onco_cell_gene cell_gene union select `Project Number` from vw_non_onco_cell_gene onco_cell_gene union select `Project Number` from vw_non_onco_pharma non_onco_pharma union select `Project Number` from vw_plasma_protein plasma_protein
输出 1:
Project Number
S100
S100
S200
S300
S300
S300
S400
S400
查询 2:
select Project Number
来自 FCT_HTA_ONC_NONONC_PGMS;
输出 2:
Project Number
S100
S200
S200
S300
S300
S300
S500
现在我必须将 Output1 和 Output2 转换为以下格式:
Output 1 : Output 2 :
Project Number Count Project Number Count
S100 2 S100 1
S200 1 S200 2
S300 3 S300 3
S400 2 S500 1
现在匹配输出 1 和输出 2 并打印差异-
最终输出:
Project Number
S100
S200
S400
S500
WITH
-- first query, UNION ALL used instead of single UNION
cte1 AS (select `Project Number` from vw_onco_pharma onco_pharma
union all
select `Project Number` from vw_onco_cell_gene cell_gene
union all
select `Project Number` from vw_non_onco_cell_gene onco_cell_gene
union all
select `Project Number` from vw_non_onco_pharma non_onco_pharma
union all
select `Project Number` from vw_plasma_protein plasma_protein),
-- count the amounts for 1st query
cte2 AS (SELECT `Project Number`, COUNT(*) cnt
FROM cte1
GROUP BY `Project Number`),
-- 2nd query, amounts counting added
cte3 AS (select `Project Number`, COUNT(*) cnt
from FCT_HTA_ONC_NONONC_PGMS
GROUP BY `Project Number`),
-- gathering all projects
cte4 AS (SELECT `Project Number`
FROM cte1
UNION
SELECT `Project Number`
FROM cte3)
-- get final data
SELECT `Project Number`
FROM cte4
LEFT JOIN cte2 USING (`Project Number`)
LEFT JOIN cte4 USING (`Project Number`)
WHERE cte2.cnt IS NULL
OR cte4.cnt IS NULL
OR cte2.cnt <> cte4.cnt;
我是 运行 两个 sql 正在提供一些输出的查询。我想把它们结合起来,根据需求筛选出结果。
说明-
查询 1:
select `Project Number` from vw_onco_pharma onco_pharma union select `Project Number` from vw_onco_cell_gene cell_gene union select `Project Number` from vw_non_onco_cell_gene onco_cell_gene union select `Project Number` from vw_non_onco_pharma non_onco_pharma union select `Project Number` from vw_plasma_protein plasma_protein
输出 1:
Project Number
S100
S100
S200
S300
S300
S300
S400
S400
查询 2:
select Project Number
来自 FCT_HTA_ONC_NONONC_PGMS;
输出 2:
Project Number
S100
S200
S200
S300
S300
S300
S500
现在我必须将 Output1 和 Output2 转换为以下格式:
Output 1 : Output 2 :
Project Number Count Project Number Count
S100 2 S100 1
S200 1 S200 2
S300 3 S300 3
S400 2 S500 1
现在匹配输出 1 和输出 2 并打印差异-
最终输出:
Project Number
S100
S200
S400
S500
WITH
-- first query, UNION ALL used instead of single UNION
cte1 AS (select `Project Number` from vw_onco_pharma onco_pharma
union all
select `Project Number` from vw_onco_cell_gene cell_gene
union all
select `Project Number` from vw_non_onco_cell_gene onco_cell_gene
union all
select `Project Number` from vw_non_onco_pharma non_onco_pharma
union all
select `Project Number` from vw_plasma_protein plasma_protein),
-- count the amounts for 1st query
cte2 AS (SELECT `Project Number`, COUNT(*) cnt
FROM cte1
GROUP BY `Project Number`),
-- 2nd query, amounts counting added
cte3 AS (select `Project Number`, COUNT(*) cnt
from FCT_HTA_ONC_NONONC_PGMS
GROUP BY `Project Number`),
-- gathering all projects
cte4 AS (SELECT `Project Number`
FROM cte1
UNION
SELECT `Project Number`
FROM cte3)
-- get final data
SELECT `Project Number`
FROM cte4
LEFT JOIN cte2 USING (`Project Number`)
LEFT JOIN cte4 USING (`Project Number`)
WHERE cte2.cnt IS NULL
OR cte4.cnt IS NULL
OR cte2.cnt <> cte4.cnt;