i need to get csv from a column but the minimum value from the rest of the columns (or any value because they are same for a group). As an example;
I have the following
You can do it with a sub query, like this:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
COL1 char(3),
COL2 int,
COL3 int,
COL4 char(6)
)
INSERT INTO @T VALUES
('ABC', 10, 35, 'GROUP1'),
('AQW', 10, 35, 'GROUP1'),
('VBN', 10, 35, 'GROUP1'),
('HJK', 10, 35, 'GROUP1'),
('DFV', 30, 25, 'GROUP2'),
('HYT', 30, 25, 'GROUP2'),
('DET', 30, 25, 'GROUP2')
The query:
SELECT DISTINCT
STUFF(
(
SELECT ',' + COL1
FROM @T
WHERE COL2 = t.COL2
AND COL3 = t.COL3
AND COL4 = t.COL4
FOR XML PATH('')
)
, 1, 1, '') As COL1,
COL2,
COL3,
COL4
FROM @T t
Results:
COL1 COL2 COL3 COL4
ABC,AQW,VBN,HJK 10 35 GROUP1
DFV,HYT,DET 30 25 GROUP2