Multiple unwanted records in Group by clause in Postgress

前端 未结 1 1482
情深已故
情深已故 2021-01-28 13:33

I have two table and I am joining them together then running a group by clause. The problem is that I keep getting unwanted data.

client table
-----         


        
相关标签:
1条回答
  • 2021-01-28 14:07

    If you want the first one in each company, then use distinct on. This is a nice construct available only in Postgres:

    SELECT DISTINCT ON (co.name) co.name, cl.name, cl.created_at
    FROM company co INNER JOIN
         client cl
         ON cl.company_id = co.id
    ORDER BY co.name, cl.created_at asc;
    
    0 讨论(0)
提交回复
热议问题