如何统计T-SQL中的xml属性值?
How to tally up xml attribute values in T-SQL?
我在 SQL 服务器 table 的数据列中有以下 XML 数据。我想总结所有 RecordCount
属性的总值,因为我 select 每个 table 行。我该怎么做?
<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
</BatchSummary>
请尝试以下解决方案。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
</BatchSummary>'),
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="10" />
</BatchSummary>');
-- DDL and sample data population, end
SELECT t.*
, c.value('sum(InterfaceTable/@RecordCount)', 'DECIMAL(12,2)') AS Result
FROM @tbl AS t
CROSS APPLY xmldata.nodes('/BatchSummary') AS t1(c);
-- to check data type of the RecordCount attribute
SELECT ID
, TRY_CAST(c.value('.', 'VARCHAR(30)') AS INT) AS Result
FROM @tbl AS t
CROSS APPLY xmldata.nodes('/BatchSummary/InterfaceTable/@RecordCount') AS t1(c);
输出
+----+--------+
| ID | Result |
+----+--------+
| 1 | 5022 |
| 2 | 14 |
+----+--------+
我在 SQL 服务器 table 的数据列中有以下 XML 数据。我想总结所有 RecordCount
属性的总值,因为我 select 每个 table 行。我该怎么做?
<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
</BatchSummary>
请尝试以下解决方案。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1438" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3462" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="122" />
</BatchSummary>'),
(N'<BatchSummary BatchNumber="7" BatchType="SecOwnerTransfer">
<InterfaceTable TableName="ASR.aps_transferevents" RecordCount="1" />
<InterfaceTable TableName="ASR.aps_transferowners" RecordCount="3" />
<InterfaceTable TableName="ASR.APS_DeleteTransferEvents" RecordCount="10" />
</BatchSummary>');
-- DDL and sample data population, end
SELECT t.*
, c.value('sum(InterfaceTable/@RecordCount)', 'DECIMAL(12,2)') AS Result
FROM @tbl AS t
CROSS APPLY xmldata.nodes('/BatchSummary') AS t1(c);
-- to check data type of the RecordCount attribute
SELECT ID
, TRY_CAST(c.value('.', 'VARCHAR(30)') AS INT) AS Result
FROM @tbl AS t
CROSS APPLY xmldata.nodes('/BatchSummary/InterfaceTable/@RecordCount') AS t1(c);
输出
+----+--------+
| ID | Result |
+----+--------+
| 1 | 5022 |
| 2 | 14 |
+----+--------+