日期:2014-05-18  浏览次数:20621 次

查询只得到一条数据

有如下表 [Log_Schedule], 希望去除掉重复数据,对于dtDatetime 列,不考虑最后毫秒 位,即认为 2011-06-04 08:19:10.210 = 2011-06-04 08:19:10.143, 如何查询只得到一条数据? sql 2000 , 谢谢

  dtDateTime sUserID
  ----------------------------------
  2011-06-04 08:19:10.210 A
  
  2011-06-04 08:19:10.143 A

------解决方案--------------------
SQL code
select * from tb t where not exists(select 1 from tb where datediff(dd,dtdatetime,t.dtdatetime)=0 and suserid=t.suserid and dtdatetime>t.dtdatetime)

------解决方案--------------------
SQL code
select * from tb t where not exists(select 1 from tb where datediff
(dd,dtdatetime,t.dtdatetime)=0 and suserid=t.suserid and dtdatetime>t.dtdatetime)

------解决方案--------------------
SQL code
IF OBJECT_ID('tempdb..#temp', 'u') IS NOT NULL
    DROP TABLE #temp
GO
CREATE TABLE #temp
(
    dtDateTime DATETIME,
    sUserID VARCHAR(10)
)
INSERT #temp
select '2011-06-04 08:19:10.210', 'A' UNION ALL
select '2011-06-04 08:19:10.143', 'A'
GO
--SQL:
SELECT DISTINCT *
FROM #temp T
WHERE NOT EXISTS(
    SELECT 1 FROM #temp 
    WHERE CONVERT(VARCHAR(23), dtDateTime, 120) = CONVERT(VARCHAR(23), T.dtDateTime, 120)
        AND dtDateTime < T.dtDateTime
)

--RESULT:
/*
dtDateTime    sUserID
2011-06-04 08:19:10.143    A
*/

------解决方案--------------------
SQL code
select distinct convert(varchar(100),dtDateTime,120)  as dtDateTime,sUserID

------解决方案--------------------
SQL code
select distinct convert(varchar(19),dtDatetime,120) as dtDatetime,sUserID
from Log_Schedule