-1

On Google I just found group by sum (integer value). How can I group by a string value? For the table:

No   Place    Car 
---  ------   --------
1    NY       VRM1
1    BT       VLI
2    NY       GAR
3    GT       GAR

How can I group by 'No'? I tried:

SELECT No, place, Car 
FROM  APPEL
GROUP BY No, Place, Car

But it didn't work.

I want to have a table like this

No   Place    Car 
---  ------   --------
1    NY,BT    VRM1,VLI
2    NY       GAR
3    GT       GAR
1

1 Answer 1

4
-- Sample data
with T (No,   Place,    Car) as
(
select 1,    'NY',       'VRM1' union all
select 1,    'BT',       'VLI' union all
select 2,    'NY',       'GAR' union all
select 3,    'GT',       'GAR'
)

-- The query
select
  T.No,
  stuff((select ','+T2.Place
         from T as T2 
         where T.No = T2.No
         for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as Place,
  stuff((select ','+T2.Car
         from T as T2 
         where T.No = T2.No
         for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as Car
from T
group by T.No
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.