我无法将SQL Select语句输出到我的XML以用于我的Graph
我有这些结果
December 2011
470
FRESENIUS
January 2012
434
FRESENIUS
February 2012
278
FRESENIUS
February 2012
2
STORESID
我需要像这样输出它们,这样我就可以循环我的代码并生成XML
Month Year
FRESNIUS
STORESID
December 2011
470
0
January 2012
434
0
February 2012
278
2
请注意我没有包含标题,这些都是列结果。 此外,STORESID和FRESNIUS不是静态价值观。多个存储器存在,因此我还需要动态扩展我的列数。
我有点需要输出第二个来通过PHP正确生成XML,我已经拥有了。还是有其他正确的方式。
感谢。
在下面查询我正在讨论的输出
SELECT DATENAME(month, orderdate) + ' ' + CAST(Year(orderdate) AS VARCHAR(4)) AS 'Month Year' , count(*) 'Number of Orders', storerkey FROM orders GROUP BY storerkey, DATENAME(month, orderdate) + ' ' + CAST(Year(orderdate) AS VARCHAR(4)), CAST(Year(orderdate) AS VARCHAR(4)) + RIGHT('0' + CAST(Month(orderdate) AS VARCHAR(2)),2) ORDER BY storerkey, CAST(Year(orderdate) AS VARCHAR(4)) + RIGHT('0' + CAST(Month(orderdate) AS VARCHAR(2)),2)
答案 0 :(得分:1)
您可以将表格加入到自身中。如果我对列名做出假设,假设可能缺少FRESNIUS或STORESID,并使用公用表表达式(未经测试!):
WITH date_fresnius AS (
SELECT [Month Year], FRESNIUS
FROM original_table
)
, date_storesid AS (
SELECT [Month Year], STORESID
FROM original_table
)
SELECT
ISNULL( f.[Month Year], s.[Month Year]) AS [Month Year]
, ISNULL( f.FRESNIUS, 0 ) AS FRESNIUS
, ISNULL( s.STORESID, 0 ) AS STORESID
FROM
date_fresnius f
FULL OUTER JOIN date_storesid s ON (f.[Month Year] = s.[Month Year])
如果您总是拥有FRESNIUS记录,则可以使用LEFT JOIN。
答案 1 :(得分:1)
我不是100%确定您需要的XML输出,但您可以利用SQL-Servers FOR XML clause和PIVOT功能。这应该让你开始:
CREATE TABLE #Test (GraphDate DATETIME, Value INT, Type VARCHAR(50))
INSERT #Test VALUES
('01/12/2011', 470, 'FRESENIUS'),
('01/01/2012', 434, 'FRESENIUS'),
('01/12/2012', 278, 'FRESENIUS'),
('01/02/2012', 2, 'STORESID')
SELECT DATEPART(YEAR, GraphDate) [Year],
DATENAME(MONTH, GraphDate) [Month],
ISNULL(FRESENIUS, 0) [FRESENIUS],
ISNULL(STORESID, 0) [STORESID]
FROM #Test
PIVOT
( SUM(Value)
FOR Type IN ([FRESENIUS], [STORESID])
) PivotTable
FOR XML PATH('row'), ROOT
DROP TABLE #Test
修改强>
以下是您根据您提供的查询要求的基本查询。
SELECT DATENAME(MONTH, OrderDate) + ' ' + DATENAME(YEAR, OrderDate) [MonthYear],
ISNULL(FRESENIUS, 0) [FRESENIUS],
ISNULL(STORESID, 0) [STORESID]
FROM Orders
PIVOT
( COUNT(StorerKey)
FOR StorerKey IN ([FRESENIUS], [STORESID])
) PivotTable
ORDER BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)
编辑2
我认为SQL-Server 2000不支持PIVOT
。您需要使用聚合函数:
SELECT DATENAME(MONTH, OrderDate) + ' ' + DATENAME(YEAR, OrderDate) [MonthYear],
COUNT(CASE WHEN StorerKey = 'FRESENIUS' THEN 1 END) [FRESENIUS],
COUNT(CASE WHEN StorerKey = 'STORESID' THEN 1 END) [STORESID]
FROM Orders
GROUP BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate), DATENAME(YEAR, OrderDate), DATENAME(MONTH, OrderDate)
ORDER BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)
我认为某些FOR XML
功能仍然可用,因此您仍然可以直接从SQL输出XML。
编辑3
DECLARE @SQL NVARCHAR(2000)
SELECT @SQL = ISNULL(@SQL, '') + ', COUNT(CASE WHEN StorerKey = ''' + StorerKey + ''' THEN 1 END) [' + StorerKey + ']'
FROM ( SELECT DISTINCT StorerKey
FROM Orders
) Keys
SET @SQL = 'SELECT DATENAME(MONTH, OrderDate) + '' '' + DATENAME(YEAR, OrderDate) [MonthYear]' + @SQL +
' FROM Orders
GROUP BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate), DATENAME(YEAR, OrderDate), DATENAME(MONTH, OrderDate)
ORDER BY DATEPART(YEAR, OrderDate), DATEPART(MONTH, OrderDate)'
EXECUTE SP_EXECUTESQL @SQL
答案 2 :(得分:1)
根据您的查询:
SELECT
DATENAME(MONTH, orderdate) + ' ' + CAST(YEAR(orderdate) AS VARCHAR(4)) AS [Month Year]
COUNT(CASE storerkey WHEN 'FRESNIUS' THEN 1 END) AS FRESNIUS,
COUNT(CASE storerkey WHEN 'STORESID' THEN 1 END) AS STORESID
FROM orders
GROUP BY YEAR(orderdate), MONTH(orderdate), DATENAME(MONTH, orderdate)
ORDER BY YEAR(orderdate), MONTH(orderdate)
输出:
Month Year FRESNIUS STORESID
December 2011 470 0
January 2012 434 0
February 2012 278 2