測試數據: mysql> select * from t2; +----+--------+---------------------+------------+ | id | userid | inputDate | infoStatus | +----+--------+---------------------+------------+ | 1 | 1 | 2014-07-11 00:00:00 | 20013 | | 2 | 1 | 2014-07-11 00:00:00 | 0 | | 3 | 2 | 2014-07-12 00:00:11 | 20015 | | 4 | 2 | 2014-07-12 00:00:22 | 20013 | | 5 | 2 | 2014-07-14 00:00:00 | 0 | | 6 | 2 | 2014-07-15 00:00:00 | 20013 | | 7 | 2 | 2014-07-15 00:00:00 | 0 | | 8 | 2 | 2014-07-16 00:00:00 | 0 | | 9 | 2 | 2014-07-13 00:00:00 | 20013 | | 10 | 2 | 2014-07-13 00:00:00 | 0 | | 11 | 2 | 2014-07-14 00:00:00 | 0 | +----+--------+---------------------+------------+ 查詢sql: select id,userid,date(inputDate) as date,max(infoStatus) as m,min(infoStatus) as i,if(min(infoStatus)=0,0,max(infoStatus)) as max from t2 where infoStatus = 0 or infoStatus <= 99999 group by date; 回執結果: +----+--------+------------+-------+-------+-------+ | id | userid | date | m | i | max | +----+--------+------------+-------+-------+-------+ | 1 | 1 | 2014-07-11 | 20013 | 0 | 0 | | 3 | 2 | 2014-07-12 | 20015 | 20013 | 20015 | | 9 | 2 | 2014-07-13 | 20013 | 0 | 0 | | 5 | 2 | 2014-07-14 | 0 | 0 | 0 | | 6 | 2 | 2014-07-15 | 20013 | 0 | 0 | | 8 | 2 | 2014-07-16 | 0 | 0 | 0 | +----+--------+------------+-------+-------+-------+ 我的思路是按照時間分組,並同時查詢最小,跟最大的,如果最小的為0,那max字段就為0,否則就取最大的那個
如果數據在A1:A1000中,B2中輸入
=T(INDEX(A:A,MATCH(,0/IF(COUNTIF(B$1:B1,A$1:A$1000)=0,ROW(A$1:A$1000)),)))
同時按下CTRL+SHIFT+ENTER輸入數組公式,再用自動填充柄將公式向下復制就可以了.
你把日期格式改下,把日期中的"."替換成"-"改成yyyy-mm-dd的格式.然後按日期降序排。加一輔助列,輸入公式=COUNTIF($A$1:A1,A1),A列是編號。公式中等於1的是不重復或重復編號對應時間最大值。