I am trying to understand some anomalies in my SQL results when returning with FOR XML AUTO. Oddly enough, I have different results in XML. In short, the number of Institutions should always be 603, but the XML data gives me 3 different totals, as shown in this set of fragments:

The ORDER BY below simply goes through the selection to order the result. To debug this, I manually launched the stored procedure (commented out the FOR XML clause and created 3 "debug" tables filled with the passed SORT batch (each result set contains 603 rows if FOR XML AUTO is not used).

I hope the problem is now clearly shown. Here is the stored procedure (the bottom logic, where the @Sort value defines ORDER BY, is most suitable, I think):
ALTER Procedure [dbo].[FLAS2_List_Awards_V4]
(
@Sort int = 1
,@Range int = 0
)
As
CREATE TABLE
(
ID nchar(6) NOT NULL
, Institution nvarchar(255) NOT NULL
, Street nvarchar(255) NULL
, City nvarchar(255) NULL
, State nvarchar(255) NULL
, Zip nvarchar(255) NULL
, Latitude decimal (28, 18) NULL
, Longitude decimal (28, 18) NULL
, TotalAwards decimal (16, 0) NULL
, TotalAmount decimal (16, 0) NULL
)
INSERT INTO
(
ID
, Institution
, Street
, City
, State
, Zip
, Latitude
, Longitude
, TotalAwards
, TotalAmount
)
SELECT DISTINCT
C.ID
, C.InstitutionName
, NULL AS street
, NULL AS city
, NULL AS state
, NULL AS zip
, NULL As Latitude
, NULL As Longitude
, NULL As TotalAwards
, NULL As TotalAmount
FROM dbo.FLAS2_Schools2 C
UPDATE
SET Street = x.street
,City = x.city
,State = x.state
,Zip = x.zip
FROM dbo.FLAS2_Schools2 X
WHERE X.ID =
AND
X.InstitutionName =
UPDATE
SET Latitude = Z.lat
,Longitude = Z.Long
FROM dbo.ZipCodesPreferred Z
WHERE Z.ZipCode =
CREATE TABLE
(
ID nchar(6) NOT NULL
, TotalAwards decimal (16, 0) NULL
, TotalAmount decimal (16, 0) NULL
)
EXECUTE dbo.FLAS2_List_Awards_V3_PrepAwards @Range
CREATE TABLE
(
ID nchar(6) NOT NULL
, Institution nvarchar(255) NOT NULL
, Street nvarchar(255) NULL
, City nvarchar(255) NULL
, State nvarchar(255) NULL
, Zip nvarchar(255) NULL
, Latitude decimal (28, 18) NULL
, Longitude decimal (28, 18) NULL
, TotalAwards decimal (16, 0) NULL
, TotalAmount decimal (16, 0) NULL
)
INSERT INTO
(
ID
, Institution
, Street
, City
, State
, Zip
, Latitude
, Longitude
, TotalAwards
, TotalAmount
)
select
t1.ID
, Institution
, Street
, City
, State
, Zip
, Latitude
, Longitude
, t2.TotalAwards
, t2.TotalAmount
FROM
join
on t1.id = t2.id
IF @Sort = 1
BEGIN
SELECT Marker.ID
,Marker.Institution
,Marker.Street
,Marker.City
,Marker.State
,Marker.Zip
,Marker.Latitude
,Marker.Longitude
,Marker.TotalAmount
,Marker.TotalAwards
,Award.GrantNumber as GrantNumber
,Award.TotalObligatedAmount as GrantAmount
FROM
LEFT JOIN dbo.FLAS2_Grants Award
ON Marker.ID = Award.ID
order by Marker.TotalAmount DESC, Marker.Institution, GrantAmount DESC
for xml auto, root('root')
END
IF @Sort = 2
BEGIN
SELECT Marker.ID
,Marker.Institution
,Marker.Street
,Marker.City
,Marker.State
,Marker.Zip
,Marker.Latitude
,Marker.Longitude
,Marker.TotalAmount
,Marker.TotalAwards
,Award.GrantNumber as GrantNumber
,Award.TotalObligatedAmount as GrantAmount
FROM
LEFT JOIN dbo.FLAS2_Grants Award
ON Marker.ID = Award.ID
order by Marker.TotalAwards DESC, Marker.Institution ,GrantAmount DESC
for xml auto, root('root')
END
IF @Sort = 3
BEGIN
SELECT Marker.ID
,Marker.Institution
,Marker.Street
,Marker.City
,Marker.State
,Marker.Zip
,Marker.Latitude
,Marker.Longitude
,Marker.TotalAmount
,Marker.TotalAwards
,Award.GrantNumber as GrantNumber
,Award.TotalObligatedAmount as GrantAmount
FROM
LEFT JOIN dbo.FLAS2_Grants Award
ON Marker.ID = Award.ID
order by Marker.Institution ,Marker.Street, GrantAmount DESC
for xml auto, root('root')
END
By the way, each of the "DEBUG" tables had exactly 1,117 rows, regardless of how it was ordered at creation time, using the INTO clause.
Finally, here is a snippet of what the web application is going through (counting the “Tokens”):
