在具有重复行的SQL Server表中按组查找行号

时间:2015-05-18 22:16:50

标签: sql sql-server sql-server-2008

我需要在一个包含重复的表中按组计算行数。

表:

 id, value1, value2
 1   972      5      
 1   972      10     

SQL:

package checkers;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import javafx.scene.Scene  ;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.application.*;
import javafx.stage.*;


public class Checkers extends Application {

    Stage window;

    public void start(Stage primaryStage) throws Exception {

        window = primaryStage;
        window.setTitle("Title");

        HBox layout = new HBox();
        Button  startButton = new Button("Start");
        Button quitButton = new Button("Quit");
        layout.getChildren().addAll(startButton, quitButton);

        Scene startScene = new Scene(layout, 400, 300);

        window.setScene(startScene);
        window.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

代码仅计算重复的行。 我需要:

$(function() {
  $('input').on('keypress', function(e) {
    var char = String.fromCharCode(e.which);
    var caret = $(this).caret();
    // var caret = this.selectionStart;  // Not supported in IE <= 8 (but who cares...)
    if((caret < 5 && !/^\d$/.test(char)) ||
       (caret >= 5 && !/^[a-zA-Z]$/.test(char))) {e.preventDefault()}
  });
});

我不需要计算重复的行,我只需要value1在value2列中有多个不同值的行。

由于

6 个答案:

答案 0 :(得分:2)

使用DISTINCT

select id, value1, count(distinct value2) cnt
from table
group by id, value1
having count(distinct value2)  > 1 

如果你想要分居,那么:

select * from table t1
cross apply(select cnt from(
                            select count(distinct value2) cnt
                            from table t2 
                            where t1.id = t2.id and t1.value1 = t2.value1) t 
            where cnt > 1)ca

答案 1 :(得分:1)

在SQL Server 2008中,您可以使用技巧使用窗口函数计算不同的值。您可能会发现这是一个很好的解决方案:

select t.id, t.value1, t.value2
from (select t.*, sum(case when seqnum = 1 then 1 else 0 end) over (partition by value1) as numvals
      from (select t.*, row_number() over (partition by value1, value2 order by (select null)) as seqnum
            from table t
           ) t
     ) t
where numvals > 1;

答案 2 :(得分:0)

在没有GROUP BY

的情况下尝试这种方式
 select id, value1, value2
 FROM table AS T1
 where 1 < (
   select  COUNT(*) 
   FROM table AS T2
   where T1.value1 = T2.value1)

答案 3 :(得分:0)

试试这个

;WITH    CTE
          AS ( SELECT   id ,
                        value1 ,
                        value2 ,
                        COUNT(*) cnt
               FROM     table
               GROUP BY id ,
                        value1 ,
                        value2
               HAVING   COUNT(*) > 1
             )
    SELECT  *
    FROM    table1
    WHERE   value1 IN ( SELECT  value1
                        FROM    CTE )

答案 4 :(得分:0)

只需在NOT之后使用HAVING,即可准确地获取不重复的行。

 select id, value1, value2
 FROM [table]
 group by id, value1, value2
 having NOT COUNT(*)  > 1 

Fiddle here

答案 5 :(得分:0)

如果您想要表中的实际行,而不仅仅是符合条件的id, value1对,您可以这样做:

WITH discrepancies AS (
  SELECT,
    id,
    value1,
    value2,
    distinctcount = COUNT(DISTINCT value2) OVER (PARTITION BY id, value1)
  FROM
    dbo.atable
)
SELECT
  id,
  value1,
  value2
FROM
  discrepancies
WHERE
  distinctcount > 1
;

如果 SQL Server 2008支持COUNT(DISTINCT ...) OVER clause

基本上,与Giorgi Nakeuri's one或多或少的想法相同,除非您不会多次击中表格。

唉,到目前为止还有no support for COUNT(DISTINCT ...) OVER ... in SQL Server。尽管如此,您仍然可以使用不同的方法,这仍然允许您只触摸一次表并返回详细信息行:

WITH discrepancies AS (
  SELECT,
    id,
    value1,
    value2,
    minvalue2 = MIN(value2) OVER (PARTITION BY id, value1),
    maxvalue2 = MAX(value2) OVER (PARTITION BY id, value1)
  FROM
    dbo.atable
)
SELECT
  id,
  value1,
  value2
FROM
  discrepancies
WHERE
  minvalue2 <> maxvalue2
;

这里的想法是每MIN(value2)获得MAX(value2)id, value1,看看它们是否有所不同。如果他们这样做,那意味着您在此id, value1子集中存在差异,并且您希望返回该行。

该方法利用带有OVER子句的聚合来避免自连接,这正是这里只访问一次表的原因。