【发布时间】:2020-07-30 19:06:06
【问题描述】:
我正在尝试从 xml 列中导出一些信息。
应用逻辑: 在每个部分中,演员姓名可能会出现两次。 (演员 + 歌曲)产生独特的组合。
仅在聚合级别、场景 = 1 时保留。我们不关心场景=2。 我们关心特定演员的台词。 辛巴、木法沙和扎祖。 让我们不要担心疤痕。他很卑鄙。 我的预期输出是:
到目前为止我的 T-Sql 代码: 我已经设法过滤掉聚合级别 = 2。
有人可以帮忙吗?
DECLARE @Temp AS TABLE (Information xml)
INSERT INTO @Temp SELECT @x;
WITH XMLNAMESPACES ('lyceum' AS ns1)
SELECT
Z.query('ns1:feature')
FROM @Temp
CROSS APPLY Information.nodes('/ns1:report/ns1:detail/ns1:parts/ns1:part') AS Y(Z)
WHERE Z.exist('ns1:aggregation[.="1"]') = 1
您可以在下面找到 xml,以便轻松重现:
DECLARE @x as xml;
set @x = '<ns1:report xmlns:ns1="lyceum">
<ns1:header>
<ns1:report>The Lion King</ns1:report>
</ns1:header>
<ns1:detail>
<ns1:parts>
<ns1:part>
<ns1:aggregation>
<ns1:scene>1</ns1:scene>
</ns1:aggregation>
<ns1:feature>
<ns1:actor>Simba</ns1:actor>
<ns1:lines>100</ns1:lines>
<ns1:song>1</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Mufasa</ns1:actor>
<ns1:lines>200</ns1:lines>
<ns1:song>1</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Simba</ns1:actor>
<ns1:lines>300</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Zazu</ns1:actor>
<ns1:lines>400</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Scar</ns1:actor>
<ns1:lines>500</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
</ns1:part>
<ns1:part>
<ns1:aggregation>
<ns1:scene>2</ns1:scene>
</ns1:aggregation>
<ns1:feature>
<ns1:actor>Simba</ns1:actor>
<ns1:lines>600</ns1:lines>
<ns1:song>1</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Mufasa</ns1:actor>
<ns1:lines>700</ns1:lines>
<ns1:song>1</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Simba</ns1:actor>
<ns1:lines>800</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Zazu</ns1:actor>
<ns1:lines>900</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
<ns1:feature>
<ns1:actor>Scar</ns1:actor>
<ns1:lines>1000</ns1:lines>
<ns1:song>2</ns1:song>
</ns1:feature>
</ns1:part>
</ns1:parts>
</ns1:detail>
</ns1:report>'
【问题讨论】:
标签: sql-server tsql xquery-sql