根据计数选择最大值

时间:2011-10-17 15:33:15

标签: sql sql-server

如何根据firmid检索每个ValueCount的最大值。我需要像这样输出数据。

我的代码在

下面
SELECT
 F.FirmID,
 F.Name,
 DL.ValueId,
 DL.ValueName,
 count(DL.ValueName) AS ValueCount
 FROM 
dbo.Jobs AS J
 INNER JOIN DimensionValues AS DV ON
DV.CrossRef = J.JobId
 INNER JOIN dbo.DimensionLists AS DL ON
DV.ValueId = DL.ValueId
 INNER JOIN Firms AS F ON
F.FirmId =  J.ClientFirmId
 WHERE
DL.DimensionId = 4
  GROUP BY
F.FirmID,
F.Name,
DL.ValueName,
DL.ValueId

这会产生类似

的东西
firmid | value | count
1        1       5
1        2       10
2        3       1
2        1       6

我需要用10和6返回记录。

4 个答案:

答案 0 :(得分:0)

编辑: SQL 2005答案已删除。

然后你可以将结果推送到临时表(或表变量)并执行类似的操作......

SELECT
  *
FROM
  TempTable
WHERE
  ValueCount = (SELECT MAX(ValueCount) FROM TempTable AS Lookup WHERE FirmID = TempTable.FirmID)

或者...

SELECT
  *
FROM
  TempTable
INNER JOIN
  (SELECT FirmID, MAX(ValueCount) AS ValueCount FROM TempTable GROUP BY FirmID) AS lookup
    ON  lookup.FirmID     = TempTable.FirmID
    AND lookup.ValueCount = TempTable.ValueCount

如果任何ValueCount与另一个相同的FirmID绑定,则这些将提供多条记录。因此,你可以试试这个......

SELECT
  *
FROM
  TempTable
WHERE
  value = (
            SELECT TOP 1
              value
            FROM
              TempTable as lookup
            WHERE
              FirmID = TempTable.FirmID
            ORDER BY
              ValueCount DESC
          )

答案 1 :(得分:0)

对于此问题,您需要生成查询的结果集以确定Max ValueCount,然后您需要再次执行查询以仅使用Max ValueCount提取记录。您可以通过多种方式执行此操作,例如将主查询重复为子查询,以及在SQL Server 2005/2008中使用CTE。我认为使用子查询会有点混乱,并且更喜欢CTE,但对于SQL Server 2000,您没有选择它。所以,我使用临时表而不是CTE。我运行一次以获取MaxValueCount并将其保存到临时表中,然后再次运行查询并加入临时表以获取MaxValueCount的记录。

create table #tempMax
(
    FirmID int,
    MaxValueCount int
)

insert #tempMax
SELECT t.FirmID, MAX(t.ValueCount) AS MaxValueCount
  FROM (
SELECT F.FirmID, F.Name, DL.ValueId, DL.ValueName
     , count(DL.ValueName) AS ValueCount
  FROM dbo.Jobs AS J
 INNER JOIN DimensionValues AS DV ON DV.CrossRef = J.JobId
 INNER JOIN dbo.DimensionLists AS DL ON DV.ValueId = DL.ValueId
 INNER JOIN Firms AS F ON F.FirmId =  J.ClientFirmId
 WHERE DL.DimensionId = 4 
 GROUP BY F.FirmID, F.Name, DL.ValueName, DL.ValueId) t

SELECT t.FirmID, t.Name, t.ValueID, t.ValueName, t.ValueCount
  FROM (
SELECT F.FirmID, F.Name, DL.ValueId, DL.ValueName
     , count(DL.ValueName) AS ValueCount
  FROM dbo.Jobs AS J
 INNER JOIN DimensionValues AS DV ON DV.CrossRef = J.JobId
 INNER JOIN dbo.DimensionLists AS DL ON DV.ValueId = DL.ValueId
 INNER JOIN Firms AS F ON F.FirmId =  J.ClientFirmId
 WHERE DL.DimensionId = 4 
 GROUP BY F.FirmID, F.Name, DL.ValueName, DL.ValueId) t
 INNER JOIN #tempMax m ON t.FirmID = m.FirmID and t.ValueCount = m.MaxValueCount

DROP TABLE #tempMax

答案 2 :(得分:0)

您应该可以使用派生表:

SELECT  F.FirmID, 
        F.Name, 
        DL.ValueId, 
        DL.ValueName, 
        T.ValueCount    

FROM    Jobs J
        INNER JOIN DimensionValues DV
            ON DV.Crossref = J.JobID
        INNER JOIN DimensionList DL
            ON DV.ValueID = DL.ValueID
        INNER JOIN Firms F
            ON F.FirmID = J.ClientFirmID

        --derived table
        INNER JOIN (SELECT FirmID, MAX(ValueName) ValueCount FROM DimensionList GROUP BY FirmID) T
            ON T.FirmID = F.FirmID

WHERE  DL.DimensionId = 4 

答案 3 :(得分:0)

TBL1和TBL2是您的查询:

SELECT * 来自TBL1 哪里 TBL1.ValueCount =(SELECT MAX(TBL2.ValueCount)FROM TBL2 WHERE TBL2.FIRMID = TBL1.FIRMID)