我有这样的表1:
number p uc_id date
3 1 g 24/09/2015
4 0 g 24/09/2015
5 1 g 24/09/2015
5 1 f 25/09/2015
3 0 f 25/09/2015
5 1 g 26/09/2015
和表2一样:
id name
g Magic
f Blue
我希望sql查询返回类似
的内容name min max
Magic 1 2
Blue 1 1
我的查询我做了类似日期的事情,但我无法回复最小值和最大值
Select
p.date,
uc.name,
min((Select
uc.name,
count(p.p)
from table1 p, table2 uc
where p.p = 1 and uc.id = p.uc_id
group by uc.name, p.date)) as 'Min',
max((Select uc.name,
count(p.p)
from table1 p, table2 uc
where p.p=1 and uc.id = p.uc_id
group by uc.name, p.date)) as 'Max'
From table1 p, table2 uc
Where uc.id = p.uc_id
group by uc.name
我想计算p = 1的列p,并按日期对它们进行分组,对于每个日期,我想要重新计算当前完成的计数的最小值和最大值。同时在另一个表中显示名称。
答案 0 :(得分:0)
使用按日期计算的总计数名称创建子查询。
SELECT name, min(nCount) cMin, max(nCount) cMax
FROM (Select
uc.name,
p.date,
count(p) as nCount
From table1 p, table2 uc
Where uc.id = p.uc_id
and p = 1
group by uc.name, p.date) nameCount
GROUP BY name