I am rewriting several pages of a badly written app, I am trying to incorporate this logic in the main query as a subquery so I can get all the data in 1 swoop instead of connection 50K times like the current code.
I started off with
DECLARE @combinedString NVARCHAR(MAX),@mds NVARCHAR(MAX)
SELECT
@combinedString = COALESCE(@combinedString + ', ', '') + serial,
@mds = MDS
FROM
(aircraftserials
LEFT JOIN
serialnums ON serialnums.ID = aircraftserials.Serialnum_ID)
WHERE
(85 = aircraftserials.Aircraft_ID AND serial IS NOT NULL)
I want to eliminate these variables and make this a subquery,
I think I need a CTE; end result needs to be
MDS, concatenated serials
I have tried this post but cannot quite get it Simplify CTE string concatenation?
Here is the sample of the data
CREATE TABLE TestBed
(
Aircraft_ID INT,
Serial VARCHAR(50),
MDS VARCHAR(50)
)
INSERT INTO TestBed (Aircraft_ID, Serial, MDS)
VALUES (85, '56-1965', 'T-37B'),
(85, '56-1967', 'T-37B'),
(85, '56-3547', 'T-37B'),
(85, '56-3577', 'T-37B'),
(85, '57-2265', 'T-37B'),
(85, '57-2272', 'T-37B'),
(85, '58-1915', 'T-37B'),
(85, '58-1925', 'T-37B'),
(85, '59-0249', 'T-37B'),
(85, '59-0273', 'T-37B'),
(85, '59-0299', 'T-37B')
select * from TestBed