SQL Server查询:如何获取特定分组的最大数量?

时间:2018-12-18 20:19:48

标签: sql sql-server group-by count max

我们有一个包含以下信息的表:

帐户ID,触摸号,类型,触摸日期和阶段(如果触摸号<50,则排名1-3,如果触摸51-100,则排名2,如果> 100,则排名3)。

Screenshot from table

我希望编写一个查询来捕获每个帐户在每个阶段接触最多的类型,如下所示:

Output I am looking to receive

这是我写的当前对我不起作用的查询:

`SELECT distinct
a.[Account ID],
a.Stage,
bb.Stage1TopType,
bb.TypeCount_1,
c.Stage2TopType,
c.TypeCount_2,
d.Stage3TopType,
d.TypeCount_3
FROM SFAX.dbo.LinearTest as a

--STAGE 1
LEFT JOIN 
(
SELECT 
a.[Account ID],
a.Type as Stage1TopType,
Max(b.TouchCount) as TypeCount_1
FROM SFAX.dbo.LinearTest as a

LEFT JOIN 
(
SELECT
[Account ID],
Type,
COUNT(TouchNumber) as TouchCount
FROM SFAX.dbo.LinearTest 
WHERE Stage = 1
GROUP BY [Account ID], Type
) as b on a.[Account ID] = b.[Account ID]

WHERE a.Stage = 1
GROUP BY a.[Account ID], a.Type

) as bb on a.[Account ID] = bb.[Account ID]


--STAGE 2
LEFT JOIN 
(
SELECT 
a.[Account ID],
a.Type as Stage2TopType,
Max(b.TouchCount) as TypeCount_2
FROM SFAX.dbo.LinearTest as a

LEFT JOIN 
(
SELECT
[Account ID],
Type,
COUNT(TouchNumber) as TouchCount
FROM SFAX.dbo.LinearTest 
WHERE Stage = 2
GROUP BY [Account ID], Type
) as b on a.[Account ID] = b.[Account ID]

WHERE a.Stage = 2
GROUP BY a.[Account ID], a.Type

) as c on a.[Account ID] = c.[Account ID]


--STAGE 3
LEFT JOIN 
(
SELECT 
a.[Account ID],
a.Type as Stage3TopType,
Max(b.TouchCount) as TypeCount_3
FROM SFAX.dbo.LinearTest as a

LEFT JOIN 
(
SELECT
[Account ID],
Type,
COUNT(TouchNumber) as TouchCount
FROM SFAX.dbo.LinearTest 
WHERE Stage = 3
GROUP BY [Account ID], Type
) as b on a.[Account ID] = b.[Account ID]

WHERE a.Stage = 3
GROUP BY a.[Account ID], a.Type

) as d on a.[Account ID] = d.[Account ID]

`

如果您对我如何获得所需的输出有任何建议,请告诉我。

2 个答案:

答案 0 :(得分:1)

我相信一个简单的ROW NUMBER窗口函数就足够了。

;WITH MostTouchesByAccountStage AS
(
    SELECT
        T.[Account ID],
        T.Stage,
        T.TouchNumber,
        T.Type,
        T.TouchDate,
        Ranking = ROW_NUMBER() OVER ( -- Generate a ranking
            PARTITION BY
                T.[Account ID], -- That will reset with each different value of Account and Stage
                T.Stage
            ORDER BY
                T.TouchNumber DESC) -- And is ordered by TouchNumber descendently
    FROM
        YourTable AS T      
)
SELECT
    T.*
FROM
    MostTouchesByAccountStage AS T
WHERE
    T.Ranking = 1

答案 1 :(得分:0)

  

我正在寻找的是出现次数最多的Type   每个阶段的时间。

您将需要首先执行计数,然后确定其中哪个值最高。在下面,您将看到在“派生表”中执行的计数,然后使用row_number()将值1分配给最高计数,最后我们只返回该值1的te行。

SELECT
    [Account ID]
  , Stage
  , TouchNumber
  , Type
  , TouchDate
  , type_count
FROM (
    SELECT
        [Account ID]
      , Stage
      , TouchNumber
      , Type
      , TouchDate
      , type_count
      , ROW_NUMBER() OVER (PARTITION BY [Account ID], Stage ORDER BY type_count DESC, Type) AS rn
    FROM (
        SELECT
            [Account ID]
          , Stage
          , TouchNumber
          , Type
          , TouchDate
          , COUNT( * ) OVER (PARTITION BY [Account ID], Stage, Type) AS type_count
        FROM YourTable AS T
    ) sq
) d
WHERE rn = 1
ORDER BY
    [Account ID]
  , Stage

nb。可能会有不止一列具有相同的高值,但只能返回一行,如果您希望将多列用于平局,请使用dense_rank()