CLR custom aggregates for SQL Server
Having the tables "product" and "product_stock" I want to display for each product the comma-separated list of sizes and this list is ordered by a specific order. CREATE TABLE product( product_id int, code varchar(50) ) CREATE TABLE product_stock( product_stock_id int, size_order int , size varchar(20) , product_id int ) -- desired output Product_id Sizes In Stock 35 UK7, UK8, UK9, UK11, UK10.5, UK12, UK10 36 L, M, S, XL, XXL, XXXL In SQL this can be achieved with cursors, but everyone suggest not to use them. After some digging I found out about custom aggregates and they saved the day. In the MSDN article is an example which concatenates the strings and this I'm writing does the same, only it adds the ordering feature. The problem occurs with the ordering because SQL doesn't allow to use the "ORDER BY" clause in subqueries or if the "GROUP BY" exists, then the only columns allowed are the ones in gr...