我有一个包含CHECKTIME,USERID和CHECKTYPE的表。此CHECKTIME类型是datetime。我想获得这些数据而没有重复的CHECKTIME日期。例如,我的数据是这样的:
USERID | CHECKTIME | CHECKTYPE
1 | 2016-05-01 06:58:00 | I
1 | 2016-05-01 06:59:00 | I
1 | 2016-05-02 06:58:00 | I
1 | 2016-05-02 06:58:10 | I
1 | 2016-05-02 06:59:00 | I
有谁知道如何删除该重复日期?所以,从上面的数据,我可以得到这样的结果:
USERID | CHECKTIME | CHECKTYPE
1 | 2016-05-01 06:58:00 | I
1 | 2016-05-02 06:58:00 | I
我已经尝试过使用distinct,但它不起作用,因为每个CHECKTIME都有不同的时间。我已经使用了GROUP BY子句,但它只能给我一个字段的结果。
我现在的查询是这样的:
SELECT USERID, CHECKTYPE, CHECKTIME, DAY(CHECKTIME)
FROM CHECKINOUT
WHERE USERID = 259
AND MONTH(CHECKTIME)='5'
AND YEAR(CHECKTIME)='2016'
AND CHECKTYPE = 'I'
GROUP BY DAY(CHECKTIME)
ORDER BY DAY(CHECKTIME) ASC
它出现了关于聚合函数的错误。
答案 0 :(得分:1)
你需要时间吗?
SELECT DISTINCT
USERID, Format([CHECKTIME],"yyyy/mm/dd") AS CHECKDATE, CHECKTYPE
FROM CHECKINOUT;
没有时间的两个记录
USERID | CHECKTIME | CHECKTYPE
1 | 2016-05-01 | I
1 | 2016-05-02 | I
或者您是否正在寻找每个独特日子的第一次?这样做
编辑:按日期条款添加订单
SELECT DISTINCT USERID, CHECKTYPE,
Format([CHECKTIME],"yyyy/mm/dd") AS CHECKDATE,
Min(CHECKINOUT.CHECKTIME) AS FirstCheckTime
FROM CHECKINOUT
WHERE (Month([CheckTime])=5) AND (Year([CheckTime])=2016)
GROUP BY USERID, CHECKTYPE, Format([CHECKTIME],"yyyy/mm/dd")
HAVING ((USERID=1) AND (CHECKTYPE="I"))
ORDER BY Format([CHECKTIME],"yyyy/mm/dd");
看起来像这样:
USERID CHECKTYPE CHECKDATE FirstCheckTime
1 I 2016/05/01 2016-05-01 06:58:00
1 I 2016/05/02 2016-05-02 06:58:00