SQL Server dynamic PIVOT query?

Dynamic SQL PIVOT: create table temp ( date datetime, category varchar(3), amount money ) insert into temp values (‘1/1/2012’, ‘ABC’, 1000.00) insert into temp values (‘2/1/2012’, ‘DEF’, 500.00) insert into temp values (‘2/1/2012’, ‘GHI’, 800.00) insert into temp values (‘2/10/2012’, ‘DEF’, 700.00) insert into temp values (‘3/1/2012’, ‘ABC’, 1100.00) DECLARE @cols AS NVARCHAR(MAX), @query AS … Read more

Simulating group_concat MySQL function in Microsoft SQL Server 2005?

SQL Server 2017 does introduce a new aggregate function STRING_AGG ( expression, separator). Concatenates the values of string expressions and places separator values between them. The separator is not added at the end of string. The concatenated elements can be ordered by appending WITHIN GROUP (ORDER BY some_expression) For versions 2005-2016 I typically use the … Read more

Row Offset in SQL Server

I would avoid using SELECT *. Specify columns you actually want even though it may be all of them. SQL Server 2005+ SELECT col1, col2 FROM ( SELECT col1, col2, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM MyTable ) AS MyDerivedTable WHERE MyDerivedTable.RowNum BETWEEN @startRow AND @endRow SQL Server 2000 Efficiently Paging Through Large … Read more

tech