在concat查询中应用distinct

时间:2015-09-30 10:37:41

标签: postgresql

我为三个表编写了一个连接查询,连接这三个表的列。但是在最后一个表keywords列中只有一个值,但在重复汇总时会重复该值。

SELECT
  g.goal_id,
  concat(coalesce(rk.keyword, ''),
         coalesce(gr.description, ''),
         coalesce(cd.keywords, ''))
FROM goal g
  LEFT OUTER JOIN resource_keyword rk ON (g.creater_id = rk.refference_id AND
                                          g.creater_id = :userId)
  LEFT OUTER JOIN groups gr ON gr.creater_id = g.creater_id
  LEFT OUTER JOIN communication_detail cd ON cd.party_id = gr.creater_id
LIMIT 5

实际结果:

      goal_id      concat

      296          noseDaily walk,sad,err
      296          Healthy is a goalDaily walk,sad,err
      296          Healthylife a healthy dayDaily walk,sad,err
      296          tasty foodDaily walk,sad,err
      296          healthygradeDaily walk,sad,err

预期结果:

      goal_id       concat

      296          noseDaily walk,sad,err
      296          Healthy is a goalDaily walk
      296          Healthylife a healthy dayDaily walk
      296          tasty foodDaily walk
      296          healthygradeDaily walk

这里很遗憾,错误属于第三个表,只有一个值用逗号分隔,但它会被附加到每一行。

0 个答案:

没有答案