我有两个类似的 sql 查询,它们只有不同的 where 子句过滤器来提取适当的管理员名称(point_of_contact、admin)。我需要生成结合两个过滤查询结果的结果,但我不确定如何解决这个问题。除了联系人和管理员之外,这些列是相同的。我需要 admin 和 point_of_contact 位于不同的列中。有联系点或管理员,但不能同时保留一个空值。我尝试过递归 sql 和 case 语句,但我遇到了一些麻烦。我将查询分为以下两个简单查询。(DB2 - 不确定版本)
SELECT
DP.DIM_PROJECT_ID,
DP.PROJECT_NAME,
DP.POINT_OF_CONTACT,
DP.FIELD,
DD.YEAR
FROM FACT_Table FAT
RIGHT OUTER JOIN DIM_A AS DA on FAT.DIM_A_ID = DA.DIM_A_ID
INNER JOIN DIM_P DP on DA.DIM_P_ID = DP.DIM_P_ID
INNER JOIN BRIDGE_USER BUP on BUP.BRIDGE_ID = DP.DIM_P_ID
INNER JOIN DIM_DATE DD on DD.DATE_KEY = DA.A_START_DATE_ID
WHERE DA.AWARD_CATEGORY <> 'N/A'
AND DD.YEAR = '2013'
and (
SELECT count(BUP_INNER.ADMIN_FLAG) FROM BRIDGE_USER BUP_INNER
INNER JOIN DIM_P DP_INNER on BUP_INNER.DIM_P_ID = DP_INNER.DIM_P_ID
WHERE DP_INNER.DIM_P_ID = DP.DIM_P_ID
AND BUP_INNER.ADMIN_FLAG = 'Y'
) = 0
GROUP BY DA.AWARD_TYPE_NAME, DA.AWARD_CATEGORY, DP.PROJECT_NAME, DP.POINT_OF_CONTACT, DD.YEAR
和
SELECT distinct
DP.DIM_PROJECT_ID,
DU.NAME_LAST CONCAT ', ' CONCAT t1.NAME_FIRST AS ADMIN,
DP.PROJECT_NAME,
DP.PROJECT_TITLE,
DD.YEAR,
DP.FIELD
FROM FACT_Table as FAT
INNER JOIN DIM_P DP ON FAT.DIM_P_ID = DP.DIM_P_ID
INNER JOIN BRIDGE_USER BUP on DP.DIM_P_ID = BUP.DIM_P_ID
INNER JOIN DIM_USER DU ON FAT.DIM_USER_ID = DU.DIM_USER_ID
INNER JOIN DIM_DATE DD on DD.DATE_KEY = DA.A_START_DATE_ID
INNER JOIN DIM_A DA ON FAT.DIM_A_ID = DA.DIM_A_ID
WHERE BUP.ADMIN_FLAG = 'Y'
and DD.YEAR = '2013'
and DA.AWARD_CATEGORY <> 'NA'
)
GROUP BY
DP.DIM_P_ID,
DU.NAME_LAST CONCAT ', ' CONCAT DU.NAME_FIRST,
DP.PROJECT_NAME,
DP.TITLE,
DD.YEAR,
DP.FIELD