【发布时间】:2015-12-03 08:01:05
【问题描述】:
我有这样的表 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 并按日期对它们进行分组,对于每个日期我想检索之前完成的计数的最小值和最大值。还在另一个表中显示名称。
【问题讨论】:
-
你的返回查询逻辑是什么,最小值/最大值是多少?
-
Min when p=0 and Max when p=1????为什么魔法的最大值是 2 而不是 3?? -
一个错误是您在
inner select和outer select中使用别名p -
基本上我想从 p 中检索最大值和最小值 ...但只计算 p=1 我想在一个日期中查看的日期计算每天有多少个 1 并告诉最小值和最大的
-
请阅读How to ask