Combine column from multiple rows into single row. If youre allowed to use CLR in your environment, this is a tailor made case for a user defined aggregate. In particular, this is probably the way to go if the source data is non trivially large andor you need to do this type of thing a lot in your application. I strongly suspect the query plan for Aarons solution will not scale well as the input size grows. I tried adding an index to the temp table, but that didnt help. This solution, like many other things, is a tradeoff Politicspolicy for even using CLR Integration in your, or your clients, environment. CLR function is likely faster, and will scale better given a real set of data. CLR function will be reusable in other queries, and you wont have to duplicate and debug a complex subquery every time you need to do this type of thing. Straight T SQL is simpler than writing and managing a piece of external code. Perhaps you dont know how to program in C or VB. EDIT Well, I went to try to see if this actually was better, and it turns out the requirement that the comments be in a specific order is currently not possible to satisfy using an aggregate function. See Sql. User. Defined. Aggregate. Attribute. Is. Invariant. To. Order. Basically, what you need to do is OVERPARTITION BY customercode ORDER BY rownum but ORDER BY is not supported in the OVER clause when aggregating. Im assuming adding this functionality to SQL Server opens a can of worms, because what would need to be changed in the execution plan is trivial. The aforementioned link says this is reserved for future use, so this could be implemented in the future on 2. This could still be accomplished by packing and parsing the rownum value into the aggregated string, and then doing the sort within the CLR object. In any event, below is the code I used in case anyone else finds this useful even with the limitation. Ill leave the hacking part as an exercise for the reader. Note that I used Adventure. Works 2. 00. 5 for test data. Aggregate assembly using System. System. IO. using System. Data. Sql. Types. Microsoft. Sql. Server. Server. namespace My. Company. Sql. Server. Serializable. Sql. User. Defined. Aggregate. Format. User. Defined. Is. Null. If. Empty false. Is. Invariant. To. Duplicates false. Update Select Multiple Rows Sqlite' title='Update Select Multiple Rows Sqlite' />Is. Invariant. To. Nulls true. Is.
Invariant. To. Order false. Max. Byte. Size 1. String. Concat. Aggregate IBinary. Serialize. private string accum. Empty. public void Init. Empty. is. Empty true. AccumulateSql. String value. Is. Null. if is. Empty. Empty false. accum value. In this part of the SQLite tutorial, we will be inserting, updating, and deleting data. We use the INSERT INTO, DELETE, and UPDATE statements. Z.png' alt='Update Select Multiple Rows Sqlite' title='Update Select Multiple Rows Sqlite' />Ive got some customercomments split out into multiple rows due to database design, and for a report I need to combine the comments from each unique id into one row. I have a table like this one SELECT value FROM table value 1 3 13 1 5 I would like to add an accumulator column, so that I have this result value accumulated 1. SQL Tutorial SELECT Statement Basics The SQL SELECT statement queries data from tables in the database. The statement begins with the SELECT keyword. I have a SQLite database with table myTable and columns id, posX, posY. The number of rows changes constantly might increase or decrease. If I know the value of id. Value. public void MergeString. Concat. Aggregate value. Accumulatevalue. Terminate. Sql. String Terminate. Sql. Stringaccum. ReadBinary. Reader r. Init. accum r. Read. String. is. Empty accum. Length 0. WriteBinary. Writer w. Writeaccum. T SQL for testing CREATE ASSEMBLY, and spconfigure to enable CLR omitted CREATE TABLE dbo. Comments. Customer. Code int NOT NULL. Row. Num int NOT NULL. Comments nvarchar2. NOT NULL. INSERT INTO dbo. CommentsCustomer. Code, Row. Num, Comments. DENSERANK OVERORDER BY First. Name. ROWNUMBER OVERPARTITION BY First. Name ORDER BY Contact. ID. FROM Adventure. Charles Spurgeon Bible Commentary Downloads. Works. Person. Contact. CREATE AGGREGATE dbo. String. Concat. Aggregate. MAX. RETURNS nvarcharMAX. EXTERNAL NAME String. Concat. Aggregate. My. Company. Sql. Server. String. Concat. Aggregate. Customer. Code. dbo. String. Concat. AggregateComments AS All. Comments. FROM dbo. Comments. GROUP BY Customer.