0

我有一个表,其中包含多个Rqst_id具有唯一Act_id风险的 s。我正在寻找获取 sql 代码以查找值全部“已批准”的req_ids 。Risk

任何人都可以帮忙吗?

谢谢!

Rqst_id Act_id  Risk

3530    24624   Abstain
3530    24714   Abstain
3733    24726   Approve
3733    24729   Null
4310    27473   Approve
4401    28444   Approve
4676    30797   Approve
5069    32003   Approve
4946    32062   Approve
4946    32063   Approve
4779    32424   Approve
4779    32425   Null
4

4 回答 4

0

尝试这个:

SELECT DISTINCT Rqst_id,RISK
FROM TABLE 
WHERE Risk='Approve'
于 2013-11-02T16:58:19.423 回答
0

你可以尝试这样的事情:

SELECT DISTINCT Rqst_id
FROM Table1
WHERE Rqst_id NOT IN (
    SELECT Rqst_id
    FROM Table1
    WHERE risk != "Approve"
      OR risk IS NULL
    );

这将为您提供没有任何风险的req_id!=“批准”。

sqlfiddle demo

于 2013-11-02T16:58:33.493 回答
0
SELECT DISTINCT rqst_id FROM my_table t
WHERE rqst_id NOT IN (
    SELECT rqst_id FROM my_table WHERE risk IS NULL OR risk <> 'Approve'
)
于 2013-11-02T16:59:25.987 回答
0
select t1.reqst_id
from the_table t1
where risk = 'Approve'
group by t1.reqst_id
having count(*) = (select count(*)
                   from the_table t2
                   where t2.reqst_id = t1.reqst_id);

SQLFiddle 示例:http ://sqlfiddle.com/#!12/915fc/3

如果您还需要完整的行(包括 act_id),您可以使用(标准 SQL)窗口函数:

select reqst_id, act_id, risk
from (
  select reqst_id, 
         act_id, 
         risk,
         count(*) over (partition by reqst_id) as total_count, 
         sum(case when risk = 'Approve' then 1 else 0 end) over (partition by reqst_id) as approve_count
  from risks
) t 
where total_count = approve_count;

SQLFiddle 示例:http ://sqlfiddle.com/#!12/915fc/5

请注意,使用该解决方案,如果有多行,则将多次返回单个 reqst_id。

使用窗口函数的解决方案实际上可能更快,因为只需要对表进行一次扫描。

于 2013-11-02T17:57:48.793 回答