我需要根据月份或顺序而不是按字母顺序对行进行排序。
当我运行下面的查询时,我得到以下结果
Col Mon Tue Wed Thu Fri
--- --- --- --- --- ---
Feb 1 2 3 4 5
Jan 2 3 4 5 6
Mar 3 4 5 6 7
我怎样才能按如下方式订购:
Col Mon Tue Wed Thu Fri
--- --- --- --- --- ---
Jan 2 3 4 5 6
Feb 1 2 3 4 5
Mar 3 4 5 6 7
我的查询:
select *
from
(
select LEFT(datename(dw,datetime),3) as DateWeek,
col,
value
from DataTable
cross apply
(
values ('Jan', JData), ('Feb', FData), ('Mar', MData)
) c (col, value)
) src
pivot
(
sum(value)
for dateweek in (Mon, Tue, Wed, Thu, Fri)
) piv
目前我将值的行替换为:
values ('1.Jan', JData), ('2.Feb', FData), ('3.Mar', MData)
我想有更好的解决方案