使用4个表获取数据

时间:2012-08-03 03:48:05

标签: sql postgresql

我有以下表格:

trendingtopic
  id
  name

trendingtopiccycle
  id
  trendingtopic_id(FK)

tweet
  id 
  text
  tt_cycle_id(FK)
  user_id(FK)

user
  id
  rank

我想知道是否有办法以有效的方式检索每个趋势主题的排名较高的用户发布的推文。

这是我当前的查询:

  SELECT tt."name",
  MAX(tu."actual_rank") AS rank,tu."name"
  FROM "trendingtopics_twitteruser" tu
  LEFT JOIN "trendingtopics_tweet" tw ON tu."id" = tw."user_id" 
  LEFT JOIN "trendingtopics_trendingtopiccycle" ttc ON tw."tt_cycle_id" = ttc."id"
  LEFT JOIN "trendingtopics_trendingtopic" tt ON ttc."tt_id" = tt."id" 
  GROUP BY tt."name"

它不起作用,因为根据Postgresql:'“tu.name”必须出现在GROUP BY子句中或用于聚合函数'。

如果我在GROUP BY子句中添加tu。“name”,我最终会得到类似这样的内容:

"106 & Park";0;"910Prince"
"106 & Park";0;"ActressAlexiss"
"106 & Park";0;"AmeliaThirlwall"
"106 & Park";0;"_ArielIvy"
"106 & Park";0;"_AyyeVce"
"106 & Park";0;"barcastuff"
"106 & Park";0.42141;"pareexo"
"106 & Park";0.0363;"khleosupporters"
"#15ThingsAboutMyCrush";0;"_ALoyalLady"
"#15ThingsAboutMyCrush";0.22275;"AmberrNikole"
"#15ThingsAboutMyCrush";0;"a_paigeturner"
"#15ThingsAboutMyCrush";0.33942018;"ArleneAndrea_xo"

但我想得到的是:

"106 & Park";0.42141;"pareexo"
"#15ThingsAboutMyCrush";0.33942018;"ArleneAndrea_xo"

3 个答案:

答案 0 :(得分:1)

实际上,这可以使用子查询中的窗口函数轻松完成:

SELECT t."topic_name", t."rank", t."user_name"
FROM 
    (SELECT tt."name" AS topic_name, tu."actual_rank" AS rank, tu."name" AS user_name,
        row_number() OVER (PARTITION BY tt."name" ORDER BY tu."actual_rank" DESC) user_rank
    FROM "trendingtopics_trendingtopic" tt
    LEFT JOIN "trendingtopics_trendingtopiccycle" ttc ON ttc."tt_id" = tt."id"
    LEFT JOIN "trendingtopics_tweet" tw ON tw."tt_cycle_id" = ttc."id"
    LEFT JOIN "trendingtopics_twitteruser" tu ON tu."id" = tw."user_id") t
WHERE t."user_rank" = 1

此外,我重新排序了连接,以便您开始使用趋势主题而不是Twitter用户。既然您正在尝试为趋势主题获得排名最高的用户,那么(至少对我来说)更有意义的是将其作为源表开始。

有关窗口功能和分区的更多信息,请访问:http://www.postgresql.org/docs/8.4/static/tutorial-window.html

答案 1 :(得分:1)

最快的解决方案可能是使用DISTINCT ON而不是窗口函数。

SELECT DISTINCT ON (tt."name")
    tt."name", tu."actual_rank" AS rank, tu."name"
  FROM "trendingtopics_twitteruser" tu
  LEFT JOIN "trendingtopics_tweet" tw ON tu."id" = tw."user_id" 
  LEFT JOIN "trendingtopics_trendingtopiccycle" ttc ON tw."tt_cycle_id" = ttc."id"
  LEFT JOIN "trendingtopics_trendingtopic" tt ON ttc."tt_id" = tt."id" 
  ORDER BY tt."name", tu."actual_rank" DESC;

顺便说一下,这是未经测试的,因为您没有提供语句来实际创建和加载您描述的表。如果您在问题中提供了这类内容,人们通常会在发布之前测试他们的答案,并且您不会遇到愚蠢的错误。

答案 2 :(得分:0)

SELECT tt."name",
  MAX(tu."actual_rank") AS rank,MAX(tu."name") as name_1
  FROM "trendingtopics_twitteruser" tu
  LEFT JOIN "trendingtopics_tweet" tw ON tu."id" = tw."user_id" 
  LEFT JOIN "trendingtopics_trendingtopiccycle" ttc ON tw."tt_cycle_id" = ttc."id"
  LEFT JOIN "trendingtopics_trendingtopic" tt ON ttc."tt_id" = tt."id" 
  GROUP BY tt."name"