我的任务是从我们的数据库中获取一些与Instant Atlas一起使用的数据。我创建了一个查询,它将返回我想在地图上显示的数据。 我遇到的问题是我似乎无法正确格式化XML。 我已经能够掌握模式(Instant Atlas),但我不知道如何在SQL执行期间应用格式化。
我的查询如下;
-- 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。
由于数据保护原因,我不得不删除值
-<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 :(得分:0)
关于你的实际表格很难帮助你,但这会给你一个想法,你如何建立你需要的结构。
我将模拟表与虚拟数据一起使用(这是您应该提供的,以便创建 MCVE(最小,可验证,完整的示例)。
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>