简体   繁体   English

如何在SQL Server Management Studio(SSMS)中合并这些列

[英]How can I combine these columns in SQL Server Management Studio (SSMS)

I am using SMSS v17.3 我正在使用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) " 已添加-选择@@ version显示为“ Microsoft SQL Server 2014-12.0.2269.0(X64)2015年6月10日03:35:45版权所有(c)Windows NT 6.1(内部版本7601:Service Pack)上的Microsoft Corporation Enterprise Edition(64位) 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;

在此处输入图片说明

What I would like is 我想要的是

在此处输入图片说明

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. 前一段时间,我尝试摆弄CONCAT来做,我会在这里包括我尝试过的内容,但我找不到它,无论如何我当时从未完全对其进行过管理。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SQL Server Management Studio (SSMS) 中评论 SQL 查询 - How to comment SQL queries in SQL Server Management Studio (SSMS) 在SSMS(SQL Server Management Studio)中,当列为只读时如何添加行? - In SSMS (Sql Server management Studio) how do I add a row to when a column is read only? SQL Server Management Studio SSMS将所有列从float转换为nvarchar - SQL Server Management Studio SSMS Convert all columns from float to nvarchar 在 SQL Server Management Studio(SSMS) 中评论和取消评论 SQL 查询的快捷方式 - Shortcuts to comment and uncomment SQL queries in SQL Server Management Studio(SSMS) 如何将行合二为一,MS SQL 服务器管理工作室 - How to combine rows into one, MS SQL Server management Studio 有没有办法在 SQL Server Management Studio (SSMS) 中分隔查询结果? - Is there a way to separate query results in SQL Server Management Studio (SSMS)? 从没有SQL Server Management Studio(SSMS)的MDF创建BAK - Create BAK from MDF without SQL Server Management Studio (SSMS) 禁用 SQL Server Management Studio (SSMS) 中的空白文本副本 - Disabling copy of blank text in SQL Server Management Studio (SSMS) 如何在SSMS(SQL Server Management Studio)中自动设置BEGIN_TRANSACTION? - How to set BEGIN_TRANSACTION automatically in SSMS (SQL Server Management Studio)? 如何使用SQL Server 2012 Management Studio添加/删除列 - How do I add/remove columns using SQL Server 2012 Management Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM