sql-server-group-concat

How to execute a query which is stored in a table column MySQL?

让人想犯罪 __ 提交于 2019-12-03 16:31:22
mysql> select * from CT; | CID | MID | REPORT_QUERY | | 1 | 1 | select * from emp; | | 2 | 2 | select * from student; | 2 rows in set (0.00 sec) I want to execute queries in REPORT_QUERY column. DELIMITER // CREATE PROCEDURE TRYct() BEGIN SET @str=(SELECT GROUP_CONCAT(REPORT_QUERY SEPARATOR ' ') FROM CT); PREPARE q from @str; EXECUTE q; END // DELIMITER ; i use this code but it works if there is only one query in my table. if there is two query than it gives an error. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for

Group and concatenate many rows to one

北战南征 提交于 2019-12-02 06:25:48
I want to "concatenate" all the "Text"-rows into one single row and get one row as a result. Is this even possible? I use MSSQL Server 2005. Use FOR XML PATH: SELECT [Text]+' ' AS 'text()' FROM _table FOR XML PATH('') Another option - use string concatenation: DECLARE @s nvarchar(max) SELECT @s = ISNULL(@s, '') + t + ' ' FROM _table OPTION (MAXDOP 1) SELECT @s Please note that the latter one isn't guaranteed to work, afaik, officially the behaviour of "@s = @s + ..." for multi-row resultset is undefined. MAXDOP 1 hint is used here to prevent the optimizer from creating a parralel execution

SQL Server Group Concat with Different characters

拥有回忆 提交于 2019-11-30 22:13:22
I have looked through a number of solutions to emulating "Group concat" functionality in SQL Server. I wanted to make a more human readable solution though and I can't work out how to do it. I have a view: ParentID | ChildName Which contains the records, for example: 1 | Max 1 | Jessie 2 | Steven 2 | Lucy 2 | Jake 3 | Mark I want to "Group Concat" these to get: 1 | Max and Jessie 2 | Steven, Lucy and Jake 3 | Mark So If there is only 1 child, just return name, if there are more than one, concat the last 2 with an ' and ' and all others with a ', '. I am a bit stuck on how to do this without

string concatenate in group by function with other aggregate functions

我们两清 提交于 2019-11-30 20:46:56
Is it possible to concatenate strings with one or more of other group by function like sum, avg, count etc . Say I have the following table Id Name Order Value 1 a 1 100 2 b 2 200 3 c 1 300 4 d 1 100 5 e 2 300 Now if I want the result to be something of this sort Order Name Value Count 1 a,c,d 500 3 2 b,e 500 2 How can i achieve the same using a query on SQL server. Sample table create table t123 (Id int, Name varchar(10), [Order] int, Value int) insert t123 select 1,'a','1',100 union all select 2,'b','2',200 union all select 3,'c','1',300 union all select 4,'d','1',100 union all select 5,'e',

SQL Server Group Concat with Different characters

这一生的挚爱 提交于 2019-11-30 17:45:58
问题 I have looked through a number of solutions to emulating "Group concat" functionality in SQL Server. I wanted to make a more human readable solution though and I can't work out how to do it. I have a view: ParentID | ChildName Which contains the records, for example: 1 | Max 1 | Jessie 2 | Steven 2 | Lucy 2 | Jake 3 | Mark I want to "Group Concat" these to get: 1 | Max and Jessie 2 | Steven, Lucy and Jake 3 | Mark So If there is only 1 child, just return name, if there are more than one,

string concatenate in group by function with other aggregate functions

杀马特。学长 韩版系。学妹 提交于 2019-11-30 04:15:16
问题 Is it possible to concatenate strings with one or more of other group by function like sum, avg, count etc . Say I have the following table Id Name Order Value 1 a 1 100 2 b 2 200 3 c 1 300 4 d 1 100 5 e 2 300 Now if I want the result to be something of this sort Order Name Value Count 1 a,c,d 500 3 2 b,e 500 2 How can i achieve the same using a query on SQL server. 回答1: Sample table create table t123 (Id int, Name varchar(10), [Order] int, Value int) insert t123 select 1,'a','1',100 union

How to concatenate multiple rows?

让人想犯罪 __ 提交于 2019-11-29 08:55:30
I have the following query which returns the salary of all employees. This work perfectly but I need to collect extra data that I will aggregate into one cell (see Result Set 2). How can I aggregate data into a comma separated list? A little bit like what Sum does, but I need a string in return. SELECT Employee.Id, SUM(Pay) as Salary FROM Employee INNER JOIN PayCheck ON PayCheck.EmployeeId = Employee.Id GROUP BY Employee.Id Result Set 1 Employee.Id Salary ----------------------------------- 1 150 2 250 3 350 I need: Result Set 2 Employee.Id Salary Data -----------------------------------------

Concatenate multiple rows

冷暖自知 提交于 2019-11-28 14:24:24
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 Action( ActionID Int Identity(101,1) Primary Key, ActionName Varchar(128) ) GO INSERT INTO Action

How to concatenate multiple rows?

霸气de小男生 提交于 2019-11-28 02:09:25
问题 I have the following query which returns the salary of all employees. This work perfectly but I need to collect extra data that I will aggregate into one cell (see Result Set 2). How can I aggregate data into a comma separated list? A little bit like what Sum does, but I need a string in return. SELECT Employee.Id, SUM(Pay) as Salary FROM Employee INNER JOIN PayCheck ON PayCheck.EmployeeId = Employee.Id GROUP BY Employee.Id Result Set 1 Employee.Id Salary ----------------------------------- 1

Concatenate one field after GROUP BY

别来无恙 提交于 2019-11-27 23:22:50
This question have been asked many times in SO but none of the answers is satisfying to my situation. Question 1 Question 2 Question 3 Question 4 I am dealing with a DataObjectVersions table that contains multiple versions for around 1.2 million unique objects (and increasing). I need to concatenate changes from a specific field for each unique object. Right now I am using the solution with the XML Path presented in Q3 but running such a query on this table is a total performance disaster. SQL Server started to retun Data after 19mn. Knowing that this data will be than joined twice, you can