Data
DROP TABLE IF EXISTS cats;
CREATE TABLE cats
(
litterID int NOT NULL,
catID int NOT null, --row uniquifier
catFirst varchar(30),
catLast varchar(30),
catSize int
);
INSERT INTO cats (litterID,catID,catFirst,catLast,catSize)
VALUES
('20020314','20262607','Scat','Meow','8'),
('20020314','20264036','Paws','Furbs','4'),
('20020314','20264267','Alexander','Meow','2');
Problem
For each catID create a string of their littermates (this is the hard part ...removing them from the set of members)
Expected Output
catID catFirst litterID littermates
-------- --------- -------- -----------------------------------------------
20262607 Scat 20020314 "Paws Furbs Size 4; Alexander Meow Size 2"
20264036 Paws 20020314 "Scat Meow Size 8; Alexander Meow Size 2"
20264267 Alexander 20020314 "Scat Meow Size 8; Paws Furbs Size 4"
I've tried EXCEPT query, CURSOR, WHILE. I think the solution lay in CROSS APPLY.
You want to list all other cats that belong to the same litter on each row.
One option uses a correlated subquery, or
apply:For each row in the outer query, what
applydoes is re-open the same table, then look for rows that have the samelitterIDand a differentcatID- thenstring_agg()aggregates the names and size into a scalar value.fiddle