sql distinct需要多个字段

时间:2015-06-15 19:36:33

标签: sql sybase advantage-database-server

我正在使用Sybase的Advantage Database Server。我需要从我的报告中删除重复的addbatch,但无法提取不同的记录。知道我错过了什么吗?

这是我正在使用的

SELECT  DISTINCT
    SI.[addbatch] as [Batch#],
    SI.[current account #] as [Account],
    SI.[status date] as [Status Date],
    SI.[SKU] as [SKU],
    AC.[email address] as [Email]
FROM  salesinventory SI, accounts AC
WHERE  AC.[account #]=SI.[current account #] and [Status Date] > '6/1/2015'

我仍然会收到重复的补丁。我不确定我哪里错了!提前致谢!甚至不确定如何谷歌这个问题!

3 个答案:

答案 0 :(得分:0)

问题是您需要检查单个列的唯一性,而这实际上并不是您的代码执行的内容。试试这个

SELECT  *
FROM    (SELECT SI.[addbatch] as [Batch#],
         SI.[current account #] as [Account],
         SI.[status date] as [Status Date],
         ETC,
             ROW_NUMBER() OVER (PARTITION BY [Batch#]) AS RowNumber
     FROM   salesinventory SI, accounts AC
     WHERE  AC.[account #]=SI.[current account #] and [Status Date] > '6/1/2015') as rec
WHERE rec.RowNumber = 1

答案 1 :(得分:0)

- 以下代码对于删除记录是通用的,可根据需要进行修改。

select  x.[Well_Name] as nameX
      , x.[TestDate]  as dateX
from (
SELECT count(*) as dup
      ,[Well_Name]

      ,[TestDate]enter code here
  FROM [dbo].[WellTests]
  group by  [TestDate] ,[Well_Name] ) x
  where dup > 1

答案 2 :(得分:0)

如果您希望在结果中包含唯一的批次编号,则必须批处理字段

这样的事情应该有效:

SELECT
    SI.[addbatch] as [Batch#],
    MIN(SI.[current account #]) as [Account],
    MIN(SI.[status date]) as [Status Date],
    MIN(SI.[SKU]) as [SKU],
    MIN(AC.[email address]) as [Email]
FROM  salesinventory SI, accounts AC
WHERE  AC.[account #]=SI.[current account #] and [Status Date] > '6/1/2015'
GROUP BY
  SI.[addbatch]

你没有说明你想要aggregate其他列的方式,只需将MIN替换为对你更有意义的内容,例如SUMCOUNT,等

有一个关于grouping in the documentation的主题。

PS:SELECT DISTINCT(基本上)只是在所有列GROUP BY的较短路径,没有任何聚合