私は、データベースから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>
有効なXMLの例がありますか?これを再構築する方がはるかに簡単です... – Shnugo
"有効な" XMLの一部で私の投稿を編集しました Yの値は地名に対応します –