FROM 子句中的子查询导致 FOR XML AUTO 输出中不需要的 ELEMENT
Subquery in FROM clause is causing unwanted ELEMENT in FOR XML AUTO output
我正在使用 Northwind 为 SQL 建模,我相信我需要使用我的实际数据来构建 Google 地图应用程序。我认为我的查询工作正常,但在 XML 中返回时,层次结构不是我需要的方式。
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,o.OrderID
,o.Freight
FROM
(
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
) AS Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')
这是返回的 XML:
<root>
<c CustomerID="ALFKI" CompanyName="Alfreds Futterkiste">
<Marker TotalAmount="225.5800">
<o OrderID="10643" Freight="29.4600" />
<o OrderID="10692" Freight="61.0200" />
<o OrderID="10702" Freight="23.9400" />
<o OrderID="10835" Freight="69.5300" />
<o OrderID="10952" Freight="40.4200" />
<o OrderID="11011" Freight="1.2100" />
</Marker>
</c>
<c CustomerID="ANTON" CompanyName="Antonio Moreno Taquería">
<Marker TotalAmount="268.5200">
<o OrderID="10365" Freight="22.0000" />
<o OrderID="10507" Freight="47.4500" />
<o OrderID="10535" Freight="15.6400" />
<o OrderID="10573" Freight="84.8400" />
<o OrderID="10677" Freight="4.0300" />
<o OrderID="10682" Freight="36.1300" />
<o OrderID="10856" Freight="58.4300" />
</Marker>
</c>
</root>
使用 c 作为 customers table 的别名的子查询在 XML 中是不需要的;然而,我似乎需要它,因为 CustomerID 在订单和客户中都是模棱两可的。我想知道如何在 Marker 中仅显示 o,如下图所示:
<root>
<Marker CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" TotalAmount="225.5800">
<o OrderID="10643" Freight="29.4600" />
<o OrderID="10692" Freight="61.0200" />
<o OrderID="10702" Freight="23.9400" />
<o OrderID="10835" Freight="69.5300" />
<o OrderID="10952" Freight="40.4200" />
<o OrderID="11011" Freight="1.2100" />
</Marker>
用cte怎么样
WITH Marker AS (
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
)
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,o.OrderID
,o.Freight
FROM Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')
通常,我一直认为在 OP 中使用子查询有点 "cooler" 但正如所评论的那样,@DVT 的 CTE 想法产生了相同的 XML 所以我重写如下.通过避免子查询并填充临时 table,我能够根据需要获取 XML 层次结构中的元素:
CREATE TABLE #TempMarkers
(
CustomerID nchar(5) NOT NULL,
CompanyName nvarchar(40) NOT NULL,
Address nvarchar(60) NULL,
TotalOrders int NULL,
TotalAmount money NULL
)
INSERT INTO #TempMarkers
(CustomerID
,CompanyName
,Address
,TotalOrders
,TotalAmount
)
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,Marker.TotalOrders
,o.OrderID
,o.Freight
FROM #TempMarkers Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')
我正在使用 Northwind 为 SQL 建模,我相信我需要使用我的实际数据来构建 Google 地图应用程序。我认为我的查询工作正常,但在 XML 中返回时,层次结构不是我需要的方式。
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,o.OrderID
,o.Freight
FROM
(
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
) AS Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')
这是返回的 XML:
<root>
<c CustomerID="ALFKI" CompanyName="Alfreds Futterkiste">
<Marker TotalAmount="225.5800">
<o OrderID="10643" Freight="29.4600" />
<o OrderID="10692" Freight="61.0200" />
<o OrderID="10702" Freight="23.9400" />
<o OrderID="10835" Freight="69.5300" />
<o OrderID="10952" Freight="40.4200" />
<o OrderID="11011" Freight="1.2100" />
</Marker>
</c>
<c CustomerID="ANTON" CompanyName="Antonio Moreno Taquería">
<Marker TotalAmount="268.5200">
<o OrderID="10365" Freight="22.0000" />
<o OrderID="10507" Freight="47.4500" />
<o OrderID="10535" Freight="15.6400" />
<o OrderID="10573" Freight="84.8400" />
<o OrderID="10677" Freight="4.0300" />
<o OrderID="10682" Freight="36.1300" />
<o OrderID="10856" Freight="58.4300" />
</Marker>
</c>
</root>
使用 c 作为 customers table 的别名的子查询在 XML 中是不需要的;然而,我似乎需要它,因为 CustomerID 在订单和客户中都是模棱两可的。我想知道如何在 Marker 中仅显示 o,如下图所示:
<root>
<Marker CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" TotalAmount="225.5800">
<o OrderID="10643" Freight="29.4600" />
<o OrderID="10692" Freight="61.0200" />
<o OrderID="10702" Freight="23.9400" />
<o OrderID="10835" Freight="69.5300" />
<o OrderID="10952" Freight="40.4200" />
<o OrderID="11011" Freight="1.2100" />
</Marker>
用cte怎么样
WITH Marker AS (
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
)
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,o.OrderID
,o.Freight
FROM Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')
通常,我一直认为在 OP 中使用子查询有点 "cooler" 但正如所评论的那样,@DVT 的 CTE 想法产生了相同的 XML 所以我重写如下.通过避免子查询并填充临时 table,我能够根据需要获取 XML 层次结构中的元素:
CREATE TABLE #TempMarkers
(
CustomerID nchar(5) NOT NULL,
CompanyName nvarchar(40) NOT NULL,
Address nvarchar(60) NULL,
TotalOrders int NULL,
TotalAmount money NULL
)
INSERT INTO #TempMarkers
(CustomerID
,CompanyName
,Address
,TotalOrders
,TotalAmount
)
SELECT c.CustomerID
,c.CompanyName
,c.Address
,COUNT(o.freight) as TotalOrders
,SUM(o.freight) as TotalAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
where c.CustomerID = 'ALFKI' or c.CustomerID = 'ANTON'
group by c.CustomerID
,c.CompanyName
,c.Address
SELECT Marker.CustomerID
,Marker.CompanyName
,Marker.TotalAmount
,Marker.TotalOrders
,o.OrderID
,o.Freight
FROM #TempMarkers Marker
INNER JOIN Orders o
ON Marker.CustomerID = o.CustomerID
where Marker.CustomerID = 'ALFKI' or Marker.CustomerID = 'ANTON'
order by 1
for xml auto, root('root')