我在试图建立一个SQL query将计两行的总数每个id和数量的'FN%'和'W%的成绩进行分组。如果这些数字都是平等的,然后学生只有所有'FN%'或所有'W%或两者的结合。

我需要一个名单的所有身份证的人只有统计数据的'FN%'或'W%'

例id#683&657会使它成为结果集中的查询,但603,781&694不会

   id stat
  683 WF
  683 WF
  683 WF
  683 WF
  683 W
  683 W
  657 W
  657 W
  657 W
  657 W
  781 B+
  781 IP
  781 WP
  781 WP
  603 FN
  603 FN
  603 F
  603 FN
  603 FN
  694 B
  694 B+
  694 CI
  694 LAB
  694 WF
  694 WF

样本输出:

683
657

有帮助吗?

解决方案

下面是我能想到的两种可能的解决方案。我不知道它们是否会在Informix的工作:

SELECT  id
FROM    foo a
GROUP   BY id
HAVING  COUNT(*) = (
                SELECT  COUNT(*)
                FROM    foo b
                WHERE   a.id = b.id
                AND     (b.stat LIKE 'FN%' OR b.stat LIKE 'W%')
        );

而如果HAVING子句中的子查询是禁止的,也许这将工作,而不是:

SELECT  id
FROM    (
                SELECT  id, COUNT(*) stat_count
                FROM    foo
                WHERE   (stat LIKE 'FN%' OR stat LIKE 'W%')
                GROUP   BY id
        ) a
WHERE   stat_count = (SELECT COUNT(*) FROM foo b WHERE a.id = b.id);

更新:我只是试图在这些甲骨文,无一不工作

其他提示

这一解释使得我的头受到伤害。你是在寻找联盟的这两套?

  • id这只有统计数据匹配"W%"
  • id这只有统计数据匹配"FN%"

如果是这种情况,使联合查询的子查询每个集。

其中xxxx是要被处理保持该信息的临时表.....

select id, fullname, count(id) ttl 
from xxxx 
group by id, fullname 
into temp www with no log;


select id, fullname, count(id) ttl_f 
from xxxx 
where grd like 'FN%' or grd like 'W%' 
group by id, fullname 
into temp wwww with no log;


select www.id, www.fullname 
from www, wwww 
where www.id = wwww.id and www.ttl = wwww.ttl_f;

此被写入针对原来的问题:

select first 50
c.id,
(select trim(fullname) from entity where id = c.id) fullname,
count(*),
(select count(*) from courses where id = c.id and grd like 'FN%') FN,
(select count(*) from courses where id = c.id and grd like 'W%') W
from courses c
group by 1

检索名称中的子查询比使用由于某种原因,加入速度要快得多。

编辑: 以下将具有相同的行为yukondude的答案,但执行上我们HPUX / Informix的v10.00.HC5盒更好。

select c.id
from courses c
where not exists (
        select id
        from courses
        where (grd not like 'W%' and grd not like 'FN%')
        and id = c.id
)
group by 1
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top