Postgresql同一天至少有2个销售

时间:2013-07-02 13:33:33

标签: sql postgresql

抱歉我的英语 我必须至少2次获得具有相同日期的所有记录,我的意思是我想知道卖家在同一天至少进行了2次销售。

表A

id          Name    Lastname     Seller     Date     Item
23456     Roberto Rodriguez    jvazquez 01/01/2013   auto
23423     Roberto Rodriguez    jvazquez 01/01/2013   moto
5654      Julián Domínguez     rfleita  05/02/2013   lancha
34534653  Romina Santaolaya    jvazquez 02/02/2013   moto
346534    Romina Santaolaya    rfleita  05/02/2013   auto

结果

23456     Roberto Rodriguez    jvazquez 01/01/2013   auto
23423     Roberto Rodriguez    jvazquez 01/01/2013   moto
346534    Romina Santaolaya    rfleita  05/02/2013   auto
5654      Julián Domínguez     rfleita  05/02/2013   lancha

3 个答案:

答案 0 :(得分:3)

SELECT seller, Date, COUNT(*) "Sales"
FROM TableA
GROUP BY Date, seller
HAVING COUNT(*) >= 2

答案 1 :(得分:1)

select id, 
       Name,
       Lastname,
       Seller,
       Date,
       Item
from (
  select id, 
         Name,
         Lastname,
         Seller,
         Date,
         Item,
         count(*) over (partition by date, seller) as cnt
   from the_table
) t
where cnt >= 2;

SQLFiddle示例:http://www.sqlfiddle.com/#!12/7c0cc/4

如果您不需要额外的列,Joel的答案会更快。

答案 2 :(得分:0)

这应该会给你要求的结果:

SELECT *
    FROM TableA
    WHERE (Date, Seller) IN (
        SELECT Date, Seller
            FROM TableA
            GROUP BY Date, Seller
            HAVING COUNT(*) >= 2
    )