Ich verwende einen Drehpunkt, um Altersbereiche aus einer Altersspalte zu gruppieren, und alle Spalten werden nicht so ausgefüllt, wie sie sollten. Ich nehme die Summe von LOS (Länge des Aufenthalts) und die Altersbereichssummen sollten dem TotalLOS entsprechen, aber momentan ist es nicht. Bitte helfen Sie !!Verwenden von Pivot für Altersbereiche Problem
Dies ist die T-SQL-Abfrage:
SELECT *
FROM
(
SELECT [Hosp Name],
Cnty,
Max([Hosp County]) as [FirstOfHosp_County],
Max(Hosp) as UID,
LOS,
Age,
Sum(LOS) as TotalLOS,
CASE
WHEN Age <=17 THEN '0-17'
WHEN Age Between 18 and 64 THEN '18-64'
WHEN Age >=65 THEN '65 Up'
ELSE NULL
END AS AGERANGE
FROM [S1: Basic Source Query]
GROUP BY [Hosp Name], Cnty, Age, [Hosp County], Hosp, LOS
) AS S
PIVOT
(
Sum(S.LOS)
for S.AGERANGE
in ([0-17], [18-64],[65 Up])
) as pvt
ich diese Abfrage aus Access zu konvertieren. Dies ist die Zugriffsabfrage:
TRANSFORM Sum([S1: Basic Source Query].LOS) AS Days
SELECT [S1: Basic Source Query].[Hosp Name],
[S1: Basic Source Query].Cnty,
FirstOfHosp County: First([S1: Basic Source Query].[Hosp County])
UID: First([S1: Basic Source Query].Hosp)
Total: Sum([S1: Basic Source Query].LOS)
FROM [S1: Basic Source Query]
GROUP BY [S1: Basic Source Query].[Hosp Name],
[S1: Basic Source Query].Cnty
PIVOT Switch([Age] Between 0 And 17,"0-17",[Age] Between 18 And 64,"18-64",[Age]>=65,"65 Up");
Dies ist ein Beispiel für die Access-Daten vor dem Drehpunkt. Es hat nicht alle Spalten.
Hosp Hosp Name LOS Age
HOSP301 Apple Hospital 16 92
HOSP301 Apple Hospital 5 34
HOSP301 Apple Hospital 14 85
HOSP301 Apple Hospital 7 21
HOSP301 Apple Hospital 4 79
HOSP301 Apple Hospital 2 26
HOSP301 Apple Hospital 2 19
Und wenn die Access-Abfrage läuft, es verwandelt es sich in diese:
UID Hosp Name TotalLOS 0-17 18-64 65 Up
HOSP301 Apple Hospital 130 117 13
HOSP301 Apple Hospital 5 5
HOSP301 Apple Hospital 722 60 455 207
HOSP301 Apple Hospital 23 17 6
HOSP301 Apple Hospital 15 15
HOSP301 Apple Hospital 6 6
HOSP301 Apple Hospital 3 3
HOSP301 Apple Hospital 32 1 31
Dies ist die falsche SQL-Ergebnisse, die ich immer bin. Die Zahlen unter den Altersklassen sollten die TotalLOS Spalte entsprechen:
UID Hosp Name Cnty FirstOfHosp_County Age TotalLOS 0-17 18-64 65 Up
HOSP301 Apple Hospital Apple Apple 57 24 4
HOSP301 Apple Hospital Apple Apple 68 13 13
HOSP301 Apple Hospital Apple Apple 69 12 4
HOSP301 Apple Hospital Apple Apple 71 10 10
HOSP301 Apple Hospital Apple Apple 73 8 2
HOSP301 Apple Hospital Apple Apple 74 7 7
HOSP301 Apple Hospital Apple Apple 75 6 6
HOSP301 Apple Hospital Apple Apple 79 34 17
HOSP301 Apple Hospital Bacon Apple 63 2 2
HOSP301 Apple Hospital Bagwin Apple 68 16 16
So wie Sie die Zahlen in den Bereichen sehen nicht immer überein, was in der TotalLOS Spalte ist. Ich glaube, ich habe es nicht korrekt von Access nach T-SQL konvertiert.
Es gibt keinen Grund, diese Felder in die Gruppe von '[Hosp County], Hosp, LOS' zu integrieren, während [aggregiert] (https://msdn.microsoft.com/en-GB/library/ms173454.aspx) sie in der Auswahlklausel. Können Sie Beispieldaten bereitstellen, die ein falsches Ergebnis zurückgeben? Ohne es ist schwer an Ihrer Anfrage zu arbeiten. –