distinct on postgres code example

Example 1: postgresql distinct

SELECT
	DISTINCT bcolor
FROM
	distinct_demo
ORDER BY
	bcolor;

Example 2: postgresql left join distinct on

Select Distinct On (u.username, u.email)
     u.username
    ,u.email
    ,l.browser
    ,l.login_time
From users u
Join logins l On l.username = u.username
Order By u.username, u.email, login_time Desc
 
                                        ^

Example 3: postgres count distinct

SELECT 
   COUNT(DISTINCT column) 
FROM 
   table_name
WHERE
   condition;

Example 4: array aggre distinct postgres

SELECT ARRAY(SELECT DISTINCT e FROM unnest(ARRAY[a,b,c,d]) AS a(e))
FROM ( VALUES
  ('foo', 'bar', 'foo', 'baz' )
) AS t(a,b,c,d);

Tags:

Sql Example