我在这里发现了一些类似的帖子,但不适用于我想做的事情......
类似的帖子:Trying to write a query that counts multiple things with different where cases
类似的帖子:Query that Counts records with a WHERE clause
我想要做的是我有一些...... 200组,在这些组中是具有特定申请日期的人。我想要计算那些申请日期在特定范围内的群体中有多少人。
所以这是我一直使用的第一种方法,但它一次只适用于1组
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),该列表属于我想要的日期范围
我基本上想要合并这两个代码,以便第二组代码添加另一个具有基于第一个代码的计数的列
答案 0 :(得分:0)
不确定这是否有效,但我可以尝试一下:
SELECT
*
FROM
(SELECT
count(*) as count, GPL_ID
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 GPL_ID) T1
INNER JOIN
(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')) T2
ON T1.GPL_ID = T2.GPL_ID
答案 1 :(得分:0)
似乎您真的只需要在查询中添加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
答案 2 :(得分:0)
这应该这样做。但我会仔细检查日期,我只使用你提供的日期;他们不匹配,我不确定他们是否应该:
SELECT ma.GPL_ID, count(*) as count
FROM groups g
INNER JOIN groupsreview AS gr ON g.grp_ID = gr.grp_ID
INNER JOIN membersapplication AS ma ON gr.GPL_ID = ma.GPL_ID
INNER JOIN members AS mb ON mb.mbr_id = ma.mbr_id
WHERE g.GRP_ENROLLTYPE = 1
AND gr.gpl_effectivedate BETWEEN 20140430 AND 20150430
AND ma.mpl_effectivedate BETWEEN 20150201 and 20150430
AND ma.mpl_cancellationdate IS NULL
GROUP BY ma.GPL_ID
;
从您的问题的措辞来看,按GPL_ID
而不是grp_ID
分组感觉有点奇怪。
答案 3 :(得分:0)
基本上你应该通过组合连接然后在GPL_ID和has子句上进行分组来解决这个问题。这就是我们想到的。
SELECT Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
count(*) as grp_count
FROM membersapplication ma
INNER JOIN members mb on mb.mbr_id = ma.mbr_id
INNER JOIN groupsreview gr on mb.GPL_ID = gr.GPL_ID
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')
GROUP BY Gr.GPL_ID, Gr.GPL_Effectivedate, G.GRP_Enrolltype, G.GRP_Name, G.GRP_ID, G.GRP_Executive
HAVING (ma.mpl_effectivedate >= '2/01/2015' and ma.mpl_effectivedate <= '4/30/2015') and (ma.mpl_cancellationdate is null)
order by grp_name asc
希望这会有所帮助