Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
Create table Agents ( Agent char(1), [Location] int ); insert into Agents VALUES ('A', 1), ('A', 2), ('A', 6), ('B', 3), ('B', 4), ('C', 1), ('C', 4), ('C', 5); select t.Agent, STRING_AGG(t.Location, ',') WITHIN GROUP (ORDER BY Location ASC) as Locations from ( select Agent, Location, rank() over (partition by [Location] order by Agent) as rnk from Agents ) as t --will return agents with distinct locations, because they have the rank equals to 1 where t.rnk = 1 group by t.Agent;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear