我不相信我需要做的事情太复杂了,但是嘿,这是星期一早上我可以使用一些建议。基本上我在SQL Server中以下列格式进行聚合查询:
SELECT [Data1], COUNT(*), MAX([Data2])
FROM [Source]
GROUP BY [Data1]
但是我也需要第4场。一个字段,计算值MAX([Data2])
出现的次数。
我将在这里做一个简单的例子:
|Data 1|Data 2|
|1 |x |
|3 |p |
|1 |z |
|3 |f |
|1 |x |
|1 |b |
|2 |h |
|1 |o |
|2 |h |
|1 |x |
|3 |f |
|2 |h |
|1 |z |
需要产生输出:
|Data1|Count|Max|Occurances|
|1 |7 |x |3 |
|2 |3 |h |3 |
|3 |3 |f |2 |
任何帮助,将不胜感激。
您正在寻找的是统计中的名称。您需要模式的模式和频率。
我会使用两级聚合来处理这个问题:
select data1, sum(cnt) as cnt,
max(case when seqnum = 1 then data2 end) as mode,
max(cnt) as mode_cnt
from (select data1, data2, count(*) as cnt,
row_number() over (partition by data1 order by count(*) desc) as seqnum
from t
group by data1, data2
) t
group by data1;
这样的事情应该有所帮助 -
;WITH CTE AS
(SELECT [Data1], COUNT(*) AS Data_Count, MAX([Data2]) AS Data_Max
FROM [Source]
GROUP BY [Data1])
SELECT [Data1],Data_Count,Data_Max,COUNT(Data_Max) AS Occurances
FROM CTE
GROUP BY [Data1],Data_Count,Data_Max
注意:我会避免使用SQL Keywords
作为列名/别名。
您可以尝试使用cte
to的窗口功能。
使用CTE
通过COUNT
和Occurances
专栏获得[Data 1]
[Data 2]
。然后使用另一个CTE2
通过Occurances
desc生成行号,这意味着最大金额。
;with cte as (
SELECT [Data 1],
[Data 2],
COUNT(*) OVER (PARTITION BY [Data 1] ORDER BY [Data 1]) [count],
COUNT(*) OVER (PARTITION BY [Data 2],[Data 1] ORDER BY [Data 2]) Occurances
FROM T
),cte2 as (
SELECT
[Data 1],
[Data 2],
[count],
Occurances,row_number() over(partition by [Data 1] order by Occurances desc) rn
FROM cte
)
select [Data 1],
[Data 2],
[count],
Occurances
from cte2
where rn = 1
结果
Data 1 Data 2 count Occurances
1 x 7 3
2 h 3 3
3 f 3 2
你可以试试这个。
DECLARE @Source AS TABLE([Data1] INT,[Data2] VARCHAR(5))
INSERT INTO @Source VALUES
(1, 'x'),
(3, 'p'),
(1, 'z'),
(3, 'f'),
(1, 'x'),
(1, 'b'),
(2, 'h'),
(1, 'o'),
(2, 'h'),
(1, 'x'),
(3, 'f'),
(2, 'h'),
(1, 'z')
;WITH T AS (
SELECT [Data1] ,
COUNT(*) OVER(PARTITION BY [Data1]) [Count],
[Data2],
COUNT(*) OVER(PARTITION BY [Data2]) Occurances
FROM @Source
)
SELECT TOP 1 WITH TIES [Data1], [Count], [Data2] [Max], Occurances
FROM T
ORDER BY ROW_NUMBER() OVER (PARTITION BY [Data1] ORDER BY Occurances DESC)
结果:
Data1 Count Max Occurances
----------- ----------- ----- -----------
1 7 x 3
2 3 h 3
3 3 f 2