选择与2列不同,但只有1列重复

时间:2012-11-16 13:48:31

标签: postgresql join duplicates distinct subquery

select a.subscriber_msisdn, war.created_datetime from 
(
select distinct subscriber_msisdn from wiz_application_response 
where application_item_id in 
(select id from wiz_application_item  where application_id=155)
and created_datetime between '2012-10-07 00:00' and '2012-11-15 00:00:54'
) a
 left outer join wiz_application_response war on (war.subscriber_msisdn=a.subscriber_msisdn)

子选择返回11行但加入时返回18(带有重复项)。此查询的目的是仅将日期列添加到子选择的11行。

2 个答案:

答案 0 :(得分:0)

根据您的描述,可以确定某些subscriber_msisdn值有多个created_datetime值,这些值促使您在子查询中使用distinct来开始。通过将子查询连接到原始表,您正在击败它。编写查询的更简洁方法是:

SELECT
    war.subscriber_msisdn
  , war.created_datetime
FROM
  wiz_application_response war
  LEFT JOIN wiz_application_item  wai
    ON war.application_item_id = wai.id
    AND wai.application_id = 155
WHERE
  war.created_datetime BETWEEN '2012-10-07 00:00' AND '2012-11-15 00:00:54'

这应该只返回war表中满足基于wai表的条件的行。它不应该是和外部连接,除非您想要从war表中返回满足created_datetime参数的所有行,而不管application_item_id参数如何。

这是我最好的猜测,基于我对你的桌子的有限信息以及我假设你想要完成的事情。如果这不能满足您的需求,我会根据您提供的其他信息继续提供其他想法。希望这有效。

答案 1 :(得分:0)

最有可能简化为:

SELECT DISTINCT ON (1)
       r.subscriber_msisdn, r.created_datetime
FROM   wiz_application_item i
JOIN   wiz_application_response r ON r.application_item_id = i.id
WHERE  i.application_id = 155
AND    i.created_datetime BETWEEN '2012-10-07 00:00' AND '2012-11-15 00:00:54'
ORDER  BY 1, 2 DESC  -- to pick the latest created_datetime

详细信息取决于缺少的信息。

More explanation here.