0

我正在汇总数据,我无法对某些列求和,所以我想从该列中获取最频繁的观察值或中值。示例如下,在此先感谢。

身份证网站

1 3

1 3

1 2

1 3

2 4

2 5

2 5

2 5

我希望它看起来像

身份证网站

1 3

2 5

4

1 回答 1

0
WITH temp AS(
  SELECT ID, Site, COUNT(*) As counts
  FROM id_table 
  GROUP BY ID, Site
)
SELECT temp.ID, temp.Site
FROM temp
JOIN (SELECT ID, MAX(counts) max_counts
      FROM temp 
      GROUP BY ID
     )b
ON temp.ID = b.ID
AND temp.counts = b.max_counts
ORDER BY ID ASC

SQL小提琴

于 2013-06-12T19:28:14.193 回答