SQL でクエリを実行しようとしている XML があります。
  <QueryB>
      <investment name="InvestmentA">
        <Account Type="DIVIDEND">
          <glsum YTD="0.0000" />
          <glsum Inception="111111.0000" />
          <glsum QTD="0.0000" />
        </Account>
      </investment>
      <investment name="InvestmentB">
        <Account Type="DIVIDEND">
          <glsum YTD="0.0000" />
          <glsum Inception="222222.0000" />
          <glsum QTD="0.0000" />
        </Account>
      </investment>
      <investment name="InvestmentC">
        <Account Type="CAP">
          <glsum YTD="90.0000" />
          <glsum Inception="333333.0800" />
          <glsum QTD="90.0000" />
        </Account>
      </investment>
      <investment name="InvestmentD">
        <Account Type="CAP">
          <glsum YTD="0.0000" />
          <glsum Inception="555555.0000" />
          <glsum QTD="0.0000" />
        </Account>
        <Account Type="DIVIDEND">
          <glsum YTD="0.0000" />
          <glsum Inception="444444.0000" />
          <glsum QTD="0.0000" />
        </Account>
      </investment>
InvestmentD には Dividend と Cap の両方のアカウント タイプがあることに注意してください。そこで、次の方法でこのデータをクエリしようとしました。
select rtrim(ltrim(t.c.value('@name', 'nvarchar(500)'))) as name,
    rtrim(ltrim(t.c.value('(Account/@Type)[1]', 'nvarchar(500)'))) as Type,
    rtrim(ltrim(t.c.value('(Account/glsum/@YTD)[1]', 'nvarchar(500)'))) as YTD,
    rtrim(ltrim(t.c.value('(Account/glsum/@Inception)[1]', 'nvarchar(500)'))) as inception,
    rtrim(ltrim(t.c.value('(Account/glsum/@QTD)[1]', 'nvarchar(500)'))) as QTD
from @x.nodes('/QueryB/investment')t(c)
ここで、@x は XML です。これは驚くべきことではありませんが、InvestmentD から両方のノードを取得するわけではありません。すべてのノードを解析する方法がわかりません。正しい方向へのポインタをいただければ幸いです。ありがとう。