sql-server-group-concat

Concatenate multiple rows

落爺英雄遲暮 提交于 2019-11-27 08:42:17
问题 I'm using Microsoft SQL Server 2005. I think I need a sub-query. I'm looking for one (1) row per customer, with the AvailableAction field be a concatenation of all the Actions for each customer. use tempdb GO IF DB_ID('myDatabase') IS NOT NULL DROP DATABASE myDatabase go CREATE DATABASE myDatabase GO USE myDatabase GO create table Cust( CustID Int Identity Primary Key, CustName Varchar(255) ) GO INSERT INTO Cust(CustName) values('One') INSERT INTO Cust(CustName) values('Two') GO CREATE TABLE

SQL, How to Concatenate results?

北战南征 提交于 2019-11-27 01:48:57
I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that. The Problem in detail: If I have a table X with two columns, ModuleID and say ModuleValue, how can I write a SQL query to take the results and Concatenate it into one field: EG Results returned from (SELECT ModuleValue FROM Table_X WHERE ModuleID=@ModuleID) Value 1 Value 2 Value 3 ... I need to return the result thus (as a single row, unlike the above): Value 1, Value 2, Value 3 Is there a simple Concatenation method that could be user? EDIT: DB is MS TSQL (2005) With

How to concatenate all strings from a certain column for each group

我是研究僧i 提交于 2019-11-26 23:01:32
问题 Suppose I have this table [Table1] Name Mark ------- ------ ABC 10 DEF 10 GHI 10 JKL 20 MNO 20 PQR 30 What should be my SQL statement to retrieve a record that looks like this: (group by [mark]). I have done the 1 and 2 columns but don't know how to accomplish the third column (concat the [name] with the same [mark]) mark count names ---- ----- ----------- 10 3 ABC,DEF,GHI 20 2 JKL,MNO 30 1 PQR I'm using Microsoft SQL. Please help. Thanks 回答1: If MS SQL 2005 or higher. declare @t table([name]

SQL comma-separated row with Group By clause

与世无争的帅哥 提交于 2019-11-26 21:02:46
I have the following query: SELECT Account, Unit, SUM(state_fee), Code FROM tblMta WHERE MTA.Id = '123' GROUP BY Account,Unit This of course throws an exception because the Code is not in the group by clause. Each state_fee has a code. How do I get this code to display in 1 record (1 code per state_fee which is multiple state_fee per unit) as a comma-separated list? I looked into different solutions on here but I couldn't find any that worked with a group by . Adrian Carneiro You want to use FOR XML PATH construct: SELECT ACCOUNT, unit, SUM(state_fee), Stuff((SELECT ', ' + code FROM tblmta t2

SQL, How to Concatenate results?

▼魔方 西西 提交于 2019-11-26 12:29:10
问题 I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that. The Problem in detail: If I have a table X with two columns, ModuleID and say ModuleValue, how can I write a SQL query to take the results and Concatenate it into one field: EG Results returned from (SELECT ModuleValue FROM Table_X WHERE ModuleID=@ModuleID) Value 1 Value 2 Value 3 ... I need to return the result thus (as a single row, unlike the above): Value 1, Value

SQL comma-separated row with Group By clause

自作多情 提交于 2019-11-26 07:48:53
问题 I have the following query: SELECT Account, Unit, SUM(state_fee), Code FROM tblMta WHERE MTA.Id = \'123\' GROUP BY Account,Unit This of course throws an exception because the Code is not in the group by clause. Each state_fee has a code. How do I get this code to display in 1 record (1 code per state_fee which is multiple state_fee per unit) as a comma-separated list? I looked into different solutions on here but I couldn\'t find any that worked with a group by . 回答1: You want to use FOR XML

SQL Query to get aggregated result in comma separators along with group by column in SQL Server

丶灬走出姿态 提交于 2019-11-26 02:13:49
问题 I need to write a sql query on the table such that the result would have the group by column along with the aggregated column with comma separators. My table would be in the below format |`````````|````````| | ID | Value | |_________|________| | 1 | a | |_________|________| | 1 | b | |_________|________| | 2 | c | |_________|________| Expected result should be in the below format |`````````|````````| | ID | Value | |_________|________| | 1 | a,b | |_________|________| | 2 | c | |_________|___

How to make a query with group_concat in sql server [duplicate]

為{幸葍}努か 提交于 2019-11-25 21:42:02
问题 This question already has answers here : Simulating group_concat MySQL function in Microsoft SQL Server 2005? (10 answers) Closed 3 years ago . I know that in sql server we cannot use Group_concat function but here is one issue i have in which i need to Group_Concat my query.I google it found some logic but not able to correct it.My sql query is select m.maskid,m.maskname,m.schoolid,s.schoolname, md.maskdetail from tblmask m join school s on s.id = m.schoolid join maskdetails md on m.maskid =

How to use GROUP BY to concatenate strings in SQL Server?

你。 提交于 2019-11-25 21:38:19
问题 How do I get: id Name Value 1 A 4 1 B 8 2 C 9 to id Column 1 A:4, B:8 2 C:9 回答1: No CURSOR, WHILE loop, or User-Defined Function needed . Just need to be creative with FOR XML and PATH. [Note: This solution only works on SQL 2005 and later. Original question didn't specify the version in use.] CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1), [Value] INT) INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'A',4) INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'B',8) INSERT INTO