如何将这两个选择查询合并到一个查询中?

时间:2017-03-03 20:05:45

标签: oracle

这是我第一次发帖,所以我相信我会遇到很多错误。不要犹豫,纠正我,我会尽我所能澄清。

在Oracle SQL Developer中,我尝试使用两个单独的SELECT语句并将它们组合起来以获得一行结果。不幸的是,因为这是敏感数据,我无法单独从语句中提供任何结果,而只是SQL语句本身。我怀疑我应该能够加入这两个领域“emplid”,但却无法实现。任何帮助是极大的赞赏!以下是代码,请注意语法:)

1st Select语句为我提供了2017年支付的人员列表:

SELECT DISTINCT C.COMPANY,
 C.EMPLID,
 C.SSN
FROM PS_PAY_CHECK C
WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
AND C.COMPANY                   IN ('001','054','076')
ORDER BY C.COMPANY, C.EMPLID

第二选择声明将是第一份声明中确定的员工扣除清单:

SELECT G.EMPLID, G.DEDCD,
  CASE
   WHEN DC.DED_CLASS IN ('A','B','T')
   THEN G.DED_ADDL_AMT
   ELSE 0
  END AS "EEAmt",
  CASE
   WHEN DC.DED_CLASS NOT IN ('A','B','T')
   THEN G.DED_ADDL_AMT
   ELSE 0
  END AS "ERAmt",
  DC.DED_CLASS,
  G.DED_ADDL_AMT,
  G.GOAL_AMT
FROM PS_GENL_DEDUCTION G,
 PS_DED_CLASS_VW DC
WHERE G.EFFDT =
 (SELECT MAX(G_ED.EFFDT)
 FROM PS_GENL_DEDUCTION G_ED
 WHERE G.EMPLID  = G_ED.EMPLID
 AND G.COMPANY   = G_ED.COMPANY
 AND G.DEDCD     = G_ED.DEDCD
 AND G_ED.EFFDT <= SYSDATE
 )
AND ( G.DEDUCTION_END_DT IS NULL
OR G.DEDUCTION_END_DT     > SYSDATE)
AND ( G.GOAL_AMT          = 0.00
OR G.GOAL_AMT            <> G.GOAL_BAL)
AND G.DED_ADDL_AMT        > 0
AND DC.PLAN_TYPE          = '00'
AND DC.DEDCD              = G.DEDCD
AND DC.EFFDT              =
 (SELECT MAX(V1.EFFDT)
 FROM PS_DED_CLASS_VW V1
 WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
 AND V1.DEDCD       = DC.DEDCD
 )
AND G.EMPLID = 'XXXXXX'

理想情况下,我想要做的是放入一个值代替'XXXXXX',并用两个合并的语句获取一行数据。

谢谢大家!

2 个答案:

答案 0 :(得分:0)

基本上你做的事情就像

select blah, blah.. (your second query )
AND G.EMPLID IN ( SELECT DISTINCT C.COMPANY,
                  C.EMPLID,
                  C.SSN
                  FROM PS_PAY_CHECK C
                  WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
                  AND C.COMPANY                   IN ('001','054','076')
                 )

所以基本上我在你的第二个查询中使用了你的第一个查询。我删除了DISTINCT,因为它不需要。

我希望这是有道理的。

答案 1 :(得分:0)

为此,我们将每个SELECT语句粘贴到子查询中,并在主查询select语句中为该子查询指定一个别名:

SELECT t1.*, t2.*
FROM
    (
        SELECT DISTINCT C.COMPANY,
         C.EMPLID,
         C.SSN
        FROM PS_PAY_CHECK C
        WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
        AND C.COMPANY                   IN ('001','054','076')
        ORDER BY C.COMPANY, C.EMPLID
    ) t1
    INNER JOIN 
    (
        SELECT G.EMPLID, G.DEDCD,
          CASE
           WHEN DC.DED_CLASS IN ('A','B','T')
           THEN G.DED_ADDL_AMT
           ELSE 0
          END AS "EEAmt",
          CASE
           WHEN DC.DED_CLASS NOT IN ('A','B','T')
           THEN G.DED_ADDL_AMT
           ELSE 0
          END AS "ERAmt",
          DC.DED_CLASS,
          G.DED_ADDL_AMT,
          G.GOAL_AMT
        FROM PS_GENL_DEDUCTION G,
         PS_DED_CLASS_VW DC
        WHERE G.EFFDT =
         (SELECT MAX(G_ED.EFFDT)
         FROM PS_GENL_DEDUCTION G_ED
         WHERE G.EMPLID  = G_ED.EMPLID
         AND G.COMPANY   = G_ED.COMPANY
         AND G.DEDCD     = G_ED.DEDCD
         AND G_ED.EFFDT <= SYSDATE
         )
        AND ( G.DEDUCTION_END_DT IS NULL
        OR G.DEDUCTION_END_DT     > SYSDATE)
        AND ( G.GOAL_AMT          = 0.00
        OR G.GOAL_AMT            <> G.GOAL_BAL)
        AND G.DED_ADDL_AMT        > 0
        AND DC.PLAN_TYPE          = '00'
        AND DC.DEDCD              = G.DEDCD
        AND DC.EFFDT              =
         (SELECT MAX(V1.EFFDT)
         FROM PS_DED_CLASS_VW V1
         WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
         AND V1.DEDCD       = DC.DEDCD
         )
        AND G.EMPLID = 'XXXXXX'
    ) t2 ON
    t1.empid = t2.empid

我们只是将每个派生的表/子查询视为自己的表并在empid上将它们连接起来。您可以根据需要在顶部调整SELECT语句。

这类似于为两个sql语句创建视图,然后在第三个sql语句中引用视图。

另一种方法是使用CTE(公用表表达式)来容纳单独的sql。这里没有性能优势,但您可能会发现它更容易阅读。

WITH t1 as
(
    SELECT DISTINCT C.COMPANY,
     C.EMPLID,
     C.SSN
    FROM PS_PAY_CHECK C
    WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
    AND C.COMPANY                   IN ('001','054','076')
    ORDER BY C.COMPANY, C.EMPLID
),
t2 AS
(
    SELECT G.EMPLID, G.DEDCD,
      CASE
       WHEN DC.DED_CLASS IN ('A','B','T')
       THEN G.DED_ADDL_AMT
       ELSE 0
      END AS "EEAmt",
      CASE
       WHEN DC.DED_CLASS NOT IN ('A','B','T')
       THEN G.DED_ADDL_AMT
       ELSE 0
      END AS "ERAmt",
      DC.DED_CLASS,
      G.DED_ADDL_AMT,
      G.GOAL_AMT
    FROM PS_GENL_DEDUCTION G,
     PS_DED_CLASS_VW DC
    WHERE G.EFFDT =
     (SELECT MAX(G_ED.EFFDT)
     FROM PS_GENL_DEDUCTION G_ED
     WHERE G.EMPLID  = G_ED.EMPLID
     AND G.COMPANY   = G_ED.COMPANY
     AND G.DEDCD     = G_ED.DEDCD
     AND G_ED.EFFDT <= SYSDATE
     )
    AND ( G.DEDUCTION_END_DT IS NULL
    OR G.DEDUCTION_END_DT     > SYSDATE)
    AND ( G.GOAL_AMT          = 0.00
    OR G.GOAL_AMT            <> G.GOAL_BAL)
    AND G.DED_ADDL_AMT        > 0
    AND DC.PLAN_TYPE          = '00'
    AND DC.DEDCD              = G.DEDCD
    AND DC.EFFDT              =
     (SELECT MAX(V1.EFFDT)
     FROM PS_DED_CLASS_VW V1
     WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
     AND V1.DEDCD       = DC.DEDCD
     )
    AND G.EMPLID = 'XXXXXX'
)

SELECT t1.*, t2.*
FROM t1 INNER JOIN t2 ON
    t1.empid = t2.empid