如何从大于某个值的子查询中得到 return 结果

How to return results from a subquery greater than a value

我正在尝试查找至少出现 3 次的测量组件 ID。此时它正在抛出 Oracle 错误“ORA-00936:缺少表达式”。我曾尝试放入现有语句来可能满足缺少的表达式错误,但无济于事。它要么返回太多值,要么根本没有返回任何值。如果我的测量组件列返回出现至少 3 次的值,我该如何修复它以返回结果。

select td.td_entry_id, td.complete_dttm, imd.init_msrmt_data_id, imd.measr_comp_id,
imd.bus_obj_cd, imd.bo_status_cd, imd.status_upd_dttm, rep.last_name FROM
ci_td_entry td,
ci_td_drlkey drill,
d1_init_msrmt_data imd,
sc_user rep
WHERE td.td_type_cd ='D1-IMDTD'
and td.entry_status_flg = 'C'
and imd.init_msrmt_data_id = drill.key_value
and td.td_entry_id = drill.td_entry_id
and imd.bo_status_cd in ('DISCARDED','REMOVED')
and td.complete_user_id = rep.user_id
and td.complete_dttm >= '01-MAY-21'
and (select count(*)
    from d1_init_msrmt_data imd
    group by imd.measr_comp_id
    HAVING COUNT(*) > 3);

How do I bring back results where my measuring component column brings back values that occur at least 3 times.

猜想这就是你要问的?

SELECT
    td.td_entry_id,
    td.complete_dttm,
    imd.init_msrmt_data_id,
    imd.measr_comp_id,
    imd.bus_obj_cd,
    imd.bo_status_cd,
    imd.status_upd_dttm,
    rep.last_name
FROM
    ci_td_entry                      td
    INNER JOIN ci_td_drlkey       drill ON drill.td_entry_id = td.td_entry_id
    INNER JOIN d1_init_msrmt_data   imd ON imd.init_msrmt_data_id = drill.key_value
    INNER JOIN sc_user              rep ON rep.user_id = td.complete_user_id
WHERE
    td.td_type_cd ='D1-IMDTD'
    AND td.entry_status_flg = 'C'
    AND imd.bo_status_cd in ('DISCARDED','REMOVED')
    AND td.complete_dttm >= '01-MAY-21'
    AND (
        SELECT COUNT(*)
        FROM d1_init_msrmt_data
        WHERE measr_comp_id = imd.measr_comp_id
    ) >= 3
;

“在 d1_init_msrmt_data 中出现至少 3 次相同 measr_comp_id 的所有记录。”

我的原始查询使用 group by 并在您添加表达式存在后具有功能。要使子查询真正起作用,需要做的是将其与主查询相关联。这是通过添加在主查询中找到的日期参数、寻找“丢弃和删除”的参数以及通过在子查询本身中创建内部联接来完成的,该内部联接加入了子查询中使用的 table到主查询中的 table。最终结果如下所示:

SELECT
    td.td_entry_id,
    td.complete_dttm,
    imd.init_msrmt_data_id,
    imd.measr_comp_id,
    imd.bus_obj_cd,
    imd.bo_status_cd,
    imd.status_upd_dttm,
    rep.last_name
FROM
    ci_td_entry          td,
    ci_td_drlkey         drill,
    d1_init_msrmt_data   imd,
    sc_user              rep
WHERE
    td.td_type_cd = 'D1-IMDTD'
    AND td.entry_status_flg = 'C'
    AND imd.init_msrmt_data_id = drill.key_value
    AND td.td_entry_id = drill.td_entry_id
    AND td.complete_dttm = imd.status_upd_dttm
    AND imd.bo_status_cd IN (
        'DISCARDED',
        'REMOVE'
    )
    AND td.complete_user_id = rep.user_id
    AND EXISTS (
        SELECT
            COUNT(*)
        FROM
            d1_init_msrmt_data imd2
        WHERE
            imd.measr_comp_id = imd2.measr_comp_id
            AND imd2.status_upd_dttm >= '01-JUN-21'
            AND imd2.bo_status_cd IN (
                'DISCARDED',
                'REMOVE'
            )
        GROUP BY
            imd2.measr_comp_id
        HAVING
            COUNT(*) >= 3
    );

我感谢大家帮助我得到这个结果。