smeimax ...
smeimax ...

Reputation: 23

SQL Server Pivot In clause

I'm trying this, but it returns results in an unexpected" order... should be

2013-01, 2013-02, 2013-03, etc view_YearWeeks is a view that select DISTINCT YearWeek ordered ASC from a sales table... if executed it ordered values correctly, but within this

Select @colWeeks = isnull(@colWeeks + ', ','') + QUOTENAME(YearWeek) 
from 
    (select YearWeek 
     from dbo.YearWeeks) as weeks

it returns

[2014-33], [2014-48], [2013-13], etc

How do I solve it?

Upvotes: 0

Views: 26

Answers (1)

Mike
Mike

Reputation: 3811

Use an Order By to order records in SQL Server.

Declare @colWeeks nvarchar(1000) = N'';

Select @colWeeks = isnull(@colWeeks + ', ','') + QUOTENAME(YearWeek.YearWeek) 
From YearWeek
Order By YearWeek.YearWeek;

Upvotes: 1

Related Questions