你好......我有一张带有family_id和city的表。我需要一个城市用于每个家庭id,其中大多数人住在这里

时间:2016-01-24 00:14:35

标签: sql oracle-sqldeveloper

enter image description here

SELECT
  family_id,
  MIN((city)) keep (DENSE_RANK FIRST ORDER BY COUNT (city) DESC)
FROM FAMILY_MEMBERS
GROUP BY family_id;
  

错误:ORA-00979:不是GROUP BY表达式   00979. 00000 - “不是GROUP BY表达式”   *原因:
  *行动:   行错误:116列:24

Family_members

1 个答案:

答案 0 :(得分:-1)

您不能将窗口函数(dense_rank())与GROUP BY一起使用。如果您需要同时执行这两项操作,则必须进行子选择。

编辑: 这是我的建议:

SELECT y.family_id, y.City
FROM 
(SELECT x.family_id, x.City, x.Member_count,
row_number() OVER (PARTITION BY x.family_id ORDER BY x.Member_count DESC) rn
FROM
(SELECT family_id, City, COUNT(*) Member_count
FROM FAMILY_MEMBERS
GROUP BY family_id, City) x) y
WHERE y.rn = 1;