Informix SQL count() 比较

发布于 2024-07-16 08:52:31 字数 535 浏览 4 评论 0原文

我正在尝试构建一个 SQL 查询,该查询将计算每个 id 的总行数以及按 id 分组的“FN%”和“W%”等级数。 如果这些数字相等,则学生只有全部“FN%”或全部“W%”或两者的组合。

我需要所有仅具有“FN%”或“W%”统计数据的 ID 的列表

示例 ID # 683 & 657 会进入查询的结果集中,但 603、781 和 603 会进入查询的结果集中。 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章

I'm trying to build a SQL query that will count both the total number of rows for each id, and the number of 'FN%' and 'W%' grades grouped by id. If those numbers are equal, then the student only has either all 'FN%' or all 'W%' or a combination of both.

I need a list of all the id's who only have stats of 'FN%' or 'W%'

example id # 683 & 657 would make it into the result set of the query, but 603, 781 & 694 would not

   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

sample output:

683
657

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

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

发布评论

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

评论(4

嗼ふ静 2024-07-23 08:52:31

这是我能想到的两种可能的解决方案。 我不确定它们是否能在 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);

更新:我刚刚在 Oracle 中尝试过这些,并且都有效。

Here are two possible solutions that I can think of. I'm not sure if they'll work in 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%')
        );

And if subqueries in the HAVING clause are verboten, maybe this will work instead:

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);

Update: I just tried these in Oracle, and both work.

盗心人 2024-07-23 08:52:31

这个解释让我头疼。 您在寻找这两个集合的并集吗?

  • 仅具有与“W%”匹配的统计数据的 ids
  • 仅具有与“FN%”匹配的统计数据的 ids

如果是这种情况,请使其成为一个 UNION 查询,其中每个集合都有一个子查询。

That explanation makes my head hurt. Are you looking for the union of these two sets?

  • ids which only have stats matching "W%"
  • ids which only have stats matching "FN%"

If that's the case, make it a UNION query with a sub-query for each of the sets.

[旋木] 2024-07-23 08:52:31

其中 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;

Where xxxx is the temp table that holds this info to be processed.....

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;
比忠 2024-07-23 08:52:31

这是针对原始问题编写的:

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

This was written against the original question:

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

The subquery to retrieve the name is much faster than using a join for some reason.

Edit:
The following will have the same behavior as yukondude's answer but performs better on our HPUX / Informix v10.00.HC5 box.

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