表中的重复行

时间:2017-02-23 05:44:34

标签: sql sql-server

我想在表格中找到总计数和不同计数,其中没有单个列具有唯一记录

任何人都可以帮我写一个针对性能量身定制的查询。

2 个答案:

答案 0 :(得分:0)

GROUP BYHAVING COUNT(*) > 1所有列,并按SELECT ColA ,ColB ,ColC ,COUNT(*) AS Number_Of_Rows FROM MyTable GROUP BY ColA ,ColB ,ColC HAVING COUNT(*) > 1 过滤:

func addSpaces(to givenString: String) -> String{
   var string = givenString

   //indexOffset is needed because each time replaceSubrange is called, the resulting count is incremented by one (owing to the fact that a space is added to every capitalised letter)
   var indexOffset = 0
   for (index, character) in string.characters.enumerated(){
       let stringCharacter = String(character)

       //Evaluates to true if the character is a capital letter
       if stringCharacter.lowercased() != stringCharacter{
           guard index != 0 else { continue } //"ILoveSwift" should not turn into " I Love Swift"
           let stringIndex = string.index(string.startIndex, offsetBy: index + indexOffset)
           let endStringIndex = string.index(string.startIndex, offsetBy: index + 1 + indexOffset)
           let range = stringIndex..<endStringIndex
           indexOffset += 1
           string.replaceSubrange(range, with: " \(stringCharacter)")
       }
   }
   return string
}

答案 1 :(得分:0)

使用COUNT(1)表示总计数,COUNT(DISTINCT ..)表示不同计数。

SELECT COUNT(1) AS [TotalCount],
       COUNT(DISTINCT ColA) AS [ColACount]
FROM [YourTable]