2014-07-16 28 views
1
我使用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 
谁没有在同一个月得分

我已经使用查询与我这样的其他要求

“select * from npstable where date_created> ='2012-01-01'AND date_created < ='2012-06-05'AND AND BETWEEN 0 A ND 10;“

任何想法如何使选择查询,可以生成我所需的报告。

+0

使用SELECT DISTINCT(得分)版本,看看这篇文章 - > http://stackoverflow.com/questions/5967130/mysql-select-one-column-distinct-with-corresponding-other-columns – SerhatCan

+1

重复http://stackoverflow.com/questions/16979136/mysql- select-distinct-records-from-latest-dates-only – ahruss

+0

检查ou你的需求输出。你认为最后一行是正确的吗? –

回答

0

此查询选择每月每最早记录PERSONID

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 

另一种选择,每月每PERSONID最低ID行

select t1.* 
from npstable t1 
where id in (select 
    min(id) from npstable t2 
    group by personid, month(date), year(date) 
)