我在Postgresql中有两个表,我试图获取按地点重复标签的次数。
我已经提出了这个问题:
SELECT tweets_with_location.user_location,
tweets_with_location.my_new_id,
all_hashtags_with_location.regexp_split_to_table
FROM tweets_with_location, all_hashtags_with_location
WHERE tweets_with_location.my_new_id = all_hashtags_with_location.my_new_id;
返回位置,推文ID和主题标签:
USER_LOCATION | MY_NEW_ID | HASHTAG
纽约州纽约市| 33 |快乐
纽约州纽约市| 40 | BigApple
布朗克斯,纽约| 12 |快乐
布朗克斯,纽约| 45 |快乐
纽约皇后区| 23 |王牌
纽约皇后区| 20 |王牌
然后,我做了另一个SQL查询,但它似乎并没有总结按地点显示主题标签的次数,Count值始终为1:
SELECT tweets_with_location.user_location,
all_hashtags_with_location.regexp_split_to_table,
COUNT(DISTINCT all_hashtags_with_location.regexp_split_to_table) AS CountOf
FROM tweets_with_location, all_hashtags_with_location
WHERE tweets_with_location.my_new_id = all_hashtags_with_location.my_new_id
GROUP BY tweets_with_location.user_location,
all_hashtags_with_location.regexp_split_to_table
ORDER BY CountOf DESC;
我需要的是这个结果:
USER_LOCATION - HASHTAG - COUNT
纽约州纽约市|快乐| 1
布朗克斯,纽约|快乐| 2
纽约皇后区|特朗普| 2
纽约州纽约市|快乐| 1
我该怎么做?我的SQL查询有什么问题?
答案 0 :(得分:1)
你真的很亲密,你在计算错误的字段:
SELECT tweets_with_location.user_location,
all_hashtags_with_location.regexp_split_to_table,
COUNT(DISTINCT tweets_with_location.my_new_id) AS CountOf
FROM tweets_with_location, all_hashtags_with_location
WHERE tweets_with_location.my_new_id = all_hashtags_with_location.my_new_id
GROUP BY tweets_with_location.user_location,
all_hashtags_with_location.regexp_split_to_table
ORDER BY CountOf DESC;
答案 1 :(得分:1)
或者只是删除COUNT()函数中的DISTINCT限定符。