我的查询返回一个数据透视表,第一列(nummnth)包含1月,2月,3月等文本值,如01,02,03。问题是排序是01,03,02而不是01, 02,03。
我怎么能解决这个问题?
查询是:
select [nummnth] ,[mnth],[Business Fixed Score],[Business Fixed
Sessions],[Business Mobile Score],[Business Mobile Sessions],[Business
Merged Score],[Business Merged Sessions] from (Select [nummnth],
[Mnth],C.* from (
SELECT [Service],nummnth,mnth,b.A2 as [User_Score],b.A2_Sessions as
[Sessions_Count] FROM [QTDB].[dbo].[QTD_BOX_BUS_MERGED_CP] as [b] where
YR=2017
and [service] = 'Business Fixed' and Agent='ANAME'
Union
SELECT [Service],nummnth,mnth,b.A2 as [User_Score],b.A2_Sessions as
[Sessions_Count]
FROM [QTDB].[dbo].[QTD_BOX_BUS_MERGED_CP] as [b] where YR=2017 and
[service] = 'Business Mobile' and Agent='ANAME'
UNION all
SELECT 'Business Merged' as [Service] ,nummnth,mnth,b.A2 as
[User_Score],b.A2_Sessions as [Sessions_Count]
FROM [QTDB].[dbo].[QTD_BOX_BUS_AGENT_MNTH_MERGED] as [b] where YR=2017
and Agent='ANAME') A
Cross Apply (Values (A.[Service]+' Score',
cast(A.[User_Score] as float)),(A.[Service]+' Sessions',cast(A.
[Sessions_Count] as float))) C (Item,Value)) R Pivot (
sum(Value) For [Item] in ([Business Fixed Score],
[Business Fixed Sessions],
[Business Mobile Score],[Business Mobile Sessions],
[Business Merged Score],[Business Merged Sessions])) PV
答案 0 :(得分:1)
在SQL-Server 中没有隐含的顺序!! 无,从不...
你甚至不能说问题在于排序是01,03,02而不是01,02,03。下一次调用它可能以不同的方式返回。
确保订单的唯一方法是在最外层的查询上ORDER BY
!
只需添加ORDER BY [nummnth]
作为最后一行并检查结果。