gpt4 book ai didi

mysql - 将2合并为1的SQL查询,具体来说就是统计每个组中每个组的人数

转载 作者:行者123 更新时间:2023-11-29 22:24:26 25 4
gpt4 key购买 nike

我在这里发现了一些类似的帖子,但不适用于我想做的事情......

类似帖子:Trying to write a query that counts multiple things with different where cases

类似帖子:Query that Counts records with a WHERE clause

我想要做的是我有一些...... 200 个小组,这些小组中有特定申请日期的人。我想要统计这些组中有多少人的申请日期在特定范围内。

这是我使用的第一个方法,但它一次只适用于一组

SELECT count(*) as count
FROM membersapplication ma
INNER JOIN members mb on mb.mbr_id = ma.mbr_id
WHERE (GPL_ID = 20179) and (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015') and (ma.mpl_cancellationdate is null)

此代码对属于 GPL_ID 20179(群组放置 ID)范围内的任何人进行计数,我有 200 个 GPL_ID,我希望为其运行,永远不会有重复的 GPL_ID。

SELECT Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
FROM groupsreview gr
INNER JOIN groups g on gr.grp_ID = g.grp_ID
WHERE (GRP_ENROLLTYPE = 1) and (gp.gpl_effectivedate >= '4/30/2014' and gp.gpl_effectivedate <= '4/30/2015')
order by grp_name asc

这段代码为我提供了我想要的在我想要的日期范围内的每个 GPL_ID 的列表(基于 GRP_Enrolltype = 1)

我基本上想组合这两个代码,以便第二组代码添加另一列,该列的计数基于第一个代码

最佳答案

看来您真的只需要在查询中添加GROUP BY:

SELECT ma.GPL_ID, count(*) as count
FROM membersapplication ma
INNER JOIN members mb
ON mb.mbr_id = ma.mbr_id
where (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015')
AND (ma.mpl_cancellationdate is null)
GROUP BY ma.GPL_ID

关于mysql - 将2合并为1的SQL查询,具体来说就是统计每个组中每个组的人数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com