无法使用distinct命令在SQL中获取不同的记录

时间:2016-01-05 16:33:09

标签: sql-server duplicates distinct

我使用以下查询从表中获取不同的记录

 SELECT distinct UFT
      ,ID
      ,NUM
      ,CONVERT(VARCHAR(19), UFT, 120) AS FTC
      ,CONVERT(VARCHAR(19), TIMESTAMP, 120) as TIMESTAMP
      ,CONVERT(VARCHAR(19), UIT, 120) AS UIT
      ,RANGE
      ,FORCE/1000/2 as FORCE
      ,CONVERT(VARCHAR(19), LFT, 120) as LFT
      ,CONVERT(VARCHAR(19), LIT, 120) as LIT
  FROM TABLE1
  where ID = 2 
  AND NUM = '144'
  AND UFT > '2014-01-01 00:00:00.000'
    and UFT <= '2015-01-01 00:00:00.000'
  order by FTC, UIT  desc

输出

UFT ID  NUM FTC TIMESTAMP   UIT RANGE   FORCE   LFT LIT
2014-08-22 16:00:00.000 2   144 2014-08-22 16:00:00 2014-08-22 14:30:31 2014-08-22 11:40:54 5.00    0.792000000 2014-08-22 16:00:00 2014-08-22 12:40:54
2014-08-22 16:30:00.000 2   144 2014-08-22 16:30:00 2014-08-22 15:30:30 2014-08-22 12:40:48 5.00    0.836000000 2014-08-22 16:30:00 2014-08-22 13:40:48
2014-08-22 17:00:00.000 2   144 2014-08-22 17:00:00 2014-08-22 15:30:30 2014-08-22 12:40:48 5.10    0.880000000 2014-08-22 17:00:00 2014-08-22 13:40:48
2014-08-22 17:30:00.000 2   144 2014-08-22 17:30:00 2014-08-22 16:30:30 2014-08-22 13:40:24 5.20    0.932000000 2014-08-22 17:30:00 2014-08-22 14:40:24
2014-08-22 18:00:00.000 2   144 2014-08-22 18:00:00 2014-08-22 16:30:30 2014-08-22 13:40:24 5.30    0.984000000 2014-08-22 18:00:00 2014-08-22 14:40:24
2014-08-22 21:30:00.000 2   144 2014-08-22 21:30:00 2014-08-22 20:30:30 2014-08-22 17:40:52 6.40    1.656000000 2014-08-22 21:30:00 2014-08-22 18:40:52
2014-08-22 21:30:00.000 2   144 2014-08-22 21:30:00 2014-08-22 17:30:30 2014-08-22 17:07:40 6.60    1.860000000 2014-08-22 21:30:00 2014-08-22 18:07:40
2014-08-22 22:00:00.000 2   144 2014-08-22 22:00:00 2014-08-22 20:30:30 2014-08-22 17:40:52 6.40    1.704000000 2014-08-22 22:00:00 2014-08-22 18:40:52
2014-08-22 22:00:00.000 2   144 2014-08-22 22:00:00 2014-08-22 17:30:30 2014-08-22 17:07:40 6.70    1.904000000 2014-08-22 22:00:00 2014-08-22 18:07:40
2014-08-22 22:30:00.000 2   144 2014-08-22 22:30:00 2014-08-22 21:30:28 2014-08-22 18:40:49 6.50    1.764000000 2014-08-22 22:30:00 2014-08-22 19:40:49

尽管在UFT列上使用distinct,我仍然在输出中找到多个记录。可能是什么原因,并有一种方法来改善查询

1 个答案:

答案 0 :(得分:1)

正如已经为其他人所解释的那样distinct对整行而不是单独的列进行操作。

我添加了一个新列,可以让您消除排序中第一次出现UFT值后出现的行。我不得不猜测您希望保留具有早期timestamp值的行,并且您还希望在FTC上进行分区:在row_number表达式中进行适当更改。

with data as (
    SELECT
         UFT
        ,row_number() over (partition by FTC, UFT order by TIMESTAMP) as rn
        ,ID
        ,NUM
        ,CONVERT(VARCHAR(19), UFT, 120) AS FTC
        ,CONVERT(VARCHAR(19), TIMESTAMP, 120) as TIMESTAMP
        ,CONVERT(VARCHAR(19), UIT, 120) AS UIT
        ,RANGE
        ,FORCE/1000/2 as FORCE
        ,CONVERT(VARCHAR(19), LFT, 120) as LFT
        ,CONVERT(VARCHAR(19), LIT, 120) as LIT
    FROM TABLE1
    WHERE
            ID = 2 
        AND NUM = '144'
        AND UFT >  '2014-01-01 00:00:00.000'
        AND UFT <= '2015-01-01 00:00:00.000'
)
select * from data where rn = 1
ORDER BY
    FTC, UIT DESC

您的查询按降序显示UIT,但我看不出与您提供的输出相匹配。不确定这是否重要。