PL/SQL 游标选择唯一记录并在平面文件中打印

发布于 2024-10-05 14:19:31 字数 1293 浏览 3 评论 0原文

我在游标中有一组值。例如:

CURSOR c_stock_option IS
SELECT empid, '1' AS ISenrolled
FROM employee emp
UNION ALL
SELECT empid, '2' AS ISenrolled
FROM employee emp;

现在我想检查 empid 是否同时出现在第一个选择中(其中 IEnrolled =1)和第二个选择(其中 IEnrolled =2)。我只想从第一个 select where enroll=1 获取值并拒绝 where enroll=2。我只想打印符合此标准的记录。

FOR v_stock_option_record IN c_stock_option LOOP
    IF v_esppstock_recs  IN (v_stock_option_record.empid) THEN

    END IF;
    -- Participant file.
    v_member_string_1 := v_stock_option_record.empid || G_DELIMITER || --1. participant id
    v_stock_option_record.last_name || G_DELIMITER || --4. Last Name
    v_stock_option_record.first_name || G_DELIMITER || --5. First Name
END loop;

在查询的第一部分中,它选择所有已购买股票的员工(这将仅给出已购买股票的员工集合,查询的其他部分给出公司中的所有在职员工,因此位于选择的第一部分中的员工将始终位于选择的第二部分中,但是位于选择的第二部分中的员工不一定位于第一部分中,当员工出现在两个部分中时,我需要做什么。只需选择 isenrolled=1 的员工)。 下面是区分的SQL

SELECT
    empid,
    '1' AS ISenrolled
    FROM employee emp,
    hrempusf usf
    where emp.employee = usf.employee
          AND usf.field_key = 76 ---- 76 determines that employee has purchased stocks
UNION ALL
    SELECT
     empid,
    '2' AS ISenrolled
     FROM employee emp;

I have set of values in a cursor. For example:

CURSOR c_stock_option IS
SELECT empid, '1' AS ISenrolled
FROM employee emp
UNION ALL
SELECT empid, '2' AS ISenrolled
FROM employee emp;

Now I want to check if the empid appears both in first select (where ISenrolled
=1
) and second select (where ISenrolled
=2
). I want to only grab the value from first select where enroll=1 and reject the one where enroll=2. I want to only print records that qualifies this criteria.

FOR v_stock_option_record IN c_stock_option LOOP
    IF v_esppstock_recs  IN (v_stock_option_record.empid) THEN

    END IF;
    -- Participant file.
    v_member_string_1 := v_stock_option_record.empid || G_DELIMITER || --1. participant id
    v_stock_option_record.last_name || G_DELIMITER || --4. Last Name
    v_stock_option_record.first_name || G_DELIMITER || --5. First Name
END loop;

In the first part of query it is selecting all the employees that have purchased the stocks (that will give only the set of employees who have purchased the stocks, other part of the query gives all the active employees in the company, so the employee who is in the first part of the select will always be in second part of the select, but the employee who is in second part of select is not necessarily in the 1st part. In the scenario when employee appears in both parts what I need to do is just select the employee who has isenrolled=1).
Below is the SQL to differentiate

SELECT
    empid,
    '1' AS ISenrolled
    FROM employee emp,
    hrempusf usf
    where emp.employee = usf.employee
          AND usf.field_key = 76 ---- 76 determines that employee has purchased stocks
UNION ALL
    SELECT
     empid,
    '2' AS ISenrolled
     FROM employee emp;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-10-12 14:19:34

这是一种可行的方法,它只会返回 1=>IsEnrolled(如果存在),否则会返回 2 IsEnrolled

“数据”是模仿你的两个选择语句。

with data as(
select 1 empId, 1 ISEnrolled from dual
union
select 2 empId, 1 ISEnrolled from dual
union
select 3 empId, 1 ISEnrolled from dual
union
select 4 empId, 1 ISEnrolled from dual
union
select 5 empId, 1 ISEnrolled from dual /** these 5 are mimicing your first select */
union
select 1 empId, 2 ISEnrolled from dual /** the next are the 'all' */
union
select 2 empId, 2 ISEnrolled from dual
union
select 3 empId, 2 ISEnrolled from dual
union
select 4 empId, 2 ISEnrolled from dual
union
select 5 empId, 2 ISEnrolled from dual
union
select 6 empId, 2 ISEnrolled from dual
union
select 7 empId, 2 ISEnrolled from dual
union
select 8 empId, 2 ISEnrolled from dual
union
select 9 empId, 2 ISEnrolled from dual
union
select 10 empId, 2 ISEnrolled from dual)
,
onlyOneIsEnrolled as (
  select empId, isEnrolled
    from data
    where isEnrolled = 1
) ,
notInOneIsEnrolled as(
  select empId, isEnrolled
    from data d
    where not exists(select null
                           from onlyOneIsEnrolled ooie
                            where ooie.empid = d.empId
                            )
)
select EmpId, isEnrolled
  from onlyOneIsEnrolled
  union
select EmpId, isEnrolled
  from notInOneIsEnrolled
order by isEnrolled, EmpId

这只是完成任务的一种方法,onlyOneIsEnrolled 收集所有 1,然后 notInOneIsEnrolled 获取所有不在上面的 emps,查询的最后部分将它们放在一起。

有很多方法可以完成此任务,这利用 CTE,但根据您的需要,您可以无需使用 with 子句即可完成此操作。

Here is one way that should work, it will only return the 1=>IsEnrolled if it exists, otherwise it'll return the 2 IsEnrolled.

The "data" is to mimic your two select statements.

with data as(
select 1 empId, 1 ISEnrolled from dual
union
select 2 empId, 1 ISEnrolled from dual
union
select 3 empId, 1 ISEnrolled from dual
union
select 4 empId, 1 ISEnrolled from dual
union
select 5 empId, 1 ISEnrolled from dual /** these 5 are mimicing your first select */
union
select 1 empId, 2 ISEnrolled from dual /** the next are the 'all' */
union
select 2 empId, 2 ISEnrolled from dual
union
select 3 empId, 2 ISEnrolled from dual
union
select 4 empId, 2 ISEnrolled from dual
union
select 5 empId, 2 ISEnrolled from dual
union
select 6 empId, 2 ISEnrolled from dual
union
select 7 empId, 2 ISEnrolled from dual
union
select 8 empId, 2 ISEnrolled from dual
union
select 9 empId, 2 ISEnrolled from dual
union
select 10 empId, 2 ISEnrolled from dual)
,
onlyOneIsEnrolled as (
  select empId, isEnrolled
    from data
    where isEnrolled = 1
) ,
notInOneIsEnrolled as(
  select empId, isEnrolled
    from data d
    where not exists(select null
                           from onlyOneIsEnrolled ooie
                            where ooie.empid = d.empId
                            )
)
select EmpId, isEnrolled
  from onlyOneIsEnrolled
  union
select EmpId, isEnrolled
  from notInOneIsEnrolled
order by isEnrolled, EmpId

This is just one way to accomplish the task, the onlyOneIsEnrolled gathers all the 1's, then notInOneIsEnrolled gets all the emps not in the above, the final part of the query puts them together.

There are many ways to accomplish this task, this utilizes CTE's but depending on your need you may be able to do this without utilizing the with clause.

明媚如初 2024-10-12 14:19:33

为此,您不需要复杂的 PL/SQL,只需要 LEFT OUTER JOIN。这将返回所有 EMPLOYEE 记录,无论它是否与 HREMPUSF 记录匹配。

SELECT
     empid
     , nvl2(usf.field_key ,'1', '2') AS ISenrolled
  FROM employee emp
     left outer join hrempusf usf
          on ( usf.employee = emp.employee
              and usf.field_key = 76 )

如果第一个参数不为 null,则 NVL2() 返回第二个值;如果为 null,则返回第三个参数。

You don't need complicated PL/SQL for this, you just need a LEFT OUTER JOIN. This will return all the EMPLOYEE records, regardless of whether it matches an HREMPUSF record.

SELECT
     empid
     , nvl2(usf.field_key ,'1', '2') AS ISenrolled
  FROM employee emp
     left outer join hrempusf usf
          on ( usf.employee = emp.employee
              and usf.field_key = 76 )

The NVL2() returns the second value if the first argument is not null and the third argument if it is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文