当前位置: 代码迷 >> Sql Server >> 查询只得到一条数据,该如何处理
  详细解决方案

查询只得到一条数据,该如何处理

热度:73   发布时间:2016-04-27 11:55:37.0
查询只得到一条数据

有如下表 [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 #tempGOCREATE TABLE #temp(    dtDateTime DATETIME,    sUserID VARCHAR(10))INSERT #tempselect '2011-06-04 08:19:10.210', 'A' UNION ALLselect '2011-06-04 08:19:10.143', 'A'GO--SQL:SELECT DISTINCT *FROM #temp TWHERE NOT EXISTS(    SELECT 1 FROM #temp     WHERE CONVERT(VARCHAR(23), dtDateTime, 120) = CONVERT(VARCHAR(23), T.dtDateTime, 120)        AND dtDateTime < T.dtDateTime)--RESULT:/*dtDateTime    sUserID2011-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,sUserIDfrom Log_Schedule
  相关解决方案