我在这里使用MySQL是我的名为npstable的表
pk id | score | personid | date
---------------------------------------
435 | 4 | 33 | 2012-01-05
436 | 10 | 78 | 2012-01-21
437 | 5 | 22 | 2012-05-11
438 | 2 | 33 | 2012-01-22
439 | 10 | 22 | 2012-05-25
440 | 4 | 33 | 2012-02-05
我希望得到那些在同一个月没有得分的人的分数,就像我想要的那样
pk id | score | personid | date
---------------------------------------
435 | 4 | 33 | 2012-01-05
436 | 10 | 78 | 2012-01-21
437 | 10 | 22 | 2012-05-25
440 | 4 | 33 | 2012-02-05
我已经将查询与我的其他要求一起使用
"从npstable中选择*,其中date_created> =' 2012-01-01' AND date_created< =' 2012-06-05' AND得分0和10之间;"
如何制作可以生成我所需报告的精选查询的任何想法。
答案 0 :(得分:0)
此查询选择每个人员每月最早的记录
select t1.*
from npstable t1
join (
select
personid, year(date) my_year,
month(date) my_month, min(date) min_date
from npstable t2
group by person_id, my_year, my_month
) t2 join on year(t1.date) = t2.my_year
and month(t1.date) = t2.my_month
and t1.date = t2.min_date
and t1.personid = t2.personid
另一个版本,用于选择每个每人每月ID最低的行
select t1.*
from npstable t1
where id in (select
min(id) from npstable t2
group by personid, month(date), year(date)
)