如何像 Informatica 一样执行分组等效?

How to perform Grouping equivalent like Informatica?

我有一个 Informatica 函数,我想将其转换为查询以在 Spring 批处理代码中使用。

我有一个 table EMPLOYEE table 具有 15 fields(我想要的全部在 select 中)并且 Informatica 具有 Router 函数,它创建基于组的在 STATUS_CD = 'A' 和默认值上(意味着所有其他记录都应该放在此处 - 状态不是 A)。

我们在 Postgres 中可以做什么?

我有所有员工,我想使用 EMPLOYEE_CD 的组合来检查,EMPLOYEE_ID 是唯一的,我想简单地 return 它的计数。

查询 1

SELECT EMPLOYEE_CD AS EMPLOYEE_CD,
         EMPLOYEE_ID AS EMPLOYEE_ID,
         COUNT (*)  AS CNT
FROM EMPLOYEE
GROUP BY EMPLOYEE_CD, EMPLOYEE_ID
  HAVING COUNT (*) > 1;

查询 2

SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS, (Create Count Field here)
FROM EMPLOYEE

查询 3 - 我需要分组(这是我原来的问题)或创建列 ACTIVENON_ACTIVE 列作为查询结果的一部分,其中 EMPLOYEE_STAT_CD = 'A'、ACTIVE 列值应为 YES,除 ANON_ACTIVE 外的 EMPLOYEE_STAT_CD 应为 Yes.

如何将查询 1、查询 2 和查询 3 合并为单个查询?

如果我理解了这个问题,你的代码是这样的:

SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS,
  COUNT(*)OVER(PARTITION BY EMPLOYEE_CD, EMPLOYEE_ID) AS counter_from_sql1,
  CASE WHEN EMPLOYEE_STAT_CD = 'A' THEN 'YES' ELSE NULL END AS ACTIVE,
  CASE WHEN EMPLOYEE_STAT_CD <> 'A' THEN 'YES' ELSE NULL END AS NON_ACTIVE
FROM EMPLOYEE;

SELECT * FROM (
  SELECT EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, EMPLOYEE_PHONE, EMPLOYEE_ADDRESS,
    COUNT(*)OVER(PARTITION BY EMPLOYEE_CD, EMPLOYEE_ID) AS counter_from_sql1,
    CASE WHEN EMPLOYEE_STAT_CD = 'A' THEN 'YES' ELSE NULL END AS ACTIVE,
    CASE WHEN EMPLOYEE_STAT_CD <> 'A' THEN 'YES' ELSE NULL END AS NON_ACTIVE
  FROM EMPLOYEE
) z
WHERE counter_from_sql1 > 1;