2016-09-20 10 views
0

私は、データベースからInstant Atlasで使用するデータを取得する作業を行っています。そして私はマップに表示したいデータを返すクエリを作成しました。 私が持っている問題は、XMLを正しくフォーマットすることができないということです。 私はスキーマ(Instant Atlas)を手に入れることができましたが、SQLの実行中に書式設定を適用する方法はありません。トラブルシューティング "FOR XML"

私のクエリは次のとおりです。

-- start of monthly pivot+ union 

--Start of inner Pivot 
DECLARE @year INT = 2015 

SELECT 
[LAName] AS '@LA' , [1] AS 'Jan', [2] AS 'Feb', [3] AS 'Mar', [4] AS 'Apr', 
[5] AS 'May', [6] AS 'Jun', [7] AS 'Jul', [8] AS 'Aug', [9] AS 'Sep', 
[10] AS 'Oct', [11] AS 'Nov', [12] AS 'Dec', 
[1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] AS 'Total' , 1 as [sort] 

FROM 
(SELECT 
L.LAName AS [LAName], 
SUBSTRING(C.AgencyCode, 1, 3) AS [AgencyCode], 
DATEPART(MONTH, ST.TransactionDate) as 'Date', 
DATEPART(YEAR, ST.TransactionDate) as 'Year', 
ST.TransactionId AS 'SyringeTransactions' 
FROM SyringeTransaction AS ST 
JOIN Client as C 
ON C.ClientId = ST.ClientId 
JOIN LocalAuthority as L 
ON SUBSTRING(C.AgencyCode, 1, 3) = L.LAShortCode 
WHERE SUBSTRING(C.AgencyCode, 1, 3) != 'XXX' AND C.Consent = 1 AND DATEPART(YEAR, ST.TransactionDate) = @year) AS SourceTable 
PIVOT 
(count(SyringeTransactions) FOR SourceTable.[Date] IN 
([1], [2], [3], [4], [5], [6], [7], [8], [9] ,[10], [11], [12]) 

) AS Transactions_2015_Area 
UNION 
--end of innerPivot 
--Start of outerPivot 

SELECT 
[AgencyCode] AS '@LA' , [1] AS 'Jan', [2] AS 'Feb', [3] AS 'Mar', [4] AS 'Apr', 
[5] AS 'May', [6] AS 'Jun', [7] AS 'Jul', [8] AS 'Aug', [9] AS 'Sep', 
[10] AS 'Oct', [11] AS 'Nov', [12] AS 'Dec', 
[1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] AS 'Total', 2 AS [sort] 

FROM 
(SELECT 
C.AgencyCode AS [AgencyCode], 
DATEPART(MONTH, ST.TransactionDate) as 'Date', 
DATEPART(YEAR, ST.TransactionDate) as 'Year', 
ST.TransactionId AS 'SyringeTransactions' 
FROM SyringeTransaction AS ST 
JOIN Client as C 
ON C.ClientId = ST.ClientId 
WHERE SUBSTRING(C.AgencyCode, 1, 3) != 'XXX' AND C.Consent = 1 AND DATEPART(YEAR, ST.TransactionDate) = @year) AS SourceTable 
PIVOT 
(count(SyringeTransactions) FOR SourceTable.[Date] IN 
([1], [2], [3], [4], [5], [6], [7], [8], [9] ,[10], [11], [12]) 

) AS Transactions_2015_Agency 

ORDER BY [sort], [@LA] 
--end of outerPivot 

-- end of pivot with union 
FOR XML PATH('Geography'), ROOT('SyringeTransactions'), TYPE 

一致する必要があるスキーマは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://data.instantatlas.com/atlas" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="AtlasData"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Geography"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="FeatureList"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element maxOccurs="unbounded" name="Feature"> 
         <xs:complexType> 
         <xs:sequence> 
          <xs:element maxOccurs="unbounded" name="FilterValue"> 
          <xs:complexType> 
           <xs:simpleContent> 
           <xs:extension base="xs:string"> 
            <xs:attribute name="for" type="xs:string" use="required" /> 
           </xs:extension> 
           </xs:simpleContent> 
          </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
         <xs:attribute name="id" type="xs:string" use="required" /> 
         <xs:attribute name="name" type="xs:string" use="required" /> 
         <xs:attribute name="href" type="xs:string" use="required" /> 
         </xs:complexType> 
        </xs:element> 
        <xs:element name="ComparisonFeature"> 
         <xs:complexType> 
         <xs:attribute name="id" type="xs:unsignedByte" use="required" /> 
         <xs:attribute name="name" type="xs:string" use="required" /> 
         <xs:attribute name="href" type="xs:string" use="required" /> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
       <xs:element name="FilterList"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element maxOccurs="unbounded" name="Filter"> 
         <xs:complexType> 
         <xs:attribute name="id" type="xs:string" use="required" /> 
         <xs:attribute name="name" type="xs:string" use="required" /> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
       <xs:element name="ThemeList"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element maxOccurs="unbounded" name="Theme"> 
         <xs:complexType> 
         <xs:sequence> 
          <xs:element maxOccurs="unbounded" name="Indicator"> 
          <xs:complexType> 
           <xs:sequence> 
           <xs:element maxOccurs="unbounded" name="Value"> 
            <xs:complexType> 
            <xs:simpleContent> 
             <xs:extension base="xs:string"> 
             <xs:attribute name="for" type="xs:string" use="required" /> 
             </xs:extension> 
            </xs:simpleContent> 
            </xs:complexType> 
           </xs:element> 
           <xs:element name="ComparisonValue"> 
            <xs:complexType> 
            <xs:simpleContent> 
             <xs:extension base="xs:float"> 
             <xs:attribute name="for" type="xs:unsignedByte" use="required" /> 
             </xs:extension> 
            </xs:simpleContent> 
            </xs:complexType> 
           </xs:element> 
           </xs:sequence> 
           <xs:attribute name="id" type="xs:string" use="required" /> 
           <xs:attribute name="name" type="xs:string" use="required" /> 
           <xs:attribute name="type" type="xs:string" use="required" /> 
           <xs:attribute name="precision" type="xs:unsignedByte" use="required" /> 
           <xs:attribute name="date" type="xs:string" use="required" /> 
           <xs:attribute name="href" type="xs:string" use="required" /> 
          </xs:complexType> 
          </xs:element> 
          <xs:element name="Property"> 
          <xs:complexType> 
           <xs:attribute name="name" type="xs:string" use="required" /> 
           <xs:attribute name="value" type="xs:string" use="required" /> 
          </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
         <xs:attribute name="id" type="xs:string" use="required" /> 
         <xs:attribute name="name" type="xs:string" use="required" /> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      <xs:attribute name="id" type="xs:string" use="required" /> 
      <xs:attribute name="name" type="xs:string" use="required" /> 
      <xs:attribute name="type" type="xs:string" use="required" /> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute name="version" type="xs:decimal" use="required" /> 
     <xs:attribute name="data-source" type="xs:string" use="required" /> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

これは、私がそれを完了したときにXMLが "すべきか"の要点です。

私は、データ保護の理由から、あなたの実際のテーブルWihtout

-<Theme name="xxxxxxxxxxxxx" id="t0" xmlns="http://data.instantatlas.com/atlas"> 


-<Indicator name="All individuals" id="i0" date="2014-15 Q1" precision="0" type="numeric"> 

<Value for="yyyyyyyy">303</Value> 

<Value for="yyyyyyyyy">633</Value> 

<Value for="yyyyyyy">240</Value> 

<Value for="yyyyyyyy">109</Value> 

<Value for="yyyyyyy">2183</Value> 

<Value for="yyyyyy">710</Value> 

<Value for="yyyyyy">340</Value> 

<Value for="yyyyyyy">176</Value> 

<Value for="yyyyyy">1525</Value> 

<ComparisonValue for="1">NaN</ComparisonValue> 

</Indicator> 


-<Indicator name="All individuals" id="i0" date="2014-15 Q2" precision="0" type="numeric"> 

<Value for="yyyyyyyy">324</Value> 

<Value for="yyyyyyyy">662</Value> 

<Value for="yyyyyyyy">334</Value> 

<Value for="yyyyyyy">124</Value> 

<Value for="yyyyyyy">2008</Value> 

<Value for="yyyyyyy">545</Value> 

<Value for="yyyyyyy">194</Value> 

<Value for="yyyyyyy">161</Value> 

<Value for="yyyyyyy">1630</Value> 

<ComparisonValue for="1">NaN</ComparisonValue> 

</Indicator> 


-<Indicator name="All individuals" id="i0" date="2014-15 Q3" precision="0" type="numeric"> 

<Value for="yyyyyyyyy">123</Value> 

<Value for="yyyyyyyy">499</Value> 

<Value for="yyyyyyyyy">298</Value> 

<Value for="yyyyyyyy">101</Value> 

<Value for="yyyyyyy">1816</Value> 

<Value for="yyyyyyyy">388</Value> 

<Value for="yyyyyyy">251</Value> 

<Value for="yyyyyyyy">103</Value> 

<Value for="yyyyyyy">1298</Value> 

<ComparisonValue for="1">NaN</ComparisonValue> 

</Indicator> 


-<Indicator name="Brief Interventions" id="i1" date="2014-15 Q1" precision="0" type="numeric"> 

<Value for="yyyyyyy">0</Value> 

<Value for="yyyyyyyyyyy">0</Value> 

<Value for="yyyyyy">444</Value> 

<Value for="yyyyyyyy">28</Value> 

<Value for="yyyyyyyy">12195</Value> 

<Value for="yyyyyyy">1239</Value> 

<Value for="yyyyyyyy">0</Value> 

<Value for="yyyyyyy">8</Value> 

<Value for="yyyyy">xxxx</Value> 

<ComparisonValue for="x">NaN</ComparisonValue> 

</Indicator> 
+0

有効なXMLの例がありますか?これを再構築する方がはるかに簡単です... – Shnugo

+0

"有効な" XMLの一部で私の投稿を編集しました Yの値は地名に対応します –

答えて

0

あなたを助けるために困難であるために値を削除しなければならなかったが、これはあなたが必要な構造を構築することができますどのように、あなたのアイデアを与えるだろう。 。

私はダミーデータとモックアップテーブルを使用します(これはあなたがMCVE(最小限、verifyable、完全な例)

DECLARE @indicator TABLE(RowID INT IDENTITY, Name VARCHAR(100),ID VARCHAR(100),[Date] VARCHAR(100),[Precision] INT,[Type] VARCHAR(100)); 
INSERT INTO @indicator VALUES('All individuals','i0','2014-15 Q1',0,'numeric') 
          ,('All individuals','i0','2014-15 Q1',0,'numeric'); 

DECLARE @values TABLE(indicatorID INT,forText VARCHAR(100),Value INT); 
INSERT INTO @values VALUES(1,'yyyyyyy',100) 
         ,(1,'yyyyyyy',200) 
         ,(1,'yyyyyyy',300) 
         ,(1,'yyyyyyy',400) 
         ,(1,'yyyyyyy',500) 
         ,(1,'yyyyyyy',600) 
         ,(1,'yyyyyyy',700) 
         ,(1,'yyyyyyy',800) 
         ,(1,'yyyyyyy',900) 

         ,(2,'yyyyyyy',110) 
         ,(2,'yyyyyyy',210) 
         ,(2,'yyyyyyy',310) 
         ,(2,'yyyyyyy',410) 
         ,(2,'yyyyyyy',510) 
         ,(2,'yyyyyyy',610) 
         ,(2,'yyyyyyy',710) 
         ,(2,'yyyyyyy',810) 
         ,(2,'yyyyyyy',910); 

を作成するために提供されているべきものです - クエリ

--Create without namespaces (otherwise each sub-select would get the namespace declaration 
DECLARE @InnerXML XML= 
(
    SELECT i.Name AS [@name] 
      ,i.ID AS [@id] 
      ,i.Date AS [@date] 
      ,i.Precision AS [@precision] 
      ,i.Type AS [@type] 
      ,(
      SELECT v.forText AS [@for] 
        ,v.Value AS [*] 
      FROM @values AS v 
      WHERE v.indicatorID=i.RowID 
      FOR XML PATH('Value'),TYPE 
      ) 
    FROM @indicator AS i 
    FOR XML PATH('Indicator'),TYPE 
); 

WITH XMLNAMESPACES(DEFAULT 'http://data.instantatlas.com/atlas') 
SELECT 'xxxxxx' AS [@name] 
     ,'t0' AS [@id] 
     ,@innerXML AS [*] 
FOR XML PATH('Theme') 

結果:

<Theme xmlns="http://data.instantatlas.com/atlas" name="xxxxxx" id="t0"> 
    <Indicator name="All individuals" id="i0" date="2014-15 Q1" precision="0" type="numeric" xmlns=""> 
    <Value for="yyyyyyy">100</Value> 
    <Value for="yyyyyyy">200</Value> 
    <Value for="yyyyyyy">300</Value> 
    <Value for="yyyyyyy">400</Value> 
    <Value for="yyyyyyy">500</Value> 
    <Value for="yyyyyyy">600</Value> 
    <Value for="yyyyyyy">700</Value> 
    <Value for="yyyyyyy">800</Value> 
    <Value for="yyyyyyy">900</Value> 
    </Indicator> 
    <Indicator name="All individuals" id="i0" date="2014-15 Q1" precision="0" type="numeric" xmlns=""> 
    <Value for="yyyyyyy">110</Value> 
    <Value for="yyyyyyy">210</Value> 
    <Value for="yyyyyyy">310</Value> 
    <Value for="yyyyyyy">410</Value> 
    <Value for="yyyyyyy">510</Value> 
    <Value for="yyyyyyy">610</Value> 
    <Value for="yyyyyyy">710</Value> 
    <Value for="yyyyyyy">810</Value> 
    <Value for="yyyyyyy">910</Value> 
    </Indicator> 
</Theme> 
+0

この回答に取り組んでいただきありがとうございます@shnugo私は作業を開始しますこれで一気に –

+0

こんにちは@PhilipMather、この問題は解決しましたか?さらに助けが必要ですか? – Shnugo

+0

私が望んでいた方法ではありません。私はアプローチのようなストリームから離れ、C#でHTTPハンドラを使用して手動でXMLを構築することに決めました。 出力をビルドするのに時間がかかりますが、おそらく毎週新しい出力を作成するだけです。 –

関連する問題