如何在SQL / Impala中的列中显示唯一值

时间:2018-05-03 22:00:22

标签: sql impala

我有一个高级查询,其中我想显示列“vq5p1.message”的唯一值。我怎么能这样做?

我的查询:

SELECT th.hashtag_id,
       COUNT(th.hashtag_id) as count_hashtags, vq5p1.message
  FROM tweet_hashtag th
  JOIN tweet t
    ON t.tweet_id = th.tweet_id
  JOIN virtualq5p1 vq5p1
    ON vq5p1.tweet_id = th.tweet_id
  JOIN hashtag_fc fc
    ON fc.hashtag_id = vq5p1.hashtag_id
  JOIN game g
    ON g.fc_id1 = fc.fc_id
    OR g.fc_id2 = fc.fc_id 
  WHERE NOT EXISTS (SELECT 1
                     FROM virtualq5p1 vq5p2
                    WHERE vq5p2.hashtag_id = th.hashtag_id
                      AND vq5p2.tweet_id = th.tweet_id)
   AND t.created_time >= g.official_start
   AND t.created_time <= g.official_end
GROUP BY th.hashtag_id, vq5p1.message
ORDER BY COUNT(th.hashtag_id) DESC
LIMIT 10;

注意: vq5p1是一个视图。

现在我只得到不正确的重复结果:

Screenshot of what I am getting now

2 个答案:

答案 0 :(得分:1)

因此,为了获得唯一的消息,我创建了以下视图:

SELECT th.hashtag_id,
       COUNT(th.hashtag_id) as count_hashtags, vq5p1.message
  FROM tweet_hashtag th
  JOIN tweet t
    ON t.tweet_id = th.tweet_id
  JOIN virtualq5p1 vq5p1
    ON vq5p1.tweet_id = th.tweet_id
  JOIN hashtag_fc fc
    ON fc.hashtag_id = vq5p1.hashtag_id
  JOIN game g
    ON g.fc_id1 = fc.fc_id
    OR g.fc_id2 = fc.fc_id 
  WHERE NOT EXISTS (SELECT 1
                     FROM virtualq5p1 vq5p3
                    WHERE vq5p3.hashtag_id = th.hashtag_id
                      AND vq5p3.tweet_id = th.tweet_id)
   AND t.created_time >= g.official_start
   AND t.created_time <= g.official_end
GROUP BY th.hashtag_id, vq5p1.message
ORDER BY COUNT(th.hashtag_id) DESC
LIMIT 10;

然后使用视图中的结果和原始“消息”列所在的表。

select vq5p2.hashtag_id, vq5p2.count_hashtags, ht.message
from hashtag ht
JOIN virtualq5p2 vq5p2
ON vq5p2.hashtag_id = ht.hashtag_id

答案 1 :(得分:0)

也许你想要:

SELECT vq5p1.message, COUNT(*) as count_hashtags
. . .
GROUP BY vq5p1.message
ORDER BY COUNT(*) DESC

这将为每条消息返回一行,并为其分配散列标记的数量(我认为)。

相关问题