Meine Abfrage gibt eine Pivot-Tabelle zurück, die erste Spalte (Nummnth) enthält Textwerte wie 01,02,03 für die Monate Jan, Feb, Mar. Das Problem ist, dass die Bestellung 01, 03, 02 statt 01,02,03.SQL Server Pivot Bestellproblem
Wie könnte ich das reparieren ???
die Abfrage:
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
Ich versuchte die ORDER BY [nummnth] überall in der Abfrage außer am Ende, nach dem PV. Funktioniert perfekt jetzt. Ich fühle mich so dumm, aber es war eine "Programmierung zu extremer" Zeit. –
@AndyL. Ich bin froh, dass du dein Problem lösen kannst. Und egal, jeder fühlt sich von Zeit zu Zeit dumm :-D – Shnugo