获取最大计数SQL

时间:2018-03-14 14:31:01

标签: sql sql-server tsql

我遗漏了一些简单的东西,但我无法弄明白。

我需要基本上获得列的最大计数。

SELECT 
    S.Type AS Type, 
    S.Version, 
    COUNT(R.FK_StoveNbr) AS TimesRepaired

FROM STOVE AS S
FULL JOIN STOVE_REPAIR AS R 
ON S.SerialNumber = R.FK_StoveNbr

GROUP BY S.Type, S.Version
ORDER BY TimesRepaired DESC;

这为我提供了我需要的信息,但现在我需要这样做它只显示COUNT(R.FK_StoveNbr)中编号最大的Type。

一些信息:

[STOVE_REPAIR](
[RepairNbr] [int] NOT NULL,
[FK_StoveNbr] [int] NOT NULL,
[RepairDt] [smalldatetime] NOT NULL,
[Description] [varchar](500) NULL,
[Cost] [numeric](7, 2) NULL,
[FK_CustomerID] [int] NOT NULL,
[FK_EmpID] [int] NULL,

[STOVE](
[SerialNumber] [int] NOT NULL,
[Type] [char](15) NOT NULL,
[Version] [char](15) NULL,
[DateOfManufacture] [smalldatetime] NULL,
[Color] [varchar](12) NULL,
[FK_EmpId] [int] NULL,

需要输出:

 type        version   times repaired
 ---------    -----     --------------
 FiredAlways    2            2

我的输出:

 type            version   times repaired
 ---------        -----     --------------
 FiredAlways        2               2
 FiredAlways        1               1
 FiredNow           2               1 
 FiredNow           3               1
 FiredNow           1               1
 FiredAlways        4               0
 FiredAlways        5               0
 FiredAlways        3               0
 FiredAtCamp        3               0
 FiredAtCamp        1               0
 FiredAtCamp        2               0

3 个答案:

答案 0 :(得分:2)

使用TOP 1TOP (1) WITH TIES。当你有联系并且想要所有最高数字时使用后者。

但是,我对您使用FULL JOIN提出质疑。这不是你想要的吗?

SELECT TOP (1) S.Type AS Type, S.Version, 
       COUNT(R.FK_StoveNbr) AS TimesRepaired
FROM STOVE S INNER JOIN
     STOVE_REPAIR R 
     ON S.SerialNumber = R.FK_StoveNbr
GROUP BY S.Type, S.Version
ORDER BY TimesRepaired DESC;

因为您获得的人数最多,我认为我们可以假设至少有一份退回的炉灶维修记录。

答案 1 :(得分:1)

如果要选择计数最多的N条记录,可以执行以下操作:

;WITH CountResults AS
(
    SELECT 
        S.Type AS Type, 
        S.Version, 
        COUNT(R.FK_StoveNbr) AS TimesRepaired
    FROM 
        STOVE AS S
        FULL JOIN STOVE_REPAIR AS R ON S.SerialNumber = R.FK_StoveNbr
    GROUP BY 
        S.Type, 
        S.Version
),
MaxCountResults AS
(
    SELECT
        MaxCountResult = MAX(C.TimesRepaired)
    FROM
        CountResults AS C
)
SELECT
    C.Type,
    C.Version,
    C.TimesRepaired
FROM
    CountResults AS C
    INNER JOIN MaxCountResults AS M ON C.TimesRepaired = M.MaxCountResult
ORDER BY
    C.TimesRepaired DESC

基本上,您首先确定最高数字,然后按该数字过滤整个结果。

如果你只需要一个最高的,那么一个简单的TOP就足够了:

SELECT TOP 1
    S.Type AS Type, 
    S.Version, 
    COUNT(R.FK_StoveNbr) AS TimesRepaired
FROM 
    STOVE AS S
    FULL JOIN STOVE_REPAIR AS R ON S.SerialNumber = R.FK_StoveNbr
GROUP BY 
    S.Type, 
    S.Version
ORDER BY
    TimesRepaired DESC

答案 2 :(得分:0)

MAXCOUNT是两个不同的函数,它们返回两个不同的值。你说

  

我需要基本上获得列的最大计数。   这给了我所需要的信息,但现在我需要这样做   仅显示具有最高编号的类型   COUNT(R.FK_StoveNbr)

当你运行你引用的代码时......

SELECT 
 S.Type AS Type, 
 S.Version, 
 COUNT(R.FK_StoveNbr) AS TimesRepaired

FROM STOVE AS S
FULL JOIN STOVE_REPAIR AS R 
ON S.SerialNumber = R.FK_StoveNbr

GROUP BY S.Type, S.Version
ORDER BY TimesRepaired DESC;

您将获得一个具有“TYPE”,“VERSION”和“COUNT”的表 - 它表示该类型和版本在数据集中出现的次数。 你说你只需要1条记录......

type        version   times repaired
 ---------    -----     --------------
 FiredAlways    2            2

是否需要其他类型的其他“MAX”?比如...

type        version   times repaired
---------    -----     --------------
FiredAlways    2            2
FiredAtCamp        3               0
FiredNow           2               1

总有一些方法可以做,具体取决于设置方式和您真正想要的方式。这就是为什么我重申上面的内容并提出这些问题,只是为了清晰起见。 为了获得我发布的内容,我建议做的是加入SUBQUERY。子查询在哪里根据TYPE查找COUNT的MAX。然后使用TYPE和COUNT将此子查询连接回原始结果集 在我的头顶,它可能是...... ....

SELECT 
 S.Type AS Type, 
 S.Version, 
 COUNT(R.FK_StoveNbr) AS TimesRepaired

FROM STOVE AS S
FULL JOIN STOVE_REPAIR AS R 
  ON S.SerialNumber = R.FK_StoveNbr
INNER JOIN ( SELECT 
               S2.Type AS Type,  
               MAX(COUNT(R2.FK_StoveNbr)) AS MXTimesRepaired
             FROM STOVE AS S2
             FULL JOIN STOVE_REPAIR AS R2
               ON S2.SerialNumber = R2.FK_StoveNbr
           ) AS SI
ON S.Type = SI.Type

GROUP BY S.Type, S.Version
HAVING COUNT(R.FK_StoveNbr) = SI.MXTimesRepaired
ORDER BY TimesRepaired DESC;

我知道这会给你一个稍小的结果集,它有一个不同的集合,根据类型仍然有多个记录。 然而,在不了解其他一些领域的情况下,问题需要是“是什么让你记录的是你正在寻找MAX还是只需要记录?”是否有我们可以查看的ID字段或其他可能使我们想要查找该记录的内容?