0
votes

I am using SMSS v17.3

added- select @@version shows "Microsoft SQL Server 2014 - 12.0.2269.0 (X64) Jun 10 2015 03:35:45 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) "

I have

-- Book = book name.
-- Ch=chapter V=verse

CREATE TABLE dbo.flow1(Id int, A varchar(10), B varchar(10), Book varchar(10), Ch INT, V INT)

INSERT INTO dbo.flow1 VALUES(1, 'qwe', 'asd', 'gggg', 3, 44)
INSERT INTO dbo.flow1 VALUES(2, 'qwe', 'asd', 'asdf', 43, 11)
INSERT INTO dbo.flow1 VALUES(3, 'erw', 'asd', 'fsef', 12, 53)
INSERT INTO dbo.flow1 VALUES(4, 'erw', 'zre', 'vxde', 53, 12)
INSERT INTO dbo.flow1 VALUES(5, 'vwe', 'hwa', 'vwhe', 11, 51)
INSERT INTO dbo.flow1 VALUES(6, 'rwq', 'yha', 'bgda', 63, 12)
INSERT INTO dbo.flow1 VALUES(7, 'qwe', 'asd', 'faaa', 32, 61)
INSERT INTO dbo.flow1 VALUES(8, 'rwq', 'yha', 'fsag', 35, 3)

SELECT A,B,COUNT(Id) as cnt  FROM dbo.flow1 GROUP BY A,B;

enter image description here

What I would like is

enter image description here

A while back I tried fiddling around with CONCAT to do it, I would include what I tried here but I can't find it, anyhow I never quite managed it at the time.

1
What version of SQL Server?RBarryYoung
@RBarryYoung i'm actually using SQL Studio but I saw no tag for it and I was not aware of any difference.barlop
@Xedni Microsoft SQL Server 2014 - 12.0.2269.0 (X64) Jun 10 2015 03:35:45 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)barlop

1 Answers

2
votes
select 
    A,
    B,
    Cnt = count(Id),
    Concatted =
    stuff
        (
            (
                select concat(',', Book, ' ', Ch, ':', V)
                from Flow1 i
                where o.A = i.A
                    and o.B= i.B
                for xml path(''), type
            ).value('.', 'nvarchar(max)'),
            1, 1, ''
        )

from flow1 o
group by A,B