获取SQL Server中具有最大记录数的组

时间:2016-03-24 19:08:25

标签: sql sql-server

[ID] [Name]   [Dept] 
--------------------
1     Manu      A   
2     Anu       A   
3     Tanu      A   
4     Danu      A   
5     Anu       B   
6     Danu      B   
7     Danu      C   
8     Anu       C   
9     Tanu      C   
10    John      C   
11    Anu       D   
12    Jane      D   
13    Danu      D 

我需要让部门拥有最多的员工。

这是我试过的

SELECT
    ID, Name, Dept
FROM
    (SELECT 
         *,
         rn = ROW_NUMBER() OVER(PARTITION BY Dept)
     FROM Emp) t
WHERE 
    rn = (SELECT MAX(rn) FROM t)

我需要WHERE子句中的帮助。

5 个答案:

答案 0 :(得分:2)

您需要汇总才能计算员工人数。使用row_number()的方法是一种方法,但使用正确的查询:

SELECT Dept
FROM (SELECT Dept, COUNT(*) as cnt,
             ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as seqnum
      FROM Emp
     ) e
WHERE seqnum = 1;

但是,更常见的方法是使用ORDER BYTOP

SELECT TOP (1) Dept, COUNT(*) as cnt
FROM emp
GROUP BY Dept
ORDER BY COUNT(*) DESC;

如果您想要关联,那么您可以在WITH TIES中使用SELECT

答案 1 :(得分:1)

不需要。尝试使用GROUP BY

public class Perimeter extends AppCompatActivity {
    private int number;
    private int number2;
    private String myString;
    private String myString2;
    private int perimeter;
    private Random rand;
    private Random rand2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perimeter);

        rand = new Random();
        rand2 = new Random();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    public void PerimeterGame(View view) {
        number = rand.nextInt(12) + 1;
        TextView myText = (TextView) findViewById(R.id.rand1);
        myString = String.valueOf(number);
        myText.setText(myString);

        number2 = rand2.nextInt(12) + 1;
        TextView myText2 = (TextView) findViewById(R.id.rand2);
        myString2 = String.valueOf(number2);
        myText2.setText(myString2);

        ((TextView) findViewById(R.id.question)).setText
        ("Find the perimeter of a rectange with a width of " + myString + "cm" + " and " + "length of " + myString2 + "cm" + ".");
    }

    public void AnswerCheck(View view) {
        EditText num = (EditText) findViewById(R.id.answertext);
        int val = Integer.parseInt(num.getText().toString());

        perimeter = (number + number2 + number + number2);
        if (val == perimeter) {
            Toast.makeText(this, "The answer is correct", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "The answer is incorrect ", Toast.LENGTH_SHORT).show();
        }
        findViewById(R.id.showsolbutton).setEnabled(true);
    }
}

最大的群体将位于顶部

结果

SELECT COUNT(Name) as NameCount, Dept from Table
GROUP BY Dept
ORDER BY COUNT(Name) DESC

答案 2 :(得分:1)

选择具有相同最大员工人数的所有部门:

;WITH c -- create a list of depts and number of emp's 
     AS (SELECT deptid, 
            Count(*) cnt 
         FROM   emp 
         GROUP  BY deptid) 
SELECT d.* 
FROM   dept d 
   INNER JOIN c 
           ON d.deptid = c.deptid  
WHERE  c.cnt = (SELECT Max(cnt) 
            FROM   c) 

答案 3 :(得分:1)

好的,现在你添加了表结构:

#include <math.h>

typedef struct {
    double r;       // ∈ [0, 1]
    double g;       // ∈ [0, 1]
    double b;       // ∈ [0, 1]
} rgb;

typedef struct {
    double h;       // ∈ [0, 360]
    double s;       // ∈ [0, 1]
    double v;       // ∈ [0, 1]
} hsv;

rgb hsv2rgb(hsv HSV)
{
    rgb RGB;
    double H = HSV.h, S = HSV.s, V = HSV.v,
            P, Q, T,
            fract;

    (H == 360.)?(H = 0.):(H /= 60.);
    fract = H - floor(H);

    P = V*(1. - S);
    Q = V*(1. - S*fract);
    T = V*(1. - S*(1. - fract));

    if      (0. <= H && H < 1.)
        RGB = (rgb){.r = V, .g = T, .b = P};
    else if (1. <= H && H < 2.)
        RGB = (rgb){.r = Q, .g = V, .b = P};
    else if (2. <= H && H < 3.)
        RGB = (rgb){.r = P, .g = V, .b = T};
    else if (3. <= H && H < 4.)
        RGB = (rgb){.r = P, .g = Q, .b = V};
    else if (4. <= H && H < 5.)
        RGB = (rgb){.r = T, .g = P, .b = V};
    else if (5. <= H && H < 6.)
        RGB = (rgb){.r = V, .g = P, .b = Q};
    else
        RGB = (rgb){.r = 0., .g = 0., .b = 0.};

    return RGB;
}

答案 4 :(得分:0)

我无法确定你的桌面结构,但这会选择员工最多的部门

SELECT * from Dept WHERE deptid = (
  SELECT TOP 1 deptid FROM employees
  GROUP BY deptid
  ORDER BY COUNT(*) DESC